syntax error !

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • compwizard
    New Member
    • Feb 2008
    • 12

    syntax error !

    Code:
    Dim rstremoveAs Recordset
    Set rstremove = Database.OpenRecordset("SELECT * FROM tblitemhire WHERE itemhireno = " & ListBox & ";")
    I am getting a syntax error (missing operator) in query expression 'itemhireno = '
    Would anyone know what this problem could be, ive been through it over and over again but the same problem remains

    Thanks
    Last edited by NeoPa; Feb 28 '08, 01:16 AM. Reason: Please use [CODE] tags
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Originally posted by compwizard
    Dim rstremoveAs Recordset
    Set rstremove = Database.OpenRe cordset("SELECT * FROM tblitemhire WHERE itemhireno = " & ListBox & ";")

    I am getting a syntax error (missing operator) in query expression 'itemhireno = '
    Would anyone know what this problem could be, ive been through it over and over again but the same problem remains

    Thanks
    [CODE=vb]
    Dim MyDB As DAO.Database, rstremove As DAO.Recordset

    Set MyDB = CurrentDb

    'If itemhireno is NUMERIC
    Set rstremove = MyDB.OpenRecord set("SELECT * FROM tblitemhire WHERE [itemhireno] = " & Me![<List Box Name>], dbOpenDynaset)

    'If itemhireno is TEXT
    Set rstremove = MyDB.OpenRecord set("SELECT * FROM tblitemhire WHERE [itemhireno] = '" & Me![<List Box Name>] & "'", dbOpenDynaset)

    Do While Not rstremove.EOF
    'processing of the Recordset goes here
    rstremove.MoveN ext
    Loop

    rstremove.Close : Set rstremove = Nothing[/CODE]

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32634

      #3
      There's no space between rstremove and As on line 1.

      Comment

      • Scott Price
        Recognized Expert Top Contributor
        • Jul 2007
        • 1384

        #4
        In addition, and to clarify: You need to fully qualify where the ListBox exists. In other words, looking closely at ADezii's code you'll see Me!<list box name>. This needs to reflect if the list box is on the form where you are running this code from, or if it is on another form.

        Me!... assumes the same form,

        Forms!<form name>!<list box name>... assumes a separate form.

        Regards,
        Scott

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32634

          #5
          ListBox is equivalent programatically to Me.ListBox (or even Me!ListBox) though. Many prefer to have the code be more explicit though, of course.

          Comment

          • Scott Price
            Recognized Expert Top Contributor
            • Jul 2007
            • 1384

            #6
            You're correct, NeoPa the two are equivalent, however for good programming practices...

            The problem with unqualified controls is the possibility of, say, a variable with the same name existing in the same sub procedure or form module. The unqualified code will point to the variable instead of the control, which can lead to undesirable and unpredictable results!

            As well, if the list box is on another form entirely, the code will not be able to find the specified list box, leading to a syntax error.

            Regards,
            Scott

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32634

              #7
              Originally posted by Scott Price
              You're correct, NeoPa the two are equivalent, however for good programming practices...
              Agreed.
              Originally posted by Scott Price
              The problem with unqualified controls is the possibility of, say, a variable with the same name existing in the same sub procedure or form module. The unqualified code will point to the variable instead of the control, which can lead to undesirable and unpredictable results!
              Agreed.
              Originally posted by Scott Price
              As well, if the list box is on another form entirely, the code will not be able to find the specified list box, leading to a syntax error.
              This doesn't pertain.
              Remember we are comparing[ListBox] with Me.[ListBox]. Clearly neither would be correct syntax for that eventuality.

              Comment

              Working...