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 Variable not working

Status
Not open for further replies.

lashwarj

IS-IT--Management
Nov 1, 2000
1,067
US
I have a public variable with the values being set on my loggin screen with the following code

SELECT user_setup
LOCAL myglobalclass
username = ALLTRIM(Thisform.User_name.Value)
userdepartment = ALLTRIM(Thisform.department.Value)


LOCATE FOR UPPER(user_setup.user_name) = UPPER(ALLTRIM(Thisform.User_name.Value))
IF FOUND() AND ALLTRIM(user_setup.password) = ALLTRIM(Thisform.password.Value)
Thisform.Release()
DO FORM current_user

ELSE
MESSAGEBOX("The User Name or Password is incorrect.","Please try again.")
Thisform.password.Value = ""
Thisform.user_name.SETFOCUS
ENDIF



In my main prg I declare the values

oGlobal = CREATEOBJECT('myGlobalClass')
Public myGlobalClass
DEFINE CLASS myGlobalClass AS CUSTOM
username =""
userdepartment =""
ENDDEFINE


When the current_user form loads if I click the button on the form for change password i have to first click in the current_user field to retrieve the value. For some reason it shows it but i have to click the field first.

When the current_user form load if I click the menu bar it gives me the error userdepartment not found then if i click the userdepartment field on the current user form first it shows only the values in the menu for the department.

So my values work, I just need to click the fields first. The two fields on the current user form have thier values being set by the varialbes. Why will it set them here and then make me click the fields before it will allow others to use them
 
The statements:
Public oGlobal
oGlobal = CREATEOBJECT('myGlobalClass')
Need to be BEFORE the "READ EVENTS"
 
Good catch wgcs !!!!!!
I hadn't even noticed, no wonder the thing never gets instanciated.
 
mgagnon, I get the values fine, its refering to them after the current_user form. I need to click the values in the text boxes before I can use them. So the values return correctly, but to use them after the form I need to click the values
 
You refer to them the same way, since now they are stored in are public class.
Try somewhere else in your app:

messagebox(TRANSFORM(oglobal.userdepartment))

You should get a value.
 
If wanted I can email the code to see what I am talking about. Its so weird, get this. Ok from the login screen it opens current user. On current user I have the form.caption = username + "/" + userdepartment and it shows everytime. Then I have two text boxes that show the username in one and the userdepartment in the other and they both show the values correct. Its when you click the command button that opens the account info form you get the username error and when you click the menu you get the department. BUT IF YOU CLICK IN BOTH THE TEXT BOXES YOU RECIEVE NO ERRORS, weird huh, wish I had become a doctors instead, who knows when you make mistakes you came in like that I swear.
 
Send me waht you have and I'll take a look. Due to virus checkers please zip your forms and data, so it doesn't get thrown out.
 
lashwarj

Or give me your e-mail, I have prepared a small sample based on your code.
 
My guess is that all he needs is to place a THISFORM.Refresh right after he sets the textbox values. Also, it's clearer coding to make the textbox's .ControlSource point to a custom property on the form, then just refer to that form property as THISFORM.UserName or whatever, instead of referring to THISFORM.TextBoxUserName.Value
 
jester4281@jester4281.com thanks mgagnon
 
WGCS, But the big question is I shouldnt need to have them on the one form they are public, why declare them anywhere if they are public
 
If the user presses "cancel" instead of login, you don't want his changes to be saved into the global object, so I wouldn't make the ControlSource point to "oGlobal.User_Name";

Also, It's my habit (because I consider it neater and more maintainable) to make anything the user edits on a form, a custom property of the form. Then I make two custom Methods, "DoOK" and "DoCancel"... the Ok button's Click event simply calls "THISFORM.DoOK", and the Cancel button's click calls "THISFORM.DoCancel". Each control on the form has it's ControlSource set to something like: "THISFORM.cUser_Name" and "THISFORM.cPassword".

Then, in THISFORM.DoOK, whatever action should be taken is coded, for example, in your case:
Code:
LOCATE FOR UPPER(user_setup.user_name) = UPPER(ALLTRIM(THISFORM.cUser_name))
IF FOUND() AND ALLTRIM(user_setup.password) = ALLTRIM(THISFORM.cPassword)
  oGlobal.username = user_setup.user_name
  oGlobal.userdepartment = user_setup.department
  THISFORM.Release()
ELSE
  MESSAGEBOX("The User Name or Password is incorrect.","Please try again.")
  Thisform.cPassword = ""
  Thisform.txtUser_name.SETFOCUS
ENDIF

Then, in the code that called this form (in your main PRG), check if there's a valid login:
Code:
IF NOT ( EMPTY(oGlobal.User_Name) or EMPTY(oGlobal.department)
  * We're Logged in!
  DO FORM current_user
ELSE 
  * Never logged in
  QUIT
ENDIF
 
If the user presses cancel it quits the program, its a enter or no enter deal
 
WGCS,

Your code looks sound but I already have the values, they are being set into the text boxes and in the caption, but when refering to them outside of the current user form the generate the error unless I click in both boxes. As a public variable this shouldnt be happening, I cant figure out why it is.. I like have it on the tip of my tounge but cant spit it out kind of deal here. The users log in fine. The current user form displays and shows both the correct username and department. Its when I click the admin button for changing thier password, if they dont click in the username field first when they click the admin button they get cannot find username. In the admin forms init I have it filtering the database for user_setup.username = username. And the department is used in the skip of my menus. Which also works fine if you first click the department field.

From your code I couldnt tell if you though the loggin wasnt working or f you thought the issues was in that event.
 
I was thinking the problem could be stemming from the general architecture of the program: I can't see your form in design mode, so I don't know what you have in the control source (It's impossible to tell which suggestions were taken and which weren't to get it to work so far); So I wanted to present an overall structure that WILL work.

Also, I wanted to point out several design habits that I consider "good practice"; I find they make things work more smoothly and consistently.

I can't see your form in design mode, so I don't know what is in the ControlSource of the textboxes, or what code is in what events. This is one reason for my coding practice of making all ControlSource's point to custom form Properties, and all Events (that are needed) point to custom form methods.
 
hahahahahahaha get this, this is grand. From within the program I have it so you can click on the current user form and log off and allow someone else to log in. using that process everything works fine after. So if i go in and click the username and userdepartment fields once you dont have to do it again until the user exts the program completely


EX. User name Ryan logs in, he has to click the username and departmet field to get the admin button to work and the menu to work. Once ryan does it he logs off leaving the application running and helen logs in, she doesnt have to click anything, everything works fine for her
 
Do this: While the form is up for the first time, SUSPEND your program (I use an ON KEY LABEL F12 SUSPEND to do this), then issue DEBUG to open the debugger, add the individual properties of your global object to the watch window, and press the Trace button several times, till it starts waiting for user input. Now, click on the next textbox (the ones that you have to pass through at least once to get it to work), and the debugger should come back. Continue clicking the step button to see just what happens.
 
why class 2 public variables?
Keep it simple and declare them public in the startup program. OR make them a property of the form.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top