1
votes

I have a Combo box on a form and want to populate from table with vba code. The query has two columns proc_name and then ID and have set combo box column count as 2, but when form is loaded I see ID first and then proc_name. Why is this happening?

For the other combo box with similar table structure they show column order how I have defined in the query.

Private Sub Form_Load()

   Dim strSQL As String

   createCon

    strSQL = "Select proc_name, ID from tblProcess"

    Set objRecordset = New ADODB.Recordset
    objRecordset.Open strSQL, objConnection, adOpenKeyset, adLockOptimistic

    If Not (objRecordset.EOF And objRecordset.BOF) Then
        Set Me.cmbProcess.Recordset = objRecordset
    End If

    objRecordset.Close
    Set objRecordset = Nothing

End Sub

enter image description here

1
What happens with this change? strSQL = "Select proc_name AS f1, ID AS f2 from tblProcess"HansUp
When I used your query, it shows correctly, what's the reason?Santosh
It seems the recordset columns are processed in alphabetical order when loading into the combo box. I aliased the field names so they are processed in the order I think you want.HansUp
Oh ok. Thanks a ton for your time!Santosh

1 Answers

2
votes

When the Recordset columns are loaded into the combo box, it appears they are processed in alphabetical order (ID before proc_name).

Alias the fields in your SELECT field list so they are processed in the order you wish:

strSQL = "Select proc_name AS f1, ID AS f2 from tblProcess"