Results 1 to 4 of 4

Thread: Syntax error in Access 2007 query

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    8

    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.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    New Member
    Join Date
    May 2014
    Posts
    6

    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
  •  



Click Here to Expand Forum to Full Width