Making variable availble throughout the Application DB (1 Viewer)

Gint3232

Registered User.
Local time
Tomorrow, 07:44
Joined
Jan 30, 2018
Messages
20
Hi Everyone!
I am struggling with how to make a variable available throughout my Ms Access Test.mdb
I am not that up on how to do this and have tried unsuccessfully to achieve this.
I know how to pass a variable form one sub to another but that is about it for my level of expertise , can anyone suggest how this is done
Reason is Every form I have needs to get the logged on users name, So I have found that I am calling the same function over and over with every form .How do you make it Public or Global or ???, the function I use is
Code:
Public Function GetCurrentUserName() As String

On Error GoTo Err_GetCurrentUserName
 Dim lpBuff As String * 25
 Dim ret As Long, Username As String
   ret = GetUserName(lpBuff, 25)
   Username = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)
   GetCurrentUserName = Username & ""
   
  

Exit_GetCurrentUserName:
    Exit Function

Err_GetCurrentUserName:
        MsgBox Err.Description
        Resume Exit_GetCurrentUserName
End Function
And at the moment I dim a variable (LoggedonUser)as string in every form
I have read googled and I just can't get my head around it for my tiny little brain, So if you can help with this please I be really happy
many Thanks
 

isladogs

MVP / VIP
Local time
Today, 23:44
Joined
Jan 14, 2017
Messages
18,186
Welcome to AWF. Your user name looks familiar from another forum.

You can create a single Public or Global variable in a standard module e.g. modDefinitions in place of all your current Dim statements which should then be deleted

Whilst there is disagreement amongst developers on their general use, this will do exactly what you want
 

Gint3232

Registered User.
Local time
Tomorrow, 07:44
Joined
Jan 30, 2018
Messages
20
You can create a single Public or Global variable in a standard module e.g. modDefinitions in place of all your current Dim statements which should then be deleted

Thanks , yeh you helped me out on another forum, Thanks..but I don't know what a modDefinitions is, I tried google but was useless...If you mean a module then which one as I have lots of modules or in all of them or ??
Can you tell me in Laymans terms (English) please !
 

isladogs

MVP / VIP
Local time
Today, 23:44
Joined
Jan 14, 2017
Messages
18,186
You could use the declarations section of any standard module.

However, I prefer to create a new module just for declaring Public variables so all are kept together and so are easy to find.

I call it modDefinitions but you can use any module name you want
 

Gint3232

Registered User.
Local time
Tomorrow, 07:44
Joined
Jan 30, 2018
Messages
20
Welcome to AWF. Your user name looks familiar from another forum.

You can create a single Public or Global variable in a standard module e.g. modDefinitions in place of all your current Dim statements which should then be deleted

Whilst there is disagreement amongst developers on their general use, this will do exactly what you want
Thanks, though I am not that sure how too
So I tried
with a sub
Code:
Loggedonuser = GetCurrentUserName()
and within a module any module
public Loggedonuser as string
So results =...inside the sub same as before and outside nothing happens out side it.
????
 

isladogs

MVP / VIP
Local time
Today, 23:44
Joined
Jan 14, 2017
Messages
18,186
Unsure what you did.
Can you upload a cut down copy of your database and tell me what to look at.
 

Gint3232

Registered User.
Local time
Tomorrow, 07:44
Joined
Jan 30, 2018
Messages
20
Ok I've created a simple example of what not works ..thanks
 

Attachments

  • PassLogonuser2 EveryForm.accdb
    828 KB · Views: 448

isladogs

MVP / VIP
Local time
Today, 23:44
Joined
Jan 14, 2017
Messages
18,186
Ok I've created a simple example of what not works ..thanks

And I've created a simple example that does work! :D

I've disabled lots of unnecessary code
I've also provided a much simplified GetCurrentUserName function

Forms 2 & 3 have the textbox bound to that function so the user name loaded automatically
Your timer event in Form 1 prevents that working so I've made the control unbound & set its value in Form_Open event

Your Form_Close event also contained a line to close the current form
That's both unnecessary as the event itself does that and will cause an error.
I've disabled that as well

HTH
 

Attachments

  • PassLogonuser2 EveryForm.zip
    50.7 KB · Views: 486

moke123

AWF VIP
Local time
Today, 19:44
Joined
Jan 11, 2013
Messages
3,852
pmfji, You could also assign the value to a TempVar, which has the advantage of not losing the value should an error occur.

Code:
TempVars.add "tvUser", GetCurrentUserName
 

isladogs

MVP / VIP
Local time
Today, 23:44
Joined
Jan 14, 2017
Messages
18,186
Hi moke

I know that's the theory but I've had instances where tempvars have lost their value.
I gave up using them as I found them unreliable
...but perhaps that's just me
 

Gint3232

Registered User.
Local time
Tomorrow, 07:44
Joined
Jan 30, 2018
Messages
20
And I've created a simple example that does work! :D

I've also provided a much simplified GetCurrentUserName function

Much obliged, for your updated Function, Hey! I don't think I've explained myself correctly. As I stated in my OP.. I wish to just have a variable that once set to Public (at say after initial login), I can/will use throughout the Test.dmb meaning no matter if I open and close forms left, right and center ,,,whenever within my VBA code, I can write/use "loggedOnUser? or debug.print LoggedOnUser and bang there it is my username. Rather than keep calling a function each time, I just wish to store or set the variable(loggedonuser) and thats that, So that the vba is not running off to a function every time to find out something that we already decided what it was, at logon we know and got the value so why the need to check again and again on each form the user navigates to and opens..
Hope that makes better sense.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 18:44
Joined
Feb 28, 2001
Messages
27,001
moke, I've long been a proponent of preserving stuff in the Public Variable / General Module situation. It isn't the error that kills the values. It is the failure to handle the error. Because the moment your "Last Chance" trap handler wakes up, it gives you limited options. The option that kills everything is the "Reset" action. That ends the ball game.

Of course, for quick & dirty databases, no biggie involved. For production databases, error handling behind the scenes is just as important as formatting and visual layout work on your desktop.
 

isladogs

MVP / VIP
Local time
Today, 23:44
Joined
Jan 14, 2017
Messages
18,186
There isn't a big issue about calling the function as and when needed

e.g. You could have a message box:
Code:
MsgBox "Hello " & GetCurrentUserName, vbExclamation, "Welcome"

BUT as explained originally you could create a module e.g. modDefinitions with this code

Code:
Option Compare Database
Option Explicit

Public LoggedOnUser As String

... add any other public variables in the same module

Then add 1 line of code in your startup form
Code:
LoggedOnUser=GetCurrentUserName

You can then refer to the variable throughout the database
Both methods are equally good. Choose whichever you prefer
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 18:44
Joined
Feb 28, 2001
Messages
27,001
Gint3232 said:
Rather than keep calling a function each time, I just wish to store or set the variable(loggedonuser) and thats that

If you create a general module and put your security code and values in it, then just call subroutines from other places, you can do what you said you wanted. I hope this makes sense to you.

Key point #1 - the values you want to retain and re-use in this general module must be declared Public. I.e. instead of "Dim LoggedOnUser As String" you use "Public LoggedOnUser As String" and you put that declaration at the top of the general module. If done correctly, ALL OTHER CODE can simply use the declared name without calling a function each time.

Key Point #2 - Both Class Modules (associated forms or reports) and General Modules (separate from any forms or reports) have declaration areas. The part between the very top of any module and the first function or subroutine definition is the Declaration Area and it is where you make all of the declarations that will be visible to your module's functions and subroutines but not defined inside the functions or subroutines themselves.

But no matter WHAT you declare, the declaration area of a Class module cannot be shared with anything outside of the form/report because that entity closes when done - and takes its declaration area with it. Whereas once you open general module for any purpose including calling a subroutine, its content remains available for the life of the session UNLESS you take a serious error that your code did not handle adequately.

You might wish to search this forum for "Error Handling" as a topic, and there was a robust discussion on the topic of "Global Variables" (vs. "TempVars") not that long ago. Your post count is low so you might not be familiar with this forum's abilities. There is a thin blue ribbon just below the top of the page where it shows you (on the left) where you are in the forum hierarchy and (on the right) who the forum page things you are. The third item from the right on the ribbon is Search and it can be VERY useful.
 

Gint3232

Registered User.
Local time
Tomorrow, 07:44
Joined
Jan 30, 2018
Messages
20
Public LoggedOnUser As String
My issue with the function was when stepping through my code I had to go through the Function line by line, but now that you supplied a much more simplified version its now not an issue, Thanks for all your help
 

Users who are viewing this thread

Top Bottom