Display a public variable value in a text box....

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,
I'm working with a login form (it saves the username in a public variable
after verifying password), after that the form is closed. I'm trying to
display the public variable in a text box on the main menu, so when is always
displaying which user had login...

Any ideas? any help will be greatly appreciated

gaba :)
 
Gaba,

Public variables can only be referenced in code. To retrieve a public
variable's value in a form, report, query etc you need to write a
function to return its value, and call that from your form etc. For
instance, assuming your public variable is called MyVariable, write a
function in a general module like:

Function GetMyVariable()
GetMyVariable = MyVariable
End Function

Then you can call the function from anywhere in your project. So, to
display its value in a textbox on a form, you would put this in the
textbox's control source:

=GetMyVariable()

That said, that's all theory, but it's not good practice, simply because
public variables are reset as soon as an untrapped error occurs in
your code. Therefore it is safer to use controls on forms to store
values, which do not suffer from this problem. In your particular case,
you would go the other way around, i.e. once the user provides their
username you store it directly in the textbox (or label) on the form
(which, I undertand, remains open at all times anyway), and read it from
there whenever required.
Generally, one can use hidden controls on a form to store values so they
are not seen, or, in the absense of a switchboard or other form that
remains open at all times, one can make a form just for the purpose,
open it at startup and keep it open but hidden as long as the database
is open.

HTH,
Nikos
 
Nikos,

I support your position on Public variables. There is another way to
accomplish this that is more native to Access and avoids having to keep track
of it at all. That is to create an application level property to hold the
user name and load the value after the user has logged in. that way, it is
always accessable. I use this in an application I have where the client
wanted a security module other than Access. It creates two properties,
UserName, and UserLevel, so I know what the user is allowed to do.
Unfortunately, I don't have the code with me. It is at home. If you need it,
post back and I can bring it in tomorrow.
 
Klatuu,

This sounds very interesting, and I would certainly like to have a look
at it (at your convenience) if you don't mind!

Regards,
Nikos
 
Nikos,

I would be happy to share it with you. Hopefully, it will not be too big
for a single post. Just to give you an overview, There is a user table that
has the user ID, user name, user level, and password. I use an encryption
routine so the information cannot be viewed even looking directly in the
table.
When a user logs in, the two properties are created. Then, a have a
function that is called from the open event of each form that determines,
based on the user's level, which contorls are enabled and which are locked.

I may have it here at work, I'll have to see if I can find it and get it
posted back to this thread.
 
Nikos,
This is the code we discussed regarding setting an Application level
property. You call SetSecurityProp and if the properties
don't exist, it creates them. I also included ChangeProperty that allows
changing a property. It works pretty much the same way.

Public Function SetSecurityProp(UserInitials As String, SecurityLevel As
Integer) As Boolean
Dim prp As Property
Const conPropNotFound As Integer = 3270

On Error GoTo ErrorSetSecurityProp
' Explicitly refer to Properties collection.
CurrentDb.Properties("User") = UserInitials
CurrentDb.Properties.Refresh
CurrentDb.Properties("SecurityLevel") = SecurityLevel
CurrentDb.Properties.Refresh
SetSecurityProp = True

ExitSetSecurityProp:
Exit Function

ErrorSetSecurityProp:
If Err = conPropNotFound Then

' Create property, denote type, and set initial value.
Set prp = CurrentDb.CreateProperty("User", dbText, UserInitials)
' Append Property object to Properties collection.
CurrentDb.Properties.Append prp
CurrentDb.Properties.Refresh
Set prp = CurrentDb.CreateProperty("SecurityLevel", dbInteger,
SecurityLevel)
' Append Property object to Properties collection.
CurrentDb.Properties.Append prp
CurrentDb.Properties.Refresh
SetSecurityProp = True
Resume ExitSetSecurityProp
Else
MsgBox Err & ": " & vbCrLf & Err.DESCRIPTION
SetSecurityProp = False
Resume ExitSetSecurityProp
End If

End Function

Function ChangeProperty(strPropName As String, varPropType As Variant,
varPropValue As Variant) As Integer
Dim dbs As Object, prp As Variant
Const conPropNotFoundError = 3270

Set dbs = CurrentDb
On Error GoTo Change_Err
dbs.Properties(strPropName) = varPropValue
ChangeProperty = True

Change_Bye:
Exit Function

Change_Err:
If Err = conPropNotFoundError Then ' Property not found.
Set prp = dbs.CreateProperty(strPropName, _
varPropType, varPropValue)
dbs.Properties.Append prp
Resume Next
Else
' Unknown error.
ChangeProperty = False
Resume Change_Bye
End If
End Function
 
Back
Top