Full Version: Problem with variable being 'forgotten'
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
Alpha7
Hi,

I have a form sets the value of a string called 'CurrentUser'. This is declared in a separate module like so:
Public CurrentUser as String

Sometimes I need to refer to the CurrentUser string, but it seems to keep disappearing, i.e. reseting back to null. This only seems to happen when I am using DLookup to fetch some information from a table:
CODE
Private Sub AddRemove_Click()

If DLookup("UserAccess", "tblUsers", "[UserID]=" &  [color="red"]CurrentUser[/color] ) = "Admin" Then

DoCmd.OpenForm "frmUserManagement", acNormal

Else

MsgBox "You are not authorised to carry out user management functions.", vbExclamation, "Insufficient Authority"

End If

End Sub


It works once, but if the user click the button again, is doesn't work as the string is null. Why does running this code reset the string? I can't see anywhere that it is being redefined - just referred to.

Thanks for any help.
Jack Cowley
Try:

CODE
If DLookup("UserAccess", "tblUsers", "[UserID] = '" &  CurrentUser & "'" ) = "Admin" Then


hth,
Jack
cheekybuddha
Hi,

Also check no code is setting the value of CurrentUser between uses.

Also, any unhandled errors in your code will cause the variable to lose its value.

hth,

d
Alpha7
Thank you guys. I didn't have an error handler - this was resetting the string.

Cheers.
cheekybuddha
You're welcome,

I'm glad we could help.

d
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.