-
Dec 3rd, 2017, 01:10 AM
#1
Thread Starter
New Member
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!
Last edited by sanjit.sam; Dec 3rd, 2017 at 01:19 AM.
-
Dec 3rd, 2017, 06:08 AM
#2
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Dec 4th, 2017, 04:29 PM
#3
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
-
Dec 5th, 2017, 03:50 AM
#4
New Member
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|