sql syntax error

G

Guest

I am getting a syntax error from the following;
Dim strSQL As String
Dim MyTime
MyTime = Time
strSQL = "INSERT INTO
tblWhoLoggedOn(UserName,UserPassword,Date,Time) " & _
" VALUES (" & Me.cboUserName & "," & Me.txtUserPassword &
",Now(),MyTime)"
CurrentDb.Execute strSQL, dbFailOnError

I am wanting to track those who sign into the db. When the error comes up,
it highlites the last line (CurrentDb...).
Can anyone see the mistake made in this?
Thanks in advance to anyone who responds.
*** John
 
G

Guest

Try putting single quotes around the string values you want to insert. Also,
SQL won't know the Now() function or MyTime var. They should be single-quoted
as well. Try this:

strSQL = "INSERT INTO tblWhoLoggedOn(UserName,UserPassword,Date,Time)
VALUES ('" & Me.cboUserName & "','" & Me.txtUserPassword & "','" & Now()
"','" & MyTime & "')"
 
D

Douglas J. Steele

Values being inserted into text fields need to be delimited with quotes,
values being inserted into date/time fields need to be delimited with #.


Dim strSQL As String
Dim MyTime
MyTime = Time
strSQL = "INSERT INTO
tblWhoLoggedOn(UserName,UserPassword,LogonDate,LogonTime) " & _
" VALUES (" & Chr$(34) & Me.cboUserName & Chr$(34) & "," &
Chr$(34) & Me.txtUserPassword & Chr$(34) & ", #" & Format(Now(), "yyyy-mm-dd
hh:nn:ss") & "#, #" & Format(MyTime, "hh:nn:ss") & "#)"
CurrentDb.Execute strSQL, dbFailOnError

I don't understand why you've got a separate Time field. The Now function
provides you with both date and time.

Also, note that I renamed your date and time fields (to LogonDate and
LogonTime). Date and Time are reserved words in Access, and should not be
used for your own fields (nor variables nor form controls, etc.)
 
G

Guest

Doug is correct when he says to delimit date fields with #. I misread the
title of your post, and thought you were working with SQL Server when I said
to use single quotes for dates.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Error 2465; Can't Find Field 5
Syntax Error 8
Syntax (Missing Operator) in Query Expr .... 2
Syntax error 14
Help with syntax error 1
Syntax error (missing operator) 4
SQL Syntax Error 2
SQL Syntax Error 6

Top