Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA code to populate an Unbound combobox with a query on Click event 1

Status
Not open for further replies.

08211987

Programmer
Apr 20, 2012
187
0
0
US
I have a query "qry_Combo_DivName" that displays distinct values of division name. I have a combobox that I want blank until the dropdown is clicked. Shouldn't this code work? It just displays a blank list.

Private Sub combo_MDV_DES_TXT_Click() 'name of combobox
Me.combo_MDV_DES_TXT.RowSource = "qry_Combo_DivName"
Me.combo_MDV_DES_TXT.Requery
End Sub

Thanks!
 
Your combo_Click() event fires when you select an item from the combo box. Since there is nothing in the combo, you did not populate it with anything, there is nothing to click on, therefore this event never fires... :-(

Is there any other place you can populate your combo from?

Have fun.

---- Andy
 
Code:
Private Sub combo_MDV_DES_TXT_Enter()
    Me.combo_MDV_DES_TXT.RowSource = "qry_Combo_DivName"
End Sub
 
I have 3 combos that I make visible depending on which radio button is selected. Here's one example and it still displays blanks. The query does actually work when running by itself.

Private Sub radio_Division_GotFocus()
frame_Filter_Group.Value = 1
combo_MDV_DES_TXT.Visible = True
combo_MSD_DES_TXT.Visible = False 'I false this just in case
combo_PI_Contact_Name.Visible = False 'I false this just in case
Me.combo_MDV_DES_TXT.RowSource = "qry_Combo_DivName"
Me.combo_MDV_DES_TXT.Requery
End Sub

I also thought I could populate one of them on the form load event but that did nothing.
Private Sub Form_Load()
Me!combo_MDV_DES_TXT.RowSource = "qry_Combo_DivName"
Me!combo_MDV_DES_TXT.Requery
DoCmd.Maximize
frame_Filter_Group.Value = 0
End Sub

Maybe I should mention the properties in case they are not correct. Control Source, Row Source, Row Source Type are all blank but the "Bound Column" is 1. Could any of that be the issue?

Thanks!

 
rowsource type is table/query
ensure you have a column width/s specified.
 
Excellent! Works like a charm!
When you mention column widths, I don't have any defined, is that only used when there is more than 1 column or is it good practice to always code those?

Thanks!
 
Probably about 80% of my combos have a hidden primary key field and a visible field. Where you see the descriptive name and save the primary key. So the properties would be something like

number columns:2
Bound column:1
column widths:0;1"

So not sure what would happen if you bind it to a query without saving these properties. You may not get the visible column you want. But normally if you return a single column, I do not think you have to specify a width and it fills the whole combo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top