Query parameters defined by variable instead of form (1 Viewer)

accessaspire219

Registered User.
Local time
Yesterday, 18:47
Joined
Jan 16, 2009
Messages
126
Hi,
I want to design a database that will pickup a windows username and store it to a variable. Later on I want to reference to this variable as the parameter in a parameter query. In other words, in the parameter queries I have built so far I have always had the user enter the parameter in a form and then referenced the form in the parameter query to get the parameter. What I am looking to do is to reference a variable instead of a form in the parameter query.
Any idea on how this could be done?
Thanks!
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 16:47
Joined
Aug 30, 2003
Messages
36,140
I typically store it in a hidden textbox on a form that remains open, and get it from there. If you want to store it in a VBA variable, you'd create a public function that returned the value of that variable, and call that from the criteria.
 

boblarson

Smeghead
Local time
Yesterday, 16:47
Joined
Jan 12, 2001
Messages
32,059
to use variables in a query you must wrap them in a function. So, for example, if you have a global variable for the user name:

mstrUser

you need to use a function

Code:
Public Function RetUser() As String
   RetUser = mstrUser
End Function

to get it in the query - or criteria:

RetUser()
 

boblarson

Smeghead
Local time
Yesterday, 16:47
Joined
Jan 12, 2001
Messages
32,059
I typically store it in a hidden textbox on a form that remains open, and get it from there. If you want to store it in a VBA variable, you'd create a public function that returned the value of that variable, and call that from the criteria.

I typed too slow on that one :rolleyes:
 

accessaspire219

Registered User.
Local time
Yesterday, 18:47
Joined
Jan 16, 2009
Messages
126
Thanks Pbaldy and boblarson. where should I define the function? I defined it in the form and put criteria as RetUser() in the query, this came up with a undefined function error when I tried to run the query. What am I doing wrong here?
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 16:47
Joined
Aug 30, 2003
Messages
36,140
To be run from a query, it should be a public function in a standard module (not behind a form).
 

accessaspire219

Registered User.
Local time
Yesterday, 18:47
Joined
Jan 16, 2009
Messages
126
What is a standard module? Can I just add a new module and put the function there?
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 16:47
Joined
Aug 30, 2003
Messages
36,140
Yes, that would be a standard module.
 

accessaspire219

Registered User.
Local time
Yesterday, 18:47
Joined
Jan 16, 2009
Messages
126
Ok, I made a new module and put the function in there. The function I have is
Code:
 Option Compare Database
Public Function GetUser()
GetUser = varName
End Function

Now do I just write GetUser() in the query criteria?
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 16:47
Joined
Aug 30, 2003
Messages
36,140
Yes; have you tried it?
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 16:47
Joined
Aug 30, 2003
Messages
36,140
Have you set a breakpoint or message box to ensure the function is returning what you expect? You obviously need to have populated the variable at some point prior to this.
 

accessaspire219

Registered User.
Local time
Yesterday, 18:47
Joined
Jan 16, 2009
Messages
126
Also, any idea on I can write a "Sub" so that it runs when the database is opened? For example, in the following code,
Code:
Private Sub Form_Load()
Dim strUser As String
Set WshNetwork = VBA.CreateObject("WScript.Network")
strUser = WshNetwork.UserName
[loginid] = strUser
Dim varName As String
varName = DLookup("SNAME", "WINDOWS_USERNAME_NAME", "[WINDOWSUSER]='" & Me.loginid.Value & "'")
[loginname] = varName
End Sub
the sub runs when the form loads. How can I write this sub so that it runs everytime the database is opened?
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 16:47
Joined
Aug 30, 2003
Messages
36,140
Either load that form on startup (Tools/Startup) or use an autoexec macro to open it.
 

Users who are viewing this thread

Top Bottom