Environ("ComputerName") (1 Viewer)

Malcolm17

Member
Local time
Today, 12:53
Joined
Jun 11, 2018
Messages
107
Hey All,

Can Environ('ComputerName') go in to a SQL Statement?

I'm looking to do:

Code:
DoCmd.RunSQL "INSERT INTO tbl_Net_UserLog(UserName, Action, Computer) SELECT Forms!frmBackground!txtUserName, 'User Logged Off', Environ('ComputerName')"

Thanks,

Malcolm
 

Gasman

Enthusiastic Amateur
Local time
Today, 12:53
Joined
Sep 21, 2011
Messages
14,425
I believe you would need to concatenate the controls/variables into the sql string.
 

jdraw

Super Moderator
Staff member
Local time
Today, 07:53
Joined
Jan 23, 2006
Messages
15,395
Try this code:
Code:
' ----------------------------------------------------------------
' Procedure Name: ListEnvironValues
' Purpose: Routine to list the Environ intrinsic values
' Procedure Kind: Sub
' Procedure Access: Public
' Author: Jack
' Date: 08-Mar-21
' ----------------------------------------------------------------
Sub ListEnvironValues()
Dim i As Integer
On Error Resume Next
For i = 0 To 50
    Debug.Print i, Environ(i)
Next i
End Sub

I use this

Code:
Function BasePath() As String
    Dim p
    p = Environ("LOCALAPPDATA")
    BasePath = p
End Function
 

Malcolm17

Member
Local time
Today, 12:53
Joined
Jun 11, 2018
Messages
107
Thank you, (I'm not really sure what you meant by that)

I did a wee workaround:

On my Background form I created a textbox called txtComputer and On Load of that form I added the line
Code:
Me.txtComputer.Value = Environ("ComputerName")

from there just added the link to the textbox, so I ended up with

Code:
DoCmd.RunSQL "INSERT INTO tbl_Net_UserLog(UserName, Action, Computer) SELECT Forms!frmBackground!txtUserName, 'User Logged Off', Forms!frmBackground!txtComputer"

I'm not sure how professional this was, but it did the trick for me :)

Thank you,

Malcolm
 

jdraw

Super Moderator
Staff member
Local time
Today, 07:53
Joined
Jan 23, 2006
Messages
15,395
(I'm not really sure what you meant by that)

My intent was to show
-how to get a list of all the Environ values, and
- how to use an Environ value in vba

Glad you have it sorted out.
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 07:53
Joined
Feb 19, 2002
Messages
43,457
No, you cannot put this into SQL. SQL does not support Access functions. You can probably create a UDF that calls the function and returns the variable and call the UDF in the SQL statement.
 

isladogs

MVP / VIP
Local time
Today, 12:53
Joined
Jan 14, 2017
Messages
18,261
As Pat has suggested, this is possible using a user defined function (UDF). Similarly for user name:

Code:
Function GetComputerName()
    GetComputerName = Environ("ComputerName")
End Function

Function GetUserName()
    GetUserName = Environ("UserName")
End Function

You can then use those functions in your query. For example

Code:
SELECT *, GetComputerName() AS ComputerName, GetUserName() AS UserName, Now() AS CurrentDateTime
FROM YourTableName;

If preferred you can use TempVars instead of UDFs in your query
 

Users who are viewing this thread

Top Bottom