having difficulty with a global string (1 Viewer)

Cowboy_BeBa

Registered User.
Local time
Tomorrow, 07:48
Joined
Nov 30, 2010
Messages
188
hi all

I'm building a login page, its Modal so cannot be clicked away

the basic login function works well (compares username and password entered with that in a table and, if all values are equal, opens a main menu form) as the login form is modal i had to set its visibility to false so that the user may interact with the main menu, which is not modal

now the code for the login page is in a seperate module (i just had the login button on the login form call a function from the module), i figured doing this by doing this (and by declaring the username string with "Public uStr as String") would create a global variable that i could use on all other forms

This does not seem to be the case (im testing it by having a message box on the form load event of the main menu "msgbox(uStr)" which returns blank)

just wondering if anyone knows how i can get this to work?
 

RuralGuy

AWF VIP
Local time
Today, 17:48
Joined
Jul 2, 2005
Messages
13,826
Where in your Module did you put the "Public uStr as String"? It needs to be outside of any procedure at the top of the module.
 

Cowboy_BeBa

Registered User.
Local time
Tomorrow, 07:48
Joined
Nov 30, 2010
Messages
188
its at the very top of the module (well... right under Option Compare Database)

However it is only initialized and set in the function that compares the username and password and determines if they match (the function thats called when a user hits the login button), if that makes a difference
 

Cowboy_BeBa

Registered User.
Local time
Tomorrow, 07:48
Joined
Nov 30, 2010
Messages
188
problem solved, i just needed to make the function public, after that everything else worked
 

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 19:48
Joined
Oct 17, 2012
Messages
3,276
Actually, RuralGuy had it right in that if you declared the variable as Global (which is virtually the same as Public) at the top of a module, then it should be visible throughout the project AS LONG AS it was declared in a regular module and not a form or report. (Public variables in forms and reports become properties of that form/report in the same way that RecordSource and Visible are.)

What I always do is create a Globals module that contains nothing but comments, Public variables, and Public constants.

Also, for something like username or access rights, if I need to track it throughout the project, I personally tend to use TempVars. Globals would work as well 99% of the time, but I've personally run into several instances where a global was mysteriously cleared, whereas I've yet to have that happen with a Tempvar.
 

MarkK

bit cruncher
Local time
Today, 16:48
Joined
Mar 17, 2004
Messages
8,178
If you need to guarantee a global has a value, you can expose a global property. Consider a case like this where you might have a default value . . .

Code:
private v_test
private m_lastChanged as date

Public Property Get GlobalTest as string
   If IsEmpty(v_test) Then v_test = "Test is 1.01"
   GlobalTest = v_test
End Property
Public Property Let GlobalTest(sValue as string)
   If sValue <> v_test Then
      m_lastChanged = Now()
      v_test = sValue
   End If
End Property
. . . and notice that in the Property Let we keep track of when the value was last set. So if you your getting or setting should run code AND expose/receive a value, a Property Get or Let procedure is an excellent choice.
 

jdraw

Super Moderator
Staff member
Local time
Today, 19:48
Joined
Jan 23, 2006
Messages
15,364
Markk,

Can you show sample usage where we can see when the value was last set?

jdraw
 

Users who are viewing this thread

Top Bottom