PDA

View Full Version : Solved: Passing Variables between subs



JG4life
06-07-2006, 11:12 AM
I have ben workin on this for a few hours now and I have no idea how to do it but I have to assume it is possible. Here is my situation.

I have a form with 3 fields in it and a bunch of buttons to do various functions with form (advance a record, go back, close, new delete etc..) I want it to check to make sure that none of the fields are empty when any of those buttons are clicked. So what I did was create a module called "verification" and then a subroutine called "Check_Instrument_Form". Below is the code for that subroutine.


Sub Check_Instrument_Form()

Check_Make = Nz(Forms![Instruments Query]![Make], "")
Check_Model = Nz(Forms![Instruments Query]![Model], "")
Check_Inv_Type = Nz(Forms![Instruments Query]![Inventory Type], "")

If Check_Make = "" Or Check_Model = "" Or Check_Inv_Type = "" Then

MsgBox ("You are missing Infomation. Please Complete this form or " & _
"Delete this Instrument to continue")

End If

End Sub


Then, in the on_click event for the button I have the following code


Private Sub Exit_Form_Click()

On Error Resume Next

verification.Check_Instrument_Form

If Check_Make = "" Or Check_Model = "" Or Check_Inv_Type = "" Then GoTo Cancel

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

DoCmd.Close

Cancel:

End Sub


So as you can see, it runs the Exit_Form_Click subroutine, makes a call to the error checking subroutine and then goes back to the Exit_Form_click subroutine after it has finished and does an additional check against the same variables that existed in the Check_Instrument_Form subroutine. The problem is when it continues with the code in Exit_Form_Click after it has run the error checking code it "forgets" the values assigned to the variable. How do I get it to remember them?

The alternative is to cut and paste the error checking code at the top of every subroutine for every button but that seems kinda silly. This example is checking 3 fields but I have another form that I want to do the same thing and it is checking 15 fields making it more difficult to make changes down the road.

Any help would be greatly appreciated!!!

Sean

JG4life
06-07-2006, 12:25 PM
I figured it out. My Problem was the error checking was in a module instead of on the same page as the code for the button. Once I put them on the same page and Defined the variable at the top of the page (instead of within the subroutine) it worked fine. I figured I would post my solution and mark the topic as solved for the next guy that makes the same mistake as me.