Syntax error in Access 2007 query
Hello everyone. I need help troubleshooting a query error. There is a form where if a user selects an Employee Id from a Combobox, the Label Below automatically displays the Employee Name.
Here is the query,
Code:
rs.Open "SELECT tblEmp.EM_ID, tblEmp.NAME FROM tblEmp where tblEmp.EM_ID = " & Me.cboEmpId.Text & " ;", cn, adOpenKeyset, adLockPessimistic
But, Due to some reasons, It's not working.
Codes,
Code:
Private Sub cboEmpId_Change()
If Me.cboEmpId.Text = vbNullString Then Exit Sub
If rs.State = adStateOpen Then rs.Close
rs.Open "SELECT tblEmp.EM_ID, tblEmp.NAME FROM tblEmp where tblEmp.EM_ID = " & Me.cboEmpId.Text & " ;", cn, adOpenKeyset, adLockPessimistic
Do While rs.EOF = False
Me.lblName.Caption = rs.Fields("NAME")
rs.MoveNext
Loop
Exit Sub
Set rs = Nothing
End Sub
Thanks in advance!
Re: Syntax error in Access 2007 query
you need the parameter in quotes, try
Code:
rs.Open "SELECT tblEmp.EM_ID, tblEmp.NAME FROM tblEmp where tblEmp.EM_ID = '" & Me.cboEmpId.Text & "' ;", cn, adOpenKeyset, adLockPessimistic
Re: Syntax error in Access 2007 query
Maybe, maybe not... depends on the datatype of EM_ID. That said...
1) you shouldn't use concatenated SQL like that... parameters are better.
2) If you insist on ignoring #1 above, at least put the SQL into a variable that you can print out to the debug, or msgbox, and see what the actual query is before running it... sometimes things aren't what we think them to be and are surprised by reality.
-tg
Re: Syntax error in Access 2007 query
You say "It's not working"
But you don't say what does happen.
Do get an empty recordset?
Do you get an error?
Does the tblEmp.EM_ID that you are using exist?
You have
Exit Sub
Set rs = Nothing
Set rs = Nothing will never be executed because you exit the sub beforehand.