Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Public Or Global Variable HELP!! Out Of My Mind!! 1

Status
Not open for further replies.

hiker0310

MIS
Apr 3, 2002
14
US
I have a form that acts as my login screen. I need to set the values entered as username and password to as variable that will be accessable in other forms. I would like to use the variable names fsUserName and fsPassword. Can anyone Please help me.

Currently, after the Username and Password are entered the form is hidden but kept open. I would perfere to set these values in a stored value in memory.

Please be very specific in any instruction. I have read every post regarding Global variables and Public Variables and I still do not understand this. Thanks for your help.
 
Hi

Not sure which bit you do not understand.

First to answer your specific question, define your variables in a code module, in the general section, as PUBLIC, and they will be available to all forms, reports, modules of your application.

Now to the general stuff.

The Public/Private thing is all about the scope of the varaible, ie where it can be 'seen', generally, unless you have a VERY good reason you should make your varaibles as 'privat' as possible, since in this way you reduce the chance of inadvertly screwing them up.

So if you dim a variable within a function, it is only visible within that function, if you dim a variable in the genearl section of a form module, it is visible within the form, and so on, you need to read up in help, or a VBA book to get the full drift, but in essence the above is what it is all about.

Hope that helps
Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
UK
kenneth.reaySPAMNOT@talk21.com
remove SPAMNOT to use
 

To create a global constant in a module. You can refer to this USERNAME constant from anywhere in your application. This design however limits the number of usernames to those for which you have constants created. As this is a global it violates some very good design standards about only using a variable constant with the smallest scope.

Public Const MYUSERNAME As String = "MyUsername"

On the other hand you could create a function to store constants with a local scope as in the following. THIS DESIGN only works for a single concurrent user because we store a single variable.

'Create a module level variable
Dim fsUsername As String
Dim fsPassword As String

Public Function IsValidUser(strUsername As String, strPassword As String) As Boolean

Const USR1 As String = "JOE"
Const USR2 As String = "FRED"
Const USR3 As String = "STEVE"
Const PWD1 As String = "JOEPW"
Const PWD2 As String = "FREDPW"
Const PWD3 As String = "STEVEPW"

If strUsername = USR1 _
Or strUsername = USR2 _
Or strUsername = USR3 Then
IsValidUsername = True
End If

If strUsername = PWD1 _
Or strUsername = PWD2 _
Or strUsername = PWD3 Then
IsValidUsername = True
Else
IsValidUsername = False
End If

End Function
Public GetUsername() As String
GetUsername = fsUsername
End Function
Public GetPassword() As String
GetPassword = fsPassword
End Function
----------------------
scking@arinc.com
Life is filled with lessons.
We are responsible for the
results of the quizzes.
-----------------------
 
I have a form that initially ask for the username and password. So when the user enters their information I want it to be stored in a variable. This will allow me to verify the accesws permissions for each form my validating the fnUserName variable against a certain set of criteria. If some one could look at this code and tell me what I may be doing wrong, That would be great.

I have created a module called security

Option Compare Database
Dim fnUserName As String

Public Function fnUserName() As String
fnUserName = Forms![LogonVerification]![UserName]
End Function

My question is this.

First: Is the code correct?
Second: How do I call this variable to use in a form text box or in a query or for use on a report? Can this even be done? As long as the form LogonVerification is open the variable will work. But this defeats my entire purpose for writing the function. I want to close this form and store the UserName information in a variable. Please Help, I am going insane!! Thanks.
 

Module-------------
Option Compare Database
Dim m_UserName As String

Public Sub SetUserName(MyUsername As String)
m_UserName = MyUsername
End Function

Public Function GetUserName() As String
GetUserName = m_UserName
End Function

In LogonVerification Form-----------
Sub Username_AfterUpdate
SetUserName(Me.Username)
End Sub

You can then user the GetUsername function to return the fnUsername module level variable from anywhere in the application without he LogonVerification form being open. This limits the scope to a module but you could also store it in a class. ----------------------
scking@arinc.com
Life is filled with lessons.
We are responsible for the
results of the quizzes.
-----------------------
 
I created a module called Security this is what is in the module:

Option Compare Database
Dim m_UserName As String

Public Sub SetUserName(MyUsername As String)
m_UserName = MyUsername
End Function

Public Function GetUserName() As String
GetUserName = m_UserName
End Function

----------------------------------------

I then Put this code in the form code

Sub Username_AfterUpdate
SetUserName(Me.Username)
End Sub

When I reference GetUserName() In any unbound field I get a blank field. Does any one know why?
Please Help.
 
Something else you might want to look into is using registry values to store the current logged on user. The password you could technically store there as well, in some encrypted form I would hope.

I'm not really sure why you need to keep the password of the logged on user in a variable, since you should be able to reference the password when you have the username.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top