Tom W
Tom W

Reputation: 1

Runtime error when using Global Variable in MS Access

I have made a login page for my project, and want to keep track of which user is logged in so that if they edit a form, their username can be attached for future record.

I am trying to do this with a global variable in a standalone module:

Option Compare Database

Global gstrActiveUser As String

And When the user successfully logs in, I am trying to write the user's username to this global variable:

If Nz((DLookup("Password", "Users", "Login = '" & Me.txtUsername.Value & "'")), "GarbageEntry") = Me.txtPassword Then
    DoCmd.Close
    gstrActiveUser = Me.txtUsername '<-- Problem Line According to Debug
    DoCmd.OpenForm "Equipment Lookup"
    MsgBox gstrActiveUser
Else

But when I run this, I get "Run-time error '2467': The expression you netered refers to an object that is closed or doesn't exist."

Not sure what I'm doing wrong, I watched this video: https://www.youtube.com/watch?v=DyZ-H2gJXGc to see what he's doing different and I can't figure it out.

I've tried Me.txtUsername, Me.txtUsername.Value, I've tried just putting "Default User", which works, and I can retrieve it on other forms - but I just can't write to it correctly.

I'm still fairly new to MS Access and VBA so any feedback is much appreciated.

Upvotes: 0

Views: 47

Answers (1)

Tom W
Tom W

Reputation: 1

I realised my mistake upon reading over this - I do DoCmd.Close before gstrActiveUser = Me.txtUsername

DUHHH, ugh no wonder it wasn't working. Anyway, maybe this can help someone else.

Upvotes: 0

Related Questions