INSERT INTO Syntax Error (1 Viewer)

Learn2010

Registered User.
Local time
Today, 15:51
Joined
Sep 15, 2010
Messages
415
Can anyone tell me why this doesn't work?

DoCmd.RunSQL ("INSERT INTO tblLog ( UserID, LoginDate, LoginTime, DB") _
& "SELECT tblLogin.UserID, tblLogin.LoginDate, tblLogin.LoginTime, 'SOL' AS 'DB'" _
& "FROM tblLogin"

Thank you.
 

isladogs

MVP / VIP
Local time
Today, 20:51
Joined
Jan 14, 2017
Messages
18,209
Add spaces at the start of each line,
Remove left bracket after RunSQL, move the quotes after DB at end of line 1 and remove the single quotes around DB in line 2

Apart from all that .... I think its OK!

Code:
DoCmd.RunSQL "INSERT INTO tblLog ( UserID, LoginDate, LoginTime, DB )" & _
       " SELECT tblLogin.UserID, tblLogin.LoginDate, tblLogin.LoginTime, 'SOL' AS DB" & _
       " FROM tblLogin"
 

Learn2010

Registered User.
Local time
Today, 15:51
Joined
Sep 15, 2010
Messages
415
That worked. Thanks very much.
 

Gasman

Enthusiastic Amateur
Local time
Today, 20:51
Joined
Sep 21, 2011
Messages
14,217
If you build the sql to a string variable, you can then Debug.Print it.

That is the way I tend to find a lot of my errors in SQL.

Code:
Dim strSQL as String
strSQL = "INSERT INTO tblLog ( UserID, LoginDate, LoginTime, DB") _
& "SELECT tblLogin.UserID, tblLogin.LoginDate, tblLogin.LoginTime, 'SOL' AS 'DB'" _
& "FROM tblLogin"
Debug.Print strSQL

DoCmd.RunSQL  strSQL

Can anyone tell me why this doesn't work?

DoCmd.RunSQL ("INSERT INTO tblLog ( UserID, LoginDate, LoginTime, DB") _
& "SELECT tblLogin.UserID, tblLogin.LoginDate, tblLogin.LoginTime, 'SOL' AS 'DB'" _
& "FROM tblLogin"

Thank you.
 

Users who are viewing this thread

Top Bottom