Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compile error in sub routine using "Environ" 1

Status
Not open for further replies.
Jul 8, 2001
29
I am trying to run a bit of vb code on the click of a YES/No object in MS Acccess 97. It runs perfectly on my machine but will not run on others. The whole purpose of the code is that when the box is ticked the code picks up the users login ID, matches the user ID to their name, puts the name in a text box and current date/time on the same line as the tick box then sets the locked property on all 3 fields on the same line to -1 (locked. On my machine this runs perfectly, on others I receive a compile error as follows: "Can't find Project or Library" on clicking {ENTER} the word "Environ" in my variable set statement is hi-lighted :- uservar = Environ("USERNAME").
There appears to be differences in the .ocx or DLL's registered on different machines.
Does anyone know which one control the "Environ" class?
 
Hi
Try this, go to one of the machines where the db is not working and go to any module window. Click on 'Tools' then 'Refernces' and look fo any line that is ticked and has the word 'Missing' next to it. Uncheck the box and try again. If this does'nt work then use this bit of API code to call the user login name and forget the environ function, this code works on any machine regardless of libraries.

Option Compare Database
Option Explicit

Declare Function WNetGetUserA Lib "mpr" (ByVal lpName As String, ByVal lpuser As String, lpnlength As Long) As Long

' Functon to get NT Login User Name

'lp = longpointer
'1 - working space
'2 - return string
'3 - length working area
' lib name defaults to dll, strings must be byval and fixed length

Public Function GetUserName() As String
Dim lngLength As Long
Dim strName As String * 255 'fixed length at 255
Dim strUser As String * 255
Dim lngResult As Long

lngLength = Len(strName)
lngResult = WNetGetUserA(strName, strUser, lngLength)

GetUserName = Left$(strUser, 10)
End Function


Good Luck
 
The possible reason for this is that you are running NT/2000/XP and your users are using Win95/98/Me? The USERNAME environment variable is not set by the earlier O/S versions.

Use Savil's code - it's the most reliable method. Beware that you may need to manually trim the returned string - API strings are terminated with Chr(0) and Access 2000 sometimes gets it wrong, returning too many characters.
 
Getting rid of the missing references seems to have solved the immediate problem of the compile error, but I will try the code you supplied , it seems like a more efficient way of logging the user details.

Thks for your help.

HS
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top