Solved SQL Statement in VBA (1 Viewer)

silversun

Registered User.
Local time
Today, 13:51
Joined
Dec 28, 2012
Messages
204
Hi everybody.
Please help me on following syntax error.
I am using a string variable as RowSource of combo box: cmb_unit.
As you can see in the attached image it has syntax error and I couldn't figure it out.
Code:
Private Sub cmb_items_AfterUpdate()
Dim sSource As String
sSource = "SELECT t_units.[unit_ID], t_units.[unit], t_units.[unit_index] " & _
          "FROM t_units " & _
          "WHERE t_units.unit_index= " & Me.cmb_items.Column(2)
    
Me.cmb_unit.RowSource = sSource

End Sub
I appreciate your help.
 

Attachments

  • Capture.JPG
    Capture.JPG
    20.3 KB · Views: 253

MajP

You've got your good things, and you've got mine.
Local time
Today, 16:51
Joined
May 21, 2018
Messages
8,555
need a space before the = sign.
 

Cronk

Registered User.
Local time
Tomorrow, 06:51
Joined
Jul 4, 2013
Messages
2,774
Is Me.cmb_items.Column(2) a text field? If so it should be

Code:
WHERE t_units.unit_index= '" & Me.cmb_items.Column(2) &"'"

@ MajP I;ve not had issues with no space before or after the = sign
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 04:51
Joined
May 7, 2009
Messages
19,246
what is the Rowsource of combo cmb_items?
can you show some records of the combo.
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 04:51
Joined
May 7, 2009
Messages
19,246
you have only 2 columns in cmb_items.
on vba, column 1 is refered as 0, column 2 as 1, etc.

so there is No Me.cmb_items.Column(2)
what you have is Me.cmb_items.Column(0) (which is column 1) and Me.cmb_items.Column(1), which is column 2.

you may try (if you are referring to Column 2):

sSource = "SELECT t_units.[unit_ID], t_units.[unit], t_units.[unit_index] " & _
"FROM t_units " & _
"WHERE t_units.unit_index= " & Me.cmb_items.Column(1)
 

silversun

Registered User.
Local time
Today, 13:51
Joined
Dec 28, 2012
Messages
204
you have only 2 columns in cmb_items.
on vba, column 1 is refered as 0, column 2 as 1, etc.

so there is No Me.cmb_items.Column(2)
what you have is Me.cmb_items.Column(0) (which is column 1) and Me.cmb_items.Column(1), which is column 2.

you may try (if you are referring to Column 2):

sSource = "SELECT t_units.[unit_ID], t_units.[unit], t_units.[unit_index] " & _
"FROM t_units " & _
"WHERE t_units.unit_index= " & Me.cmb_items.Column(1)
Thank you very much.
I added the third column in my query design and it started working.
Good catch.
Thank you a lot
 

Gasman

Enthusiastic Amateur
Local time
Today, 21:51
Joined
Sep 21, 2011
Messages
14,369
If you had put it all into a string variable and used debug.print to display it, it would have been pretty obvious?
 

Users who are viewing this thread

Top Bottom