I have two subforms on a tab. On subform 1, I have a combo box (eName) and if the value is Tour De Lis, the make subform 2 visible. I am not sure how to approach this situation.
If ename.text = "Tour De Lis" then ' the value for the combo is assumed to be an ID and not visible
Me!Subform2.Form.visible = true
else
Me!Subform2.Form.visible = false
end if
However you may want to invoke this when loading the form - based on the value of the Foreign Key or the "eName" field in the record, and change the visibility on the after update event of the cbo.
1. The .text property is only available while the control still has the focus so it is only available in the change event and possibly the click event. I would always use the .Value property for a combo.
2. Keep in mind that you are actually referencing the bound value, not the value that is displayed.
3. When the data in subformB depends on a value on formA, use the Current event of formA to control the visibility of subformB.
4. If the bound value is not the visible value, you need to reference the .Column property of the control to compare the string to the text value of the combo's RowSource.
I was able to solve the problem with the users who responded and with a little help from ChatGPT.
Private Sub eName_AfterUpdate()
If Me.eName = "Tour De Lis LA" Then
' Replace "SomeValue" with the value that should trigger hiding Form2
Me.tourDetails_subform.Visible = True
Else
Me.tourDetails_subform.Visible = False
End If
End Sub
The error is? It is likely associated with incorrectly referencing the combobox properties, although this looks like it may be the first error encounterd in the code you posted:
What is the rowsource for the cbo eName? If it is multicolumn note the order of columns, even those that are hidden (width = 0). If the rowsource includes two columns say TourID and TourName in that order and the cbo eName is bound to TourID, then
Code:
If Me!eName.Value = thevalueofTourIDcorrespondingto"Tour De Lis LA" Then
Me.tourDetails_subform.Visible = True
Else
Me.tourDetails_subform.Visible = False
End if
Alternately, if you explicitly need to refer to matching the text "Tour De Lis LA" then the statement is likely
If Me!eName.text = "Tour De Lis LA" Then ... etc
' (where the text TourName is the second column in the bound cbo control and is the visible content of the cbo).
Or
If Me!eName.column(1) = "Tour De Lis LA" Then ... etc
' (where TourName is the second column in the rowsource of the cbo control - column numbering starts at 0).
As @Pat Hartman mentioned - the display of the subform depends upon the value for "TourID" in subform1 records, so place the code in the Current event of subform1. If you also want the subform2 to be hidden immediately on selection of a value in the cbo eName, then you can also place the code in the Change event of the combo eName.
@johnny_swindle
Glad you have it sorted. Your rowsource is a simple list of values, not driven from a table of stored tour name values (hence the difference). This is OK but may in the longer term mean it is not a readily maintained.