Setting Environ("username") = to a Value in a table

ashleybyrdnc

Registered User.
Local time
Today, 10:18
Joined
Sep 27, 2006
Messages
17
Sorry you guys, I am new the VB in Access and have spent about 30 min searching for this and can't figure it out.

I need to Pull the User ID or the Network ID, which I have done useing Environ("username") and make sure that ID is in a table call associates and then pull the relationships with that ID into a form (Which is linked to the UserID). I can get the NetworkID to come up in a Msg Box, but for the life of me I can't figure out how to set it equal to the associates tables userID. And then be able to pull the information needed

HELP
 
So you have a function called username()?
If that is the case, then just add a textbox to the form, where the control is set to =username()
If you are saying you have a table that contains the users real name in one field, and there network id in another field, then you will need to write a small function to return their real name.
try something along the lines of

Public Function RealUserName()

RealUserName = CurrentDb.OpenRecordset("select [usys_tbl_users].[realusername] from [usys_tbl_users] where [usys_tbl_users].[logonid] = '" & UserName() & "'").Fields("realusername")

End Function

You will need to substitute [usys_tbl_users] with the name of your table containing data, [realusername] / field("realusername") for the name of the field that contains the users real name, and [logonid] for the name of the field that contains the users network id. Also, if your function above is not called username() you will have to replace the name of this too.

You can then use the function realusername() in a form the same way as username().
 
Last edited:
Environ("Username"

Sorry I may have misworded my question the only way I have figured out to find the Network ID is through this

Private Sub Form_Open(Cancel As Integer)

MsgBox "Network Name = " & Environ("username")

End Sub

I need to set the value of Environ("username") equal to the AssociatesTable ID. In a query I am useing. So that I can pull the associates information from there.
 
I find this confusing

I need to set the value of Environ("username") equal to the AssociatesTable ID
.

surely what you want is to lookup the Associates TableID = Environ("username")

Just put =Environ("username") in the criteria.

Brian
 
To get the network id of the user, use the following code in a module:

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

' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
Function UserName() As String
' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If (lngX > 0) Then
UserName = Left$(strUserName, lngLen - 1)
Else
UserName = vbNullString
End If
End Function
 
Doesn't work

Everything I have tried doesn't work. Would someone please take a look at my database and see what I am doing wrong.

When you open Form Label, I want it to pull the network's username and then return that persons first and last name from the Associates Table

PLEASE HELP!!!
 

Attachments

The form needs to have a query as its record source, in the attached database I have added data inorder to test on my PC, query is qryUsername.

Brian
 

Attachments

Environ("Username") gives error

Brian, I opened your database and it gives me the error
"undefined function 'Environ' in expression.

Which is the same error I get every time I try this or to run any other function. I am useing Access 2000, does any one have an idea why this is happening?
 
My guess is that you have a library reference missing, but my knowledge is too sketchy to help. I would create a new post stating the facts as you have said, ie error you get when trying to use Environ and other functions, hopefully this will attract a response where it may not here.

Best of luck

Brian
 
Using Access 2000

Nope I am using Access 2000, I had seen that 2003 had problems with Environ() I don't know what my problem is, But I think I have found a way around using it, its definately not the best way but I think it will work! thanks for your help.
 

Users who are viewing this thread

Back
Top Bottom