Report Open Message

eMel

Registered User.
Local time
Today, 13:37
Joined
Jun 23, 2014
Messages
62
I'm trying to get a record written to a table showing when a table opens. I want to see Date/Time, UserName, "***Report has been opened." I'm having issue here:
Public Function LogEvt()
Dim SQL As String
SQL = "INSERT INTO AuditTrail ( DateTime, UserName, FormName ) SELECT #" & Now() & "#, fOSUserName(), '" & LogEvent & "';"
DoCmd.RunSQL SQL
End Function

I was getting errors with INSERT INTO. DoCmdRunSQL is highlighted in yellow, so I assume there is still an issue. SuggestionS?
 
Looks like you need to resolve the fOSUsername()

SQL = "INSERT INTO AuditTrail ( DateTime, UserName, FormName ) SELECT #" & Now() & "#,'" & fOSUserName() &'", '" & LogEvent & "';"

???
 
Didn't work.
 
Last edited:
DoCmd.RunSQL SQL is highlighted in yellow. Runtime 3134.
 
Ok. Is that the exact full error message you're seeing?
 
Run-time error '3134':
Syntax error in INSERT INTO statement
 
Print your SQL statement to the Immediate Window using Debug.Print and see the result (or paste it here).

To get to the Immediate Window go the VBA code editor > click Debug menu > click Immediate Window. That's where you need to look after running your code:
Code:
Debug.Print "INSERT INTO AuditTrail ( DateTime, UserName, FormName ) SELECT #" & Now() & "#,'" & fOSUserName() &'", '" & LogEvent & "';"
 
This is what Ken's code should have been.
Code:
SQL = "INSERT INTO AuditTrail ( DateTime, UserName, FormName ) SELECT #"  & Now() & "#,'" & fOSUserName() & "', '" & LogEvent  & "';"
The single quote after the fOSUserName is the trouble maker. Edit and see if it works. If not as vbaInet has requested, show the generated SQL.
 
Good spot there sir! I've been looking at my screen for too long today :)
 
Thank you for that one. That line looks ok. Now, I'm doing debug.print
on DoCmd.RunSQL; SQL. .RunSQL is highlighted in blue
 
You can't print a method (or command). You print only text.
 
http://www.access-programmers.co.uk/forums/showthread.php?t=266335

My desktop won't let me reply, so I'm having to do this ????? I'll reply with my phone.

Code:
Public Function LogEvt()
Dim SQL As String
    SQL = "INSERT INTO AuditTrail ( DateTime, UserName, FormName ) SELECT #" & Now() & ", fOSUserName(), '" & LogEvent & "';"
DoCmd.RunSQL SQL
End Function

Run-time error '3134':
Syntax error in INSERT INTO statement.
The RunSQL is highlighted in yellow.
 
Here's my full code.
Code:
Option Compare Database

Public LogEvent As String

Private Declare PtrSafe Function apiGetUserName Lib "advapi32.dll" Alias _
    "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function fOSUserName() As String
' Returns the network login name
Dim sName
Dim lngLen As Long, lngX As Long
Dim strUserName As String
Dim userID As String
    strUserName = String$(49, 0)
    lngLen = 50
    lngX = apiGetUserName(strUserName, lngLen)
    If (lngX > 0) Then
        fOSUserName = Left$(strUserName, lngLen - 1)
    Else
        fOSUserName = vbNullString
    End If

End Function
'******************** Code End **************************

Public Function LogEvt()
Dim SQL As String
    SQL = "INSERT INTO AuditTrail ( DateTime, UserName, FormName ) SELECT #" & Now() & ", '" &  fOSUserName() & "', '" & LogEvent & "';"
DoCmd.RunSQL SQL
End Function
DoCmd RunSQL SQL is highlighted in yellow , with the 3134 error.
 
Last edited:
I thought you said it was working? If you want to see the contents of the string SQL, Debug.Print the SQL variable.
 
Why do I get ads?
 
Last edited:
If you're a paid member you don't get ads (depending on your level of membership) plus you get some other added benefits.

Can we see what you say is the proper output?

Just replace that line of code with what pr2-eugin gave you. He fixed the syntax problem but your new code doesn't reflect any of that.
 

Users who are viewing this thread

Back
Top Bottom