Giving a Constant a Value

crhodus

Registered User.
Local time
Today, 14:41
Joined
Mar 16, 2001
Messages
257
Is it possible to pass the value that is in a variable into a constant? This is what I'm trying to do, but I don't know if I'm leaving out something in the code, or if this is illegal.

Input_User_Name = InputBox("Please enter your name.")

Const UName = Input_User_Name

Thanks!
 
I think that what you want to do is to keep your value as a variable, not a constant. A constant can never change:
"By declaring a constant, you can assign a meaningful name to a value. You use the Const statement to declare a constant and set its value. After a constant is declared, it cannot be modified or assigned a new value." - from Access help

By definition, a value from an Input box is going to be different from run to run.
 
The reason why I am trying to do this is because the value that is entered into my variable will not stay there. Each time a different form is opened, the value will not pass to it. I thought this might keep the value in the variable while the program is in use.
 
I have the following code in a module:

Option Compare Database
Option Explicit

Public Function Input_User_Name()
Input_User_Name = InputBox("Please enter your name.")

End Function

The value of Input_User_Name is passed into a field on each form in a field called TEMP_USER in the Before Update property of each form. The code for this is:

Private Sub Form_BeforeUpdate(Cancel As Integer)
Me.LAST_UPDATED = Date
Me.TEMP_USER = Input_User_Name
End Sub

Anytime data is changed on a form, the user has to enter his/her name. I would like for the user to only have to enter their name when the program is first launched. I tried calling the funcion in the On Open and On Load property of my main form, but the value is not passed to the next form that is opened. Instead, the user is prompted to enter their name when they make a change.

Can anyone tell me what I'm missing in my code?

Thanks!
 
Define a public variable strInput_User_Name:

Public strInput_User_Name as String

Set it to the value that the user enters, and then use it where ever you need it in the application.
 
if you want a public variable that will be accessible from all forms, the best way is to declare it in a standard module (in the modules tab of your database rather than in the class module attached to a form)

Public variables decared in the class module of a form are public to that form only (i.e. all subs and functions in that form can see them).

Also, you should be aware that if the program breaks for degugging, the value of the public variable may be lost; many people find it a better option to have a hidden form that remains open all of the time and use a text box on that form to store the values.

Mike
 
Thanks for everyones suggestions. I finally got it to work by declaring a public variable.

BTW can anyone suggest a good VBA book that I can use to help teach myself?
 

Users who are viewing this thread

Back
Top Bottom