I really have no experience with modules and code but i have tried to use code that others create. I am trying to use this code to enter in the username into a table when the user runs a particular macro. I was told that I have to convert this sub (?) into a function.
This was my original way of doing this by prompting the user to type in their name.
I now want to use this code:
I tried to call this from my query like this, but it does not work. I was told it has to be a function but I don't know how to create it as a function. Any help would be greatly appreciated.
Thanks so much. CB
This was my original way of doing this by prompting the user to type in their name.
Code:
INSERT INTO [EEC Uploads Log] ( MacroName, EndingTotal, EndTime, Auditor )
SELECT "Newhires" AS Upload, Count(EmpInf.EEID) AS CountOfEEID, Now() AS EndTime, [Specialist Name] AS Auditor
FROM EmpInf
GROUP BY "Newhires", Now(), [COLOR=red][Specialist Name], [/COLOR]EmpInf.FILECOMPLETE
HAVING (((EmpInf.FILECOMPLETE)=False));
I now want to use this code:
Code:
' Declare for call to mpr.dll.
Declare Function WNetGetUser Lib "mpr.dll" _
Alias "WNetGetUserA" (ByVal lpName As String, _
ByVal lpUserName As String, lpnLength As Long) As Long
Const NoError = 0 'The Function call was successful
Sub GetUserName()
' Buffer size for the return string.
Const lpnLength As Integer = 255
' Get return buffer space.
Dim status As Integer
' For getting user information.
Dim lpName, lpUserName As String
' Assign the buffer size constant to lpUserName.
lpUserName = Space$(lpnLength + 1)
' Get the log-on name of the person using product.
status = WNetGetUser(lpName, lpUserName, lpnLength)
' See whether error occurred.
If status = NoError Then
' This line removes the null character. Strings in C are null-
' terminated. Strings in Visual Basic are not null-terminated.
' The null character must be removed from the C strings to be used
' cleanly in Visual Basic.
lpUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1)
Else
' An error occurred.
MsgBox "Unable to get the name."
End
End If
' Display the name of the person logged on to the machine.
MsgBox "The person logged on this machine is: " & lpUserName
End Sub
I tried to call this from my query like this, but it does not work. I was told it has to be a function but I don't know how to create it as a function. Any help would be greatly appreciated.
Code:
INSERT INTO [EEC Uploads Log] ( MacroName, EndingTotal, EndTime, Auditor )
SELECT "Newhires" AS Upload, Count(EmpInf.EEID) AS CountOfEEID, Now() AS EndTime, GetUserName() AS Auditor
FROM EmpInf
GROUP BY "Newhires", Now(), GetUserName(), EmpInf.FILECOMPLETE
HAVING (((EmpInf.FILECOMPLETE)=False));
Thanks so much. CB