I want:
To be able to open a form, select an item from a control box, click a button to open a new form and be able to input new records that have the previously selected item added to their fields.
What I have done so far:
I have made two forms, added the controls in both, added a command button.
I have used MS Access wizard to add this code:
Private Sub CommandNext_Click()
On Error GoTo Err_CommandNext_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "FormX"
DoCmd.OpenForm stDocName, , , "[BatchID] = " & Me![ListBatch], acFormAdd
Exit_CommandNext_Click:
Exit Sub
Err_CommandNext_Click:
MsgBox Err.Description
Resume Exit_CommandNext_Click
End Sub
The third line is mine.
As a result:
- If the second form isn’t already open, pressing the button on the first form will open the second one with empty data (the text field that should show the value of the parameter is empty).
- If the second form is already open, pressing the button on the first form will change focus to the second one and it will show an old, existing record that matches the parameter I selected
- I want to open the second form without any old records showing but with the selected parameter appearing in the designated text field.
In both cases in the second form the Filter property gets populated with the parameter I’ve sent using the button, but that isn’t what I am aiming for.
Are you aware the 4th argument for the open form command is the where condition or filter to apply?
If you use the last parameter which is called open arguments (OpenArgs) then you can write code in the 2nd forms open event to set the default value for the column in question.
The open form code would look something like this:
You could then put code similar to the following in this 2nd forms open event:
Or if you do not need the ID to be set for every new record you could just set the current value of the control to the defaultID variable.
Please note the code above would not open the form if the open argument turned out to be empty or 0.