Is it possible to make a selection from a combobox dropdown list entirely in VBA code? I have a form with a cbo whose initial selection is known by the application when the form is loaded. For convenience I want to have the program make the initial selection.
I will use the code that I am trying to get working (unsuccessfully) as an example of what I’m trying to do. If you have a solution, in no way has it to resemble my code (which, again, doesn’t work). We start with a table of composer names which contains two fields: txtCompCode and txtCompName. The cbo in question is to be loaded from the table of composer names.
Private Sub Form_Open(Cancel As Integer)
Me![cboComposerNames].SetFocus
‘Comment: This gives the cbo immediate focus and control to cboComposerNames_GotFocus below.
End Sub
Private Sub cboComposerNames_GotFocus()
Me![cboComposerNames].RowSource = _
“SELECT txtCompCode, txtCompName FROM tblComposers WHERE txtCompCode = ‘GERS’;”
‘Comment: As the select criteria “GERS” is known, this SQL creates an internal dropdown list consisting of a single line.
Me![cboComposerNames] = Me![cboComposerNames].Column(1, 0)
‘Comment: This attempts to assign the single dropdown line to the cbo’s listbox. It fails; the listbox remains blank..
Debug.Print Me![cboComposerNames]
‘Comment: This is to show the result of the above assignment. It works!?! Gershwin’s name is printed although the listbox is still blank..
End Sub
(Note. Inserting Me![cboComposerNames].dropdown immediately following the SQL statement does produce the one line list, but, after selecting it, the results are exactly the same as described below.)
The application has now stopped. To get it moving again I click on the cbo’s down arrow and select the single entry for Gershwin. cbo_GotFocus is run again, cbo_AfterUpdate is run and the application continues on using the selected code and name as it should EXCEPT that the cbo’s listbox continues to show blank.
I simply want to know how, on initial entry, to accomplish the selection of the lone dropdown line with its subsequent raising of cbo_AfterUpdate. OR, as mentioned above, something completely different that results in the same thing without user intervention.
Thanks to all of you that will put in time and thought.
John