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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing variables when opening another form 2

Status
Not open for further replies.

delman333

Technical User
Oct 20, 2003
32
US
Here is what I'm trying to do:

Call GoToOtherForm("ScenarioOverviewForm", "ScenarioID", "Location")

Private Sub GoToOtherForm(ByVal SelectedForm As String, ByVal FormID As String, ByVal FormFocus As String)

strScenarioID = Me![OrdnanceID]
DoCmd.Close acForm, "ScenarioOrdnanceForm"
DoCmd.OpenForm SelectedForm, , , , acFormEdit, , strScenario_ID
Forms!ScenarioOverviewForm![FormID].SetFocus
DoCmd.FindRecord strScenarioID, , True, , True, , True
Forms!ScenarioOverviewForm![FormFocus].SetFocus

End Sub

The correct form does open...but it can't find the FormID or FormFocus I pass to the Sub. My limited experience tells me this is because the new form doesn't know about the value of the variables being passed. If this is correct...does this mean I need to do something with a global variable...or put the Sub itself in a global module? If so...how would I do that? If incorrect or unnecessary...how can I make this thing work?

Thanks in advance.
 
Try the above changes to your code above:

.....
Forms!ScenarioOverviewForm[blue](FormID)[/blue].SetFocus
.....
Forms!ScenarioOverviewForm[blue](FormFocus)[/blue].SetFocus

These changes ensure that the variable values will be used to represent the form controls; the way you have it, the program will be looking for actual controls on the form called "Formid" and "FormFocus" respectively.

Hope this helps,


Steve Lewy
Solutions Developer
steve@lewycomputing.com.au
(dont cut corners or you'll go round in circles)
 
When moving among forms, it is helpful to open the new form BEFORE closing the old form. That makes it much easier to transfer information between the forms. Remember that the code will execute much faster than your users can see, so the users won't see two forms at once.

You can certainly transfer values using code, but there are other options. For example, you can make a field in the new form equal to a field in the old form. If the new form opens before the old form closes, this is a viable option (the fields can be invisible if that is helpful). You can also create a table with one record, and no way to add records. Then each field will be like a global variable.
 
Although as it turns out I had another issue to deal with concerning this Sub...the tip you provided solved the original issue I asked about. Thanks.
 
Shoot...I posted my reply for Steve Lewy before realizing that OhioSteve had also posted a tip.

Here is what I ended up with:

Private Sub GoToOtherForm(ByVal SelectedForm As String, ByVal FormID As String, ByVal FormFocus As String)

strScenarioID = Me![OrdnanceID]
DoCmd.OpenForm (SelectedForm), , , , acFormEdit, , strScenario_ID
DoCmd.GoToControl (FormID)
DoCmd.FindRecord strScenarioID, , True, , True, , True
DoCmd.GoToControl (FormFocus)
DoCmd.Close acForm, "ScenarioOrdnanceForm"

End Sub

So after combining all this good info...everything is working great.

Thanks to both of you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top