UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
> Global Declaring Issue, Access 2013    
 
   
gint32
post Apr 4 2019, 09:24 PM
Post#1



Posts: 368
Joined: 8-May 09
From: Australia


Seems like a silly question, so don't laugh I am trying to make a variable global which seems straight enough to most

I want the logged on user to remain the logged on user throughout the entire "ACCDB" or ACCDR, so I have read from heaps of sources that you just need to call it

so, I have tried inserting the below declaring but I get error when compiling and have no idea why!
CODE
Public loggedonuser As String = Environ("UserName")'''

or
CODE
Constant loggedonuser = Environ("UserName")


So my question ..is : where does the above need to reside (meaning where can I place this) so its available everywhere throughout the DB, or do I have to copy this into every form at the top of the code within "Microsofts Access Class Headers" or does it have to go into each header within each "Module", surely and hopefuly not and theirs a far simpler way that I can just insert it in one place.

Currently, I have to keep calling Call a Mod that I created from the code within each form. which I think is just not right!

thanks for reading smile.gif
This post has been edited by gint32: Apr 4 2019, 09:25 PM
Go to the top of the page
 
Start new topic
Replies
sumosoftware
post Apr 4 2019, 09:41 PM
Post#2



Posts: 33
Joined: 3-April 19



This is a basic example, please edit as required.

Global gUserName As String

Public Function setGlobalUserName()
gUserName = Environ("UserName")
End Function

You can place this in a separate module, maybe call it GlobalUtils and use where required.

This will allow you to access gUserName from anywhere, for example textbox0.value = gUserName, or perhaps make a function that returns gUserName which can also be used in other MS Access Objects.

Public Function getGlobalUserName()
getGlobalUserName = gUserName
End Function

Apologies for beginning method names with lower case, today I'm in javascript land.


Cheers.
This post has been edited by sumosoftware: Apr 4 2019, 09:48 PM
Go to the top of the page
 
gint32
post Apr 4 2019, 09:43 PM
Post#3



Posts: 368
Joined: 8-May 09
From: Australia


great where does it go to make avail to all forms..thanks
This post has been edited by gint32: Apr 4 2019, 09:43 PM
Go to the top of the page
 
sumosoftware
post Apr 4 2019, 09:49 PM
Post#4



Posts: 33
Joined: 3-April 19



just pop the code in a new code module, I like to place global declarations in a separate module just to be organized.
Go to the top of the page
 
gint32
post Apr 4 2019, 10:25 PM
Post#5



Posts: 368
Joined: 8-May 09
From: Australia


So back to my OP, do I have to call this from each form? or like I asked can this be placed just once and set at login or on opening of the accdb and remain a constant somehow.thanks
This post has been edited by gint32: Apr 4 2019, 10:27 PM
Go to the top of the page
 
sumosoftware
post Apr 4 2019, 10:33 PM
Post#6



Posts: 33
Joined: 3-April 19



Yep set gUserName once and this works across the whole app.

1. Set gUserName on login form or main form load event.

2. On another form load event, you could set a text box value. eg txtUserName = gUserName.

I hope that makes sense.

cheers.
This post has been edited by sumosoftware: Apr 4 2019, 10:37 PM
Go to the top of the page
 
gint32
post Apr 4 2019, 10:36 PM
Post#7



Posts: 368
Joined: 8-May 09
From: Australia


Will do and thanks, for the quick response.
Go to the top of the page
 
gint32
post Apr 4 2019, 11:06 PM
Post#8



Posts: 368
Joined: 8-May 09
From: Australia


Sorry to be a pest, but after inserting as you suggested into a new module, I now get an error "ambiguous name detected" everywhere I use the variable such as the following:
CODE
MsgBox " User, " & loggedonuser & " Hey" , vbCritical, "SECURITY BREACH! Attempted by " & loggedonuser

This post has been edited by gint32: Apr 4 2019, 11:10 PM
Go to the top of the page
 
MadPiet
post Apr 4 2019, 11:13 PM
Post#9



Posts: 3,704
Joined: 27-February 09



Did you declare it somewhere else too? A global variable is visible EVERYWHERE in your application, so if you have another variable with the same name somewhere in your code, the compiler would flag it as an error. I suspect that you tried to declare the variable somewhere else and forgot to remove that code. I would open a code module and then search all the modules for that variable name. If you find that the variable has been declared elsewhere, delete the other instances of it.
Go to the top of the page
 
gint32
post Apr 4 2019, 11:27 PM
Post#10



Posts: 368
Joined: 8-May 09
From: Australia


I done a find and replace for all instances of :

CODE
loggedonuser = Environ("UserName")

and replaced with
CODE
' loggedonuser = Environ("UserName")

and same with
CODE
dim loggedonuser as string

with
CODE
'dim loggedonuser as string


I still get the same error message, any other reasons this maybe happening

Go to the top of the page
 
sumosoftware
post Apr 5 2019, 12:46 AM
Post#11



Posts: 33
Joined: 3-April 19



Try from the vba window menu clicking Debug > Compile. This will usually tell you where you have code issues throughout your vba.

I'm sure you'll work it out, good luck.
Go to the top of the page
 
gint32
post Apr 5 2019, 01:07 AM
Post#12



Posts: 368
Joined: 8-May 09
From: Australia


QUOTE
try VBA Compile.

thanks this is what I have done and it stops at and highlights "loggedonuser" on the line below : with ambiguous error
CODE
MsgBox " User, " & loggedonuser & " Hey" , vbCritical, "SECURITY BREACH! Attempted by " & loggedonuser
Go to the top of the page
 

Posts in this topic
- gint32   Global Declaring Issue   Apr 4 2019, 09:24 PM
- - sumosoftware   This is a basic example, please edit as required. ...   Apr 4 2019, 09:41 PM
|- - gint32   great where does it go to make avail to all forms....   Apr 4 2019, 09:43 PM
|- - sumosoftware   just pop the code in a new code module, I like to ...   Apr 4 2019, 09:49 PM
|- - gint32   So back to my OP, do I have to call this from each...   Apr 4 2019, 10:25 PM
|- - sumosoftware   Yep set gUserName once and this works across the w...   Apr 4 2019, 10:33 PM
||- - gint32   Will do and thanks, for the quick response.   Apr 4 2019, 10:36 PM
||- - gint32   Sorry to be a pest, but after inserting as you sug...   Apr 4 2019, 11:06 PM
||- - MadPiet   Did you declare it somewhere else too? A global va...   Apr 4 2019, 11:13 PM
||- - gint32   thanks very much for your help, I do a search and ...   Apr 4 2019, 11:18 PM
||- - gint32   I done a find and replace for all instances of : ...   Apr 4 2019, 11:27 PM
||- - sumosoftware   Try from the vba window menu clicking Debug > C...   Apr 5 2019, 12:46 AM
||- - gint32   QUOTE try VBA Compile. thanks this is what I have ...   Apr 5 2019, 01:07 AM
||- - isladogs   Suggest you make a backup then do a global search ...   Apr 5 2019, 04:05 AM
||- - AlbertKallal   Ok, Your global declare is to be placed in a sta...   Apr 5 2019, 10:54 AM
||- - gint32   QUOTE Your global declare is to be placed in a sta...   Apr 5 2019, 06:35 PM
|- - theDBguy   Hi. Pardon me for jumping in but if you’re using E...   Apr 4 2019, 10:36 PM
|- - sumosoftware   very true theDBguy as the username is very simple ...   Apr 4 2019, 10:56 PM
|- - gint32   QUOTE re-dbguy.. using Environ(“Username”), You ...   Apr 5 2019, 12:05 AM
- - TenaciousCoder   I just skim-read this thread and I’m blown away wi...   Apr 7 2019, 05:09 PM
- - theDBguy   Hi Tenecious. Welcome back to the forum! Thank...   Apr 7 2019, 05:26 PM



Custom Search


RSSSearch   Top   Lo-Fi    27th May 2020 - 08:07 AM