Selecting a value in a combo box

skippypower

Registered User.
Local time
Today, 14:43
Joined
Jan 6, 2012
Messages
10
Hi,

Im trying to select a value in a combo box. using this code:

Form_frmExpandAssembly.cboAssemblyID.SetFocus
Form_frmExpandAssembly.cboAssemblyID.ListIndex = 0
Form_frmExpandAssembly.cboAssemblyID.Dropdown

This selects the first value but i want to select a value that is equal to a text box i have elsewhere.

I have tryed:

Form_frmExpandAssembly.cboAssemblyID.SetFocus
Form_frmExpandAssembly.cboAssemblyID = Text23
Form_frmExpandAssembly.cboAssemblyID.Dropdown

Do i need some form of look up code?? or am i just being a spanner??
 
I would first of all point out that you shouldn't be using Form_ to reference the form. If you're referencing the form where the code is written, just use Me. If you want to reference another form, use Forms!FormName or Forms("FormName")

How many records would your combo box contain? If it's not too many you will need to loop through the items in the combo box until you find a match.
 
You will need to put the code under an event such as the on change or on exit event of the text box.

Code:
Form_frmExpandAssembly.cboAssemblyID.value = Text23.value

If your combobox is set to not allow edits or additions and also set to limit to list then if whatever is entered in the textbox does not match the combo box list then access will issue a pop up and tell the user its not valid.

If you do not have your combo box set to this then you most def. need to do a search on your combobox to make sure that whatever is entered in the textbox is actually listed in the combobox.
 
I just need to stress again that this Form_frmExpandAssembly shouldn't be used as a form of reference. That refers to the class and not the form object.

If the OP just wants to synch the textbox to the Combo box then all he or she needs to do is to set the Control Source of the Combo Box to the textbox. But this is not how I understood the OP.
 
Thanks guys

yeah the code does reference another form with a public sub setting. Not sure if this is the best way of doing it?? The total code is:

Private Sub Text23_Click()

DoCmd.OpenForm "frmExpandAssembly"

Form_frmExpandAssembly.cboAssemblyID.SetFocus
Form_frmExpandAssembly.cboAssemblyID.Value = Text23.Value
Form_frmExpandAssembly.cboAssemblyID.Dropdown


Call Form_frmExpandAssembly.cmdExpand_Click

End Sub

It is opening a form and expanding a tree view based on the selection made in the combo box. The code opens the form and runs cmdExpand_Click but does not select the value in to combo box. Now tried with the .value but no joy :(

There will probably be a max of 35 choices in the combo box.
 
Private Sub Text23_Click()

DoCmd.OpenForm "frmExpandAssembly"

Form_frmExpandAssembly.cboAssemblyID.SetFocus
Form_frmExpandAssembly.cboAssemblyID.Value = Text23.Value
Form_frmExpandAssembly.cboAssemblyID.Dropdown


Call Form_frmExpandAssembly.cmdExpand_Click

End Sub
I've stressed about your use of Form_ twice already but you still continue to use it.

The value you pass must be the type of value found in the bound column of the combo box. In most cases this is the first column in the combo box.
 
Awsome thanks vbaInet. I was passing over info that was not for the same columb as the combo box. I am a fool!!

Sorted now!!

I have also changed all my Forms_ to Forms!FormName

Thanks :)
 
You're not a fool, you just needed a nudge in the right direction. :)

Glad to see it's sorted and happy to know you took that Form_ thing on board.
 

Users who are viewing this thread

Back
Top Bottom