The DoCmd.OpenForm parameters you want to adjust are the optional WhereCondition
and DataMode
. WhereCondition
accepts a SQL Where clause without the word where. This restriction is applied to the form's rowsource before the form opens.
DataMode
can override the form's saved settings for AllowEdits and DataEntry. It sounds like you want to use acFormEdit in this case.
Private Sub CommandFoo_OnClick()
Dim strWhere As String
'Do yourself a favor and build the WhereCondition in a string variable so that you can see what you've built.
'Surround text values in double quotes.
'Surround dates with # and specify in m/d/yyyy or yyyy-mm-dd
'Make sure you build in spaces in as necessary.
strWhere = "NumberField1=" & ListBox1.Columns(0) & " AND TextField2=" & Chr(34) & ListBox1.Columns(1) & Chr(34)
Debug.Print strWhere
DoCmd.OpenForm FormName:="YourFormName", WhereCondition:=strWhere, DataMode=acFormEdit
End Sub