Sorin Sorin - 1 month ago 21
SQL Question

SQL insert into Table issue

I've got a a user log for our access database; however, if the system crashes, it does not properly record the user logging out. So I'm trying to create a code to check to see if the user is listed as logged in prior to creating a record.

Here is my code:

Function LogOn()
Dim sUser As String
Dim sSQL As String

DoCmd.SetWarnings False
sUser = Environ("username")
sSQL = "INSERT INTO tblUserLog ( UserID )" _
& "SELECT '" & sUser & "' AS [User] & WHERE tblUserLog.UserID='" & sUser & "' AND tblUserLog.LogOn Is Null & From tblUserLog;"
DoCmd.RunSQL sSQL
DoCmd.SetWarnings True
End Function


I'm getting an error message of


"must have at least one destination table".


I do not get the error message if I remove the WHERE statement.

Thanks for your help.
Sorin

Answer

FROM clause is placed at wrong position. Try Updated Query

sSQL = "INSERT INTO tblUserLog ( UserID )" _
& "SELECT '" & sUser & "' AS [User] From tblUserLog WHERE tblUserLog.UserID='" & sUser & "' AND tblUserLog.LogOn Is Null ;"
                     -----------------^