Insert Into query

G

Guest

I have the following code. Most of it was copied from the knowledge base.
It gets the computername, langroup, username and logondomain. At the very
end of the code I want to insert the results into a table called tblUsers.
When the code is run it asks for the details of each field (excpet the date
and current db name).

Does anyone know how to insert the computername, langroup, username and
logondomain results into the tblUsers table?

Thanks,

Martin

Option Compare Database
Option Explicit

Type WKSTA_INFO_101
wki101_platform_id As Long
wki101_computername As Long
wki101_langroup As Long
wki101_ver_major As Long
wki101_ver_minor As Long
wki101_lanroot As Long
End Type

Type WKSTA_USER_INFO_1
wkui1_username As Long
wkui1_logon_domain As Long
wkui1_logon_server As Long
wkui1_oth_domains As Long
End Type

Declare Function WNetGetUser& Lib "Mpr" Alias "WNetGetUserA" _
(lpName As Any, ByVal lpUserName$, lpnLength&)
Declare Function NetWkstaGetInfo& Lib "Netapi32" _
(strServer As Any, ByVal lLevel&, pbBuffer As Any)
Declare Function NetWkstaUserGetInfo& Lib "Netapi32" _
(reserved As Any, ByVal lLevel&, pbBuffer As Any)
Declare Sub lstrcpyW Lib "kernel32" (dest As Any, ByVal src As Any)
Declare Sub lstrcpy Lib "kernel32" (dest As Any, ByVal src As Any)
Declare Sub RtlMoveMemory Lib "kernel32" _
(dest As Any, src As Any, ByVal size&)
Declare Function NetApiBufferFree& Lib "Netapi32" (ByVal buffer&)


Function GetWorkstationInfo()
Dim ret As Long, buffer(512) As Byte, i As Integer
Dim wk101 As WKSTA_INFO_101, pwk101 As Long
Dim wk1 As WKSTA_USER_INFO_1, pwk1 As Long
Dim cbusername As Long, Username As String
Dim computername As String, langroup As String, logondomain As String

' Clear all of the display values.
computername = "": langroup = "": Username = "": logondomain = ""

' Windows 95 or NT - call WNetGetUser to get the name of the user.
Username = Space(256)
cbusername = Len(Username)
ret = WNetGetUser(ByVal 0&, Username, cbusername)
If ret = 0 Then
' Success - strip off the null.
Username = Left(Username, InStr(Username, Chr(0)) - 1)
Else
Username = ""
End If

'NT only - call NetWkstaGetInfo to get computer name and lan group
ret = NetWkstaGetInfo(ByVal 0&, 101, pwk101)
RtlMoveMemory wk101, ByVal pwk101, Len(wk101)
lstrcpyW buffer(0), wk101.wki101_computername
' Get every other byte from Unicode string.
i = 0
Do While buffer(i) <> 0
computername = computername & Chr(buffer(i))
i = i + 2
Loop
lstrcpyW buffer(0), wk101.wki101_langroup
i = 0
Do While buffer(i) <> 0
langroup = langroup & Chr(buffer(i))
i = i + 2
Loop
ret = NetApiBufferFree(pwk101)

' NT only - call NetWkstaUserGetInfo.
ret = NetWkstaUserGetInfo(ByVal 0&, 1, pwk1)
RtlMoveMemory wk1, ByVal pwk1, Len(wk1)
lstrcpyW buffer(0), wk1.wkui1_logon_domain
i = 0
Do While buffer(i) <> 0
logondomain = logondomain & Chr(buffer(i))
i = i + 2
Loop
ret = NetApiBufferFree(pwk1)

Debug.Print computername, langroup, Username, logondomain

DoCmd.RunSQL "INSERT INTO tblUsers ([Username],[Date],[Server],[RBOS
Number],[Domain]) VALUES (Username,
now(),Application.CurrentDb.Name,computername,logondomain)"


End Function
 
R

Rick Brandt

Martin said:
I have the following code. Most of it was copied from the knowledge
base.
It gets the computername, langroup, username and logondomain. At the
very end of the code I want to insert the results into a table called
tblUsers. When the code is run it asks for the details of each field
(excpet the date and current db name).

Does anyone know how to insert the computername, langroup, username
and logondomain results into the tblUsers table?

When RunSQL is executed it sends the SQL string to the query engine which is
NOT able to evaluate your variables. You need to delimit the SQL string so
that it evaluates to the *values* in yout variables instead of the *names*
of your variables. (The below was not tested).

DoCmd.RunSQL "INSERT INTO tblUsers " & _
"([Username],[Date],[Server],[RBOSNumber],[Domain]) " & _
"VALUES ('" & Username & "', Now(), '" & _
Application.CurrentDb.Name & "', '" & _
computername & "', '" & logondomain & "')"

In case I've made a mistake with that above I would recommend commenting out
the RunSQL line, stuffing the SQL string into a string variable and then
putting a Debug.Print strVariableName line in your code. The goal is to see
in the debug window a properly constructed SQL string containing your
variable values. Once you see a good SQL statement in the debug window the
RunSQL line should work.
 

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


Top