SQL Syntax Error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • damicomj
    New Member
    • Sep 2010
    • 34

    SQL Syntax Error

    Run-time errpr '3134':
    Sytanx error in INSERT INTO statement.

    I don't see what I am doing wrong. Thank you for helping in advance.

    Code:
    lblUsername.Caption = fOSUserName
    
    strTable = "tblUserLog"
    strColumns = "(Username, Time)"
    strValues = "('" & lblUsername.Caption & "', '" & Time() & "')"
    
    strSQL = "INSERT INTO " & strTable & " " & strColumns & " VALUES " & strValues & ";"
    
    DoCmd.RunSQL strSQL
  • julietbrown
    New Member
    • Jan 2010
    • 99

    #2
    Try this ...

    strSQL = "INSERT INTO tblUserLog (Username,Time) VALUES ('" & lblUsername.Cap tion & "'," & Time() & ")"

    The messy stuff about single and double quotes and strings inside sql strings is a source of huge confusion to me, too! I think making more substrings has made your Insert statement even more difficult to unravel.

    Comment

    • Mariostg
      Contributor
      • Sep 2010
      • 332

      #3
      Time is a reserved word in access. It is a function. So if you use it as a field Name, you must enclose it between square brackets. [Time]
      It would be much safer to use a different name to avoid confusion. LogTime... maybe.

      Comment

      • damicomj
        New Member
        • Sep 2010
        • 34

        #4
        Juilet,
        That didn't work either. There are 3 columns in the table: UserID, Username, Time. Does that change anything?

        Comment

        • damicomj
          New Member
          • Sep 2010
          • 34

          #5
          I changed Time to Time1 and now I am getting:

          Syntax error (missing operator) in query expression '1:31:20 PM'.


          Code:
          strSQL = "INSERT INTO tblUserLog (Username,Time1) VALUES (" & lblUsername.Caption & "," & Time() & ")"
          
          DoCmd.RunSQL strSQL

          Comment

          • Mariostg
            Contributor
            • Sep 2010
            • 332

            #6
            Looks like you lost the single quotes surrounding the Time() function and your label caption.
            You should have
            Code:
            strSQL = "INSERT INTO tblUserLog (Username,Time1) VALUES ('" & lblUsername.Caption & "','" & Time() & "')"

            Comment

            • julietbrown
              New Member
              • Jan 2010
              • 99

              #7
              The number of fields won't make any difference as long as the bit in brackets ... (Username,Time1 ) ... has the same number of fields in the same order as the stuff in the VALUES brackets, (and of course UserName and Time1 must be names of fields in the table)

              If Time() is a date/time function you could try

              strSQL = "INSERT INTO tblUserLog (Username,Time1 ) VALUES ('" & lblUsername.Cap tion & "',#" & Time() & "#)"

              It's getting late and I'm well into my bottle of wine: I'll look in the morning and definitely sort this one out if someone else hasn't overnight.

              Good luck!

              Comment

              • Oralloy
                Recognized Expert Contributor
                • Jun 2010
                • 988

                #8
                You might try:
                Code:
                  Dim strSQL$
                  strSQL = "INSERT " & vbCrLf & _
                           "  INTO tblUserLog (Username,Time1)" & vbCrLf & _
                           "  VALUES('" & lblUsername.Caption & "', " & _
                                     "#" & CDate(Time()) & "#)"
                
                DoCmd.RunSQL strSQL

                Comment

                Working...