Browsing to another form, and passing values

tfurnivall

Registered User.
Local time
Today, 03:45
Joined
Apr 19, 2012
Messages
81
I'm not certain quite how to express this, but let's take a stab.

All forms and controls are unbound. (See below)

I have a situation with two object classes (let's call them Studies and Surveys). Each class has an independent existence, but surveys 'belong' to a study. I need to be able to look at Surveys by themselves, or as part of a study. Each class has a whole form to itself. A study has a button that allows me to limit the selection of surveys to those which belong to the study.

The code for the "Surveys" button on the Study form includes the following:
Code:
   DoCmd.BrowseTo acBrowseToForm, "STDY_SurveyDesignForm"
'  Load study fields into the target form
   Form_STDY_SurveyDesignForm.lblStudyID.Caption = StudyID
   Form_STDY_SurveyDesignForm.lblStudyName.Caption = StudyName
   
'  Adjust visibility of targetform controls
   Form_STDY_SurveyDesignForm.cmdQuestions.Visible = True
   Form_STDY_SurveyDesignForm.cmdBrowseSurvey.Visible = True
   Form_STDY_SurveyDesignForm.cmdSurveyAction.Visible = True
   Form_STDY_SurveyDesignForm.cmdSurveyAction.Caption = "New Survey"
This works fine - and gives me a blank form with the two fields set up.
Now I'm trying to go one step beyond this, and have the first of the Surveys visible.
So happens I have a routine on the form called BRGoToFirst. This calls a routine called BRSetUpBrowse, which depends on a routine called BRSetBrowseObject, which defines all the parameters for a Browse. So I add the following right after the last line of code above:
Code:
   Form_STDY_SurveyDesignForm.BRGoToFirst
BRSetBrowseObject is called as part of the Form.Load procedure.
When I trace execution I see the DoCmd.BrowseTo execute, and the Survey form gets loaded. In addition, the BRSetBrowseObject routine executes, setting the browse objetc to a Survey (and all the goodies associated with it).

I also see control return (briefly) to the code for the surveys button (the lines that poke values into fields on the form - Study ID and name, and do some control management.

Here's my problem.

The Form.Load procedure operates fine.
The additional lines in the Study form operate fine, and produce the desired result. However, all the setting of browse parameters has disappeared.

It's AS IF the form instance that I loaded has been replaced by another. Except that, the values I poked into it are all there. So, it must be the same instance of the form, right? Except that some control values are affected, but a global variable has been wiped out!

I'm totally stumped about how even to debug this situation. (I'm guessing a lab situation, probably, but that's a lot of effort ). Do any of the folks here have any ideas?

Or do you think there's a chance of getting MS to implement inheritance properly?

Tony

PS I realize that this is probably the 'cost' of breaking away from Microsoft's paradigm of how a User Interface should operate - trouble is, I have my own ideas about that ;).
 
If you refer to objects of another form using the syntax

Form_FormName.ObjectName.(SOmePropertyOrValue)

then unless you know exactly what you are doing, you open yourself to untold problems with instances being open here and there and everywhere.

Use the reference method of

Forms.FormName.FormObject.Whatever
 
Use the reference method of

Forms.FormName.FormObject.Whatever
One slight correction - Exclamation point to refer to the forms collection:

Forms!FormName.FormObject.Whatever
 
Thanks to spikepl and Bob Larson for the suggestions. So I've changed the syntax for specifying the form. Here's the revised code for the button that causes the transfer of control to the secondary form:
Code:
Private Sub cmdSurvey_Click()

'   Browse or create a survey for the existing study.
'   Increment the STDY_SurveyCount, and copy the STDY_studyid value

Dim StudyID As String
Dim StudyName As String
Dim SurveyPrefix As String
Dim SurveyID As String          '   We actually get the ID before we create the survey!
Dim Status As Long
Dim SurveyCount As Integer

'   Get the StudyID and StudyName

#If TraceLevel > 2 Then
    Debug.Print Time(), Me.Caption, "BrowseSurveys", Study.StudyID, Me.txtStudyID
#End If

If IsNull(Me.txtStudyID) Then
   StudyID = ""
Else
   StudyID = Me.txtStudyID
End If

If IsNull(Me.txtStudyName) Then
   StudyName = ""
Else
   StudyName = Me.txtStudyName
End If

On Error Resume Next

'   Back up the study to the database
If StudyID > "" Then
'  Save the existing state of the study. This will alllow us to come back and pick up where we left off!
   UnloadFormFieldsIntoObject
'  Locate the record within the database. This could be an as yet unsaved Study.
   Study.SaveToDatabase

'  Load the target form
   #If TraceLevel > 3 Then
       Debug.Print Time(), Me.Caption, "Transfer values to Survey Form"
       Debug.Print , , "PassValues", "StudyID="; StudyID, "StudyName="; StudyName
   #End If
   DoCmd.BrowseTo acBrowseToForm, "STDY_SurveyDesignForm"
'  Load study fields into the target form
   Forms!STDY_SurveyDesignForm.lblStudyID.Caption = StudyID
   Forms!STDY_SurveyDesignForm.lblStudyID.Tag = StudyID
   Forms!STDY_SurveyDesignForm.lblStudyName.Caption = StudyName
   Forms!STDY_SurveyDesignForm.lblStudyName.Tag = StudyName
#If TraceLevel > 2 Then
   Debug.Print Time(), Me.Caption, "Values passed to Survey form"
#End If

'  Adjust visibility of targetform controls
   Forms!STDY_SurveyDesignForm.cmdQuestions.Visible = True
   Forms!STDY_SurveyDesignForm.cmdBrowseSurvey.Visible = True
   Forms!STDY_SurveyDesignForm.cmdSurveyAction.Visible = True
   Forms!STDY_SurveyDesignForm.cmdSurveyAction.Caption = "New Survey"    '   We might want to create a new one
   Forms!STDY_SurveyDesignForm.BRGoToFirst
#If TraceLevel > 2 Then
   Debug.Print Time(), Me.Caption, "Survey controls adjusted"
#End If

Else
   MsgBox "Please create a study before trying to add a survey to it!"
End If

On Error GoTo 0

'   Get rid of the Study object!
Set Study = Nothing

End Sub
And here's the trace of what actually happens. (Most of the lines are from the snippet above, but a few are from the STUDY object, and the last chunk from the SURVEY form).

Code:
********************************************************************************
***                                                                          ***
*** Loading form:  STUDYFORM                                                 ***
***                                                                          ***
********************************************************************************
1/22/2013   1/22/2013   USA

11:38:56 AM   STUDYFORM     LOADFORM
11:38:56 AM   BRSetBrowseObject           STDY_Study    STDY_StudyControl
11:38:56 AM   STUDYFORM                   BrowseObject: STDY_Study    BrowseObjectTable: STDY_StudyControl      Segmented browse: False
11:38:59 AM   STUDYFORM     New Study     
11:38:59 AM   STUDY         NewInstance:  KinquX1N4huHZYDfg1Yig6q8sg5q8G7V
11:39:15 AM   STUDYFORM     Update Study  
11:39:15 AM   STUDYFORM     UnloadForm=>Object          STDY_Study                  KinquX1N4huHZYDfg1Yig6q8sg5q8G7V
                            StudyID=KinquX1N4huHZYDfg1Yig6q8sg5q8G7V  StudyName=Ringing Satisfaction Study (2013)
11:39:15 AM   STUDY         SaveStudy     KinquX1N4huHZYDfg1Yig6q8sg5q8G7V          Status= 0     Ringing Satisfaction Study (2013)
11:39:15 AM   STUDY         InsertValues  KinquX1N4huHZYDfg1Yig6q8sg5q8G7V          Status= 0     Ringing Satisfaction Study (2013)
11:39:19 AM   STUDYFORM     BrowseSurveys KinquX1N4huHZYDfg1Yig6q8sg5q8G7V          KinquX1N4huHZYDfg1Yig6q8sg5q8G7V
11:39:19 AM   STUDYFORM     UnloadForm=>Object          STDY_Study                  KinquX1N4huHZYDfg1Yig6q8sg5q8G7V
                            StudyID=KinquX1N4huHZYDfg1Yig6q8sg5q8G7V  StudyName=Ringing Satisfaction Study (2013)
11:39:19 AM   STUDY         SaveStudy     KinquX1N4huHZYDfg1Yig6q8sg5q8G7V          Status= 0     Ringing Satisfaction Study (2013)
11:39:19 AM   STUDY         InsertValues  KinquX1N4huHZYDfg1Yig6q8sg5q8G7V          Status= 0     Ringing Satisfaction Study (2013)
11:39:19 AM   STUDYFORM     Transfer values to Survey Form
                            PassValues    StudyID=KinquX1N4huHZYDfg1Yig6q8sg5q8G7V  StudyName=Ringing Satisfaction Study (2013)
********************************************************************************
***                                                                          ***
*** Loading form:  SURVEYFORM                                                ***
***                                                                          ***
********************************************************************************
11:39:19 AM   SURVEYFORM    PassedValues  StudyID=      Caption= 
11:39:19 AM   SURVEYFORM    PassedValues  StudyName=    Caption= 
11:39:19 AM   SURVEYFORM    BRSetBrowseObject           STDY_Survey   STDY_SurveyControl
11:39:19 AM   SURVEYFORM    BrowseObject=STDY_Survey
                            BrowseObjectTable=STDY_SurveyControl
                            BrowseKeyField=STDY_SurveyID
                            BrowseObjectTable=STDY_SurveyControl
                            SegmentedBrowse=True
                            BrowseParentKey=STDY_StudyID
The questions I have are:

1) It does not seem possible to capture the exact sequence of operations between cmdSurvey (the button handler) and FormLoad (the load handler for the new form). If it weren't for the fact that I've had this working (as coded), and the values passed, then I'd assume that nothing in cmdSurvey gets executed after the DoCmdBrowseTo statement. THis seems borne out by the absence of the traces past that point.
How can I get better execution profiling?

2) How can I pass values between forms? This is not just a 'typical' sub-form situation. The Survey form is completely independent (ie it can browse All surveys, regardless of what Study they belong to). Is there any way to create an instance of the Survey form, modify it as I want and then switch to that instance?

Thanks for any help,

Tony
 

Users who are viewing this thread

Back
Top Bottom