OpenArgs is an alpha string. So if you want to change it to a number you will need to use "val". It is also recommend to use the "nz" function, just in case you accidentally pass a null string.
In the form to be opened you would have in the open event:
Code:
SomeStringVariable= NZ(Me.OpenArgs,"ERROR")
You then use SomeStringVariable for any subsequent computations. That variable should be declared as PUBLIC so that it is available for all subroutines in the form/report.
Here's three different groups of code, depending on exactly how you want to use this; be sure to read the section headers (in blue) to find exactly what you need. For Existing or New Record
In the Primary Form:
Code:
Private Sub Go2SecondaryForm_Click()
If Nz(Me.RecordID,"") <> "" Then
DoCmd.OpenForm "Secondary Form", , , , , , Me.RecordID
Else
MsgBox "A RecordID Must Be Entered First!"
End If
End Sub
In the Secondary Form:
Code:
Private Sub Form_Load()
Dim rst As Recordset
If Nz(Me.OpenArgs,"") <> "" Then
Set rst = Me.RecordsetClone
rst.FindFirst "[RecordID] = " & Me.OpenArgs
If Not rst.NoMatch Then
Me.Bookmark = rst.Bookmark
Else
DoCmd.GoToRecord , , acNewRec
Me.RecordID = Me.OpenArgs
End If
rst.Close
Set rst = Nothing
End If
End Sub
For Existing Record only
In the Primary Form:
Code:
Private Sub Go2SecondaryForm_Click()
If Nz(Me.RecordID,"") <> "" Then
DoCmd.OpenForm "Secondary Form", , , , , , Me.RecordID
Else
MsgBox "A RecordID Must Be Entered First!"
End If
End Sub
In the Secondary Form:
Code:
Private Sub Form_Load()
Dim rst As Recordset
If Nz(Me.OpenArgs,"") <> "" Then
Set rst = Me.RecordsetClone
rst.FindFirst "[RecordID] = " & Me.OpenArgs
If Not rst.NoMatch Then
Me.Bookmark = rst.Bookmark
Else
Msgbox "Matching Record Not Found!"
End If
rst.Close
Set rst = Nothing
End If
End Sub
The above two groups of code assumes that RecordID is Numeric. If it is actually defined Text, replace the line
You'll also need to replace Go2SecondaryForm with the actual name of your Command Button and SecondaryForm.
For New Record only
In the Primary Form:
Code:
Private Sub Go2SecondaryForm_Click()
If Nz(Me.RecordID,"") <> "" Then
DoCmd.OpenForm "Secondary Form", , , , , , Me.RecordID
Else
MsgBox "A RecordID Must Be Entered First!"
End If
End Sub
In the Secondary Form:
Code:
Private Sub Form_Load()
DoCmd.GoToRecord , , acNewRec
If Nz(Me.OpenArgs,"") <> "" Then
Me.RecordID = Me.OpenArgs
End If
End Sub
For the New Record only code you'll need to replace Go2SecondaryForm with the actual name of your Command Button and SecondaryForm with the actual name of your second Form and RecordID with the actual name of your Control.