Q: How to pre-load a combo-box or set its default value with a value passed from another form.
Context:
I have a database of aid donations which includes two main tables: 'DataPersons' and 'DataHholds'. These tables have a relationship established as
[DataPersons].[HHoldref] = [DataHhold].[HholdID]
where HholdID is the primary key for the DataHhold table. This is a one-to-many relationship as a household can comprise more than one registered person.
A form, selected from the switchboard, gives options for a report based on DataHhold - these options include viewing the report, printing, writing to pdf, etc. The particular household is selected by a combobox on that form. All works as intended.
I have another form, also selected from a different switchboard menu, which allows editing of the records in DataPersons - again all fine so far.
I would like to be able to move directly between these two forms, without having to go back through the switchboard - specifically, with the Persons form open and a particular person selected, to then open the reports form with the appropriate data for that person's household. To do this I have a button on the Person form which calls the Household form and modifies the query on which it is based. This works fine, except that the combobox on the household form remains blank and the buttons produce an empty report. I have to select the household again (although there is only one listed) before the reports can be seen.
What I want to do is to pre-load the combobox with the data from the first record returned by the query; ie something like
[forms].[HholdSelect].[combobox] = [forms].[fP1PersonReview].[HholdID].
I can hard-code the default value of the combobox manually by using
Me.[combobox].default = 234 where '234' is a typical HholdID but the following attempt at automation does not work
Me.[combobox].default = [forms].[fP1PersonReview].[HholdID]
As a work-around I have added a simple textbox to the Hhold form, and attempted to give it a value, as per the attached code.
Code:
Private Sub Form_Open(Cancel As Integer)
Dim stFormName As String: stFormName = "fP1PersonReview"
Dim inHholdID As Double
'Check if person review form is open, alter the data query if present.
If SysCmd(acSysCmdGetObjectState, acForm, stFormName) = acObjStateOpen Then
Me.ItemSelector.RowSource = "qHholdSelectRev"
inHholdID = Forms!stFormName!HholdID << Debug error - cannot find form referred to
'Me.SelectedHhold = inHholdID
Else
Me.ItemSelector.RowSource = "qHholdSelectAll"
End If
Me.ItemSelector.Requery
Me.ItemSelector.SetFocus
End Sub
In each case, the debug procedure reports that it cannot find the PersonReview form, even though it has successfully found it just two lines earlier in the same subroutine and the form is still open on the screen.
Is FindFirst worth exploring? Any other advice?