Open Args Usage (1 Viewer)

rzw0wr

I will always be a newbie
Local time
Today, 17:02
Joined
Apr 1, 2012
Messages
489
I have tried a few times to get open args to work while opening a new form.

I never could get it to work.

Will someone tell me how this is used.

Thanks,
Dale
 

Steve R.

Retired
Local time
Today, 17:02
Joined
Jul 5, 2006
Messages
4,707
You can look it up in MS Access help. In the example below Me.Name passes the name of the calling form to the report to be opened.

Code:
DoCmd.OpenReport csPrintRecord, acViewPreview, , , , Me.Name

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.
 
Last edited:

missinglinq

AWF VIP
Local time
Today, 17:02
Joined
Jun 20, 2003
Messages
6,423
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

rst.FindFirst "[RecordID] = " & Me.OpenArgs

with

rst.FindFirst "[RecordID] = '" & Me.OpenArgs & "'"

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.

Linq ;0)>
 

rzw0wr

I will always be a newbie
Local time
Today, 17:02
Joined
Apr 1, 2012
Messages
489
Really, linq how do you make stuff so easy to understand.
I worked all day and never got it right.

You should have been a teacher.


Thanks,
Dale
 

missinglinq

AWF VIP
Local time
Today, 17:02
Joined
Jun 20, 2003
Messages
6,423
Been doing this for donkey years!

Glad we could help, Dale!

Linq ;0)>
 

Users who are viewing this thread

Top Bottom