| View previous topic :: View next topic |
| Author |
Message |
windyweather General User


Joined: 14 May 2008 Posts: 19 Location: Coos Bay, Oregon
|
Posted: Mon Jan 05, 2009 7:58 pm Post subject: Combo Box in a Base Subform? |
|
|
I have a database with 3 tables:
Character
Stones_Held
Stone_Types
I'd like to make a form to fill out the Stones_Held table.
It has the following fields:
ID - autofill unique id
Stone_ID - ID joined from Stones_Types field
Number - of stones held
Stone Types is:
ID - unique ID of the stone type
Stone_Type_Name
I have made a form with the main form from the Character table, and a sub form of the Stones_Held table - joined across Character ID. This works.
I would now like to be able to set the Stone_Type in the sub form from a combo box that is populated by the Stone_Types table.
I have looked at the data tab of the combo box and nothing seems to work. In one case I got numbers, but what I want to do is pull down the box and see the Stone_Type_Name in the combo box, but when I make a selection, the field is actually filled in with the ID of the corresponding record. To maintain Normal Form of the database in a user friendly manner. Surely this is what the Combo Box is for, right?
If you want to know the why and wherefore of the project, here's the blog entry with all the details and an example - slightly out of date.
http://www.windyweather.net/wp/2009/01/05/2moons-management-database/
{{ This question is a simplified example of the real thing. }}
Thanks much for your help,
- windy |
|
| Back to top |
|
 |
Voobase OOo Advocate


Joined: 21 Nov 2007 Posts: 392 Location: Australia
|
Posted: Tue Jan 06, 2009 3:43 am Post subject: |
|
|
Hi there Windy,
You may find that you need to use a listbox instead. The differences between a listbox and a combobox is that a listbox can display one thing (Stone_Type) and bind a different thing (ID from Stone_Type table) into the table field you choose (Stone_ID) of the form the listbox is sitting in. A combobox can only display the same thing as to which it binds to the datafield. There is also a difference between the two as far as operation goes as with a combobox you could type into it a different word than is already in its list. This could make the choice hard between the two types of controls.
It is possible to use a little macro with a combobox to enable it to bind a separate value to the form than it is displaying. I'll try to dig up an example.
The easiest way to put these controls onto your form is to have the "wizards" button pushed in (which is towards the bottom of the "form controls" toolbar). This steps you through the creation of the list or combo box after you have drawn it onto your form in design mode. I'll have a quick look for an example of the macro you would need to turn a combobox into a listbox (sort of).........
If you really want to use a combobox, have a look at this post here (towards the bottom) where I have discussed the issue. http://www.oooforum.org/forum/viewtopic.phtml?t=72134 I'll reproduce the macro here but change it a bit. At the moment the macro uses SQL to determine the ID from what was selected in the combobox and then SQL to put it into the data table of the form the combobox is sitting in. I will add one line so that the value is copied to the rowset of the form as well. This means the value is available in the form straight away without needing to use refreshrow() to bring the data from the table to the form again. The other thing is that when a new value is written into the combobox it will not be stored in the table that supplies the combobox. I'll see if I can pen a little section that offers to save it to the table. I have tried to use your field and form names where possible however check them for accuracy with case etc. I probably won't be able to test it so let me know if it works or not.
| Code: | Rem... this macro is sort of like adding a separate bound field to a combobox like a listbox has. While you are using the combobox's
Rem... natural features to copy the displayed value somewhere, this macro copies the associated primary key. By Voo!
Rem... Triggered from the item status changed event of the combobox.
Sub Combobox_Add_Extra_Bound_Field (oEv as object)
dim oSubForm as object
dim oControl as object
dim CbxValue as string
dim FieldName_ID as integer
dim Sub_ID as integer
dim Result
dim strSQL As String
dim oCreateStatement as object
oSubForm = oEv.source.model.parent
oControl = oSubForm.GetByName("cbxStone") Rem... check what your combobox is named!
CbxValue = oControl.CurrentValue
strSQL="SELECT ""Stone_Type_Name"",""ID"" FROM ""Stone_Types"" WHERE ""Stone_Type_Name"" = '" & cbxStone & "'" Rem... check what your combobox is named!
oCreateStatement = oSubForm.ActiveConnection.CreateStatement
Result = oCreateStatement.executeQuery(strSQL)
If Result.next() then rem... this actually does a result.next()
FieldName_ID = Result.getInt2)
Else
Rem... Value from combobox not in Stone_Type table
Rem... First ask the question...
IF Msgbox("Would you like to add in anew type of stone?",4,"I have a question for you!") =6 Then
Rem... First use SQL and INSERT statement to put in table
strSQL = "INSERT INTO ""Stone_Types"" ( ""Stone_Type_Name"" ) VALUES ( '" & CbxValue & "' ) "
oCreateStatement = oSubForm.ActiveConnection.CreateStatement
oCreateStatement.executeUpdate(strSQL)
Rem... Next use SELECT to get the value from the table to see what ID it was given
strSQL="SELECT ""ID"" FROM ""Stone_Types"" WHERE ""Stone_Type_Name"" = '" & CbxValue & "' "
oCreateStatement = oSubForm.ActiveConnection.CreateStatement
Result2 = oCreateStatement.executeQuery(strSQL)
Result2.Next()
Rem... now do a result.next() and put this ID value into FieldName_ID variable
If Result2.next() then rem... this actually does a result.next()
FieldName_ID = Result2.getInt(1)
End if
Else
exit sub
End if
End if
Sub_ID = oSubForm.GetInt(oSubForm.FindColumn("ID")) Rem set the ID of subform row into variable
Rem... Copy to SubForm's rowset (exists only in form until save is pressed)...
oSubForm.UpdateInt( oSubForm.FindColumn("Stone_ID"), FieldName_ID )
Rem... Copy to table of the form that the combobox is sitting in using SQL...
dim oPrepStatement
dim sSQL as string
Rem... This bit updates the table with the Foreign Key field of what was selected in combobox.
sSQL = "UPDATE ""Stones_Held"" SET ""Stone_ID"" = ? "
sSQL = sSQL & "WHERE ""ID"" = ? "
oPrepStatement = oSubForm.ActiveConnection.prepareStatement( sSQL )
oPrepStatement.setInt( 1, FieldName_ID ) Rem... PK of table which holds the combobox list
oPrepStatement.setInt( 2, Sub_ID ) Rem... The ID value of the row the subform is on.
oPrepStatement.executeUpdate()
End Sub |
| Code: | Sub Text_Modified_Enter Pressed (oEv as object)
Rem... Run off text modified event of combobox so when enter pressed the sub above is run as I don't think hitting enter drives the item status changed event.
If oEv.KeyCode = 1280 then
Combobox_Add_Extra_Bound_Field (oEv)
Else
Exit sub
End if
End sub |
OK, so there are actually two subs there. The first one should be driven from the "Item status changed" event of the combobox whilst the second should be driven from the "text Modified" event of the combobox.
If you are not used to macro's, here are some quick instructions. Press Alt F11 and the Macro dialog launches. Select "My Macro's" on the left and then "Standard". Inside "Standard" there should be "Module1". Past the code in there (or create a new module etc). With your form, whilst in design mode, right click the combobox and choose properties and then the events tab. Assign the sub's to their appropriate events by clicking the [...] and then finding where you put the code once the dialog box appears.
If you try it out and it doesn't work I am not surprised. I have not tested it. Have a look yourself and check all the names for correct case etc and post back here with what it is doing (or not doing).
Cheers
Voo |
|
| Back to top |
|
 |
windyweather General User


Joined: 14 May 2008 Posts: 19 Location: Coos Bay, Oregon
|
Posted: Wed Jan 07, 2009 11:10 pm Post subject: Got it working... ;) Thanks... |
|
|
Voobase,
Thanks much. Listboxes are just fine. I've got it working very nicely. I've updated my posting here: http://www.windyweather.net/wp/2009/01/05/2moons-management-database/ with a working example.
I'll probably add a form for Rings too, since they seem to be an important loot type.
Since I"m using a spreadsheet for the subform, the wizard doesn't work. But I made a field outside the spreadsheet and put a list box in it and the wizard made the field work, then I copied the sittings into the column for the sheet and it worked very nicely.
Thanks again,
- windy |
|
| Back to top |
|
 |
Voobase OOo Advocate


Joined: 21 Nov 2007 Posts: 392 Location: Australia
|
Posted: Wed Jan 07, 2009 11:19 pm Post subject: |
|
|
Hi Windy,
| Quote: |
I've got it working very nicely. |
Good stuff!
| Quote: |
Since I"m using a spreadsheet for the subform, the wizard doesn't work. But I made a field outside the spreadsheet and put a list box in it and the wizard made the field work, then I copied the sittings into the column for the sheet and it worked very nicely. |
10 points for lateral thinking! Yes, I've heard the Wizard does not give much help when putting a listbox inside a table grid control.
Cheers
Voo |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|