Using OpenArgs to open a specific record on another form (1 Viewer)

Russ05

New member
Local time
, 08:21
Joined
Jan 19, 2022
Messages
7
Hello everyone,

I have tried using the OpenArgs parameter to open another form ("Form1") as modal. WHen it opens I need it to go to a specific record based on the "txtemplID" field on the "frmEmployeeDetails" form. I have used the following code:

Activated from a button on "frmEmployeeDetails" form.

Private Sub cmdOpenFrm_Click()
Dim StrValue As String

StrValue = Me!txtEmpID.Value
'MsgBox "The value is " & StrValue
'I am doing this to check the string is correct. At this stage it returns an employee number as I expect.

DoCmd.OpenForm "form1", acNormal, , , , acDialog, StrValue

End Sub



In form "Form1" - form I am opening

Private Sub Form_Load()
Dim StrValue As String

'If OpenArgs property contains EmpID, find
' corresponding employee record and display it on
' form. For example,if the OpenArgs property
' contains "801029", move to first "801029"
' record.


StrValue = Forms!frmEmployeeDetails.OpenArgs
MsgBox "The value is " & StrValue
'It now doesn't the string as I received in the cmdOpenFrm_Click() module as above.
If Len(StrValue) > 0 Then
DoCmd.GoToControl txtEmpID
DoCmd.FindRecord StrValue
End If
End Sub


When I click the button the form (Form1) opens but nothing shows in the txtempID text box on the form. It is just a blank form. I note above, when I use the Msgbox to return the string value in the Form_Load module it doesn't return the string set in cmdOpenFrm_Click().
I have searched for the same error but found nothing, hence my appreciation for any help you can provide.

Kind regards,
Russel
 
Hello everyone,

I have tried using the OpenArgs parameter to open another form ("Form1") as modal. WHen it opens I need it to go to a specific record based on the "txtemplID" field on the "frmEmployeeDetails" form. I have used the following code:

Activated from a button on "frmEmployeeDetails" form.

Private Sub cmdOpenFrm_Click()
Dim StrValue As String

StrValue = Me!txtEmpID.Value
'MsgBox "The value is " & StrValue
'I am doing this to check the string is correct. At this stage it returns an employee number as I expect.

''DoCmd.OpenForm "form1", acNormal, , , , acDialog, StrValue

End Sub
Code:
DoCmd.OpenForm "form1", acNormal, , "txtemplID=" & StrValue, acFormEdit, acDialog
 
You are using the wrong form's OpenArgs :(

Think of it. You are passsing the OpenArgs to Form1, NOT Forms!frmEmployeeDetails, so you use Form1 OpenArgs, which when in Form1 is Me.OpenArgs ?
If you use the Form elsewhere, without passing in an ID in OpenArgs, then test it has a value first.
 
You want to move your code on Form1 from the Form Load to Form Open event (in addition to what Gasman suggested).
 
The WHERE parameter is a filter (which a user can remove once the form is opened).

I think what the OP wants is to not allow a user to be able to do that - so you can use openargs as a way to do that

One option in frmEmployeeDetails is (Assuming field is actually called EmpID)

DoCmd.OpenForm "form1", acNormal, , , , acDialog, "[EmpID]=" & txtEmpID

(aside: where do people get the idea that using Me! is the better thing to do?)

and in form1 open event (assumes a tablename recordsource without criteria)

me.recordsource= me.recordsource & " WHERE " & nz(me.openargs,"true") 'if no openarg supplied, return all records, change to "false" if no records to be returned, replace ";" with zls if included in recordsource

If the two forms are tightly bound together (i.e. form1 is only every opened from frmEmployeeDetails) you can just use as a recordsource for form1

SELECT * FROM myTable WHERE EmpID=forms!frmEmployeeDetails.txtEmpID

no coding required
 
You are using the wrong form's OpenArgs :(

Think of it. You are passsing the OpenArgs to Form1, NOT Forms!frmEmployeeDetails, so you use Form1 OpenArgs, which when in Form1 is Me.OpenArgs ?
If you use the Form elsewhere, without passing in an ID in OpenArgs, then test it has a value first.
Thank you Gasman, even though it took me some time to actually understand or identify what you explained. I guess sometimes you get too close to something and cannot see the 'wood for the trees'. Much appreciated.
 
The WHERE parameter is a filter (which a user can remove once the form is opened).

I think what the OP wants is to not allow a user to be able to do that - so you can use openargs as a way to do that

One option in frmEmployeeDetails is (Assuming field is actually called EmpID)

DoCmd.OpenForm "form1", acNormal, , , , acDialog, "[EmpID]=" & txtEmpID

(aside: where do people get the idea that using Me! is the better thing to do?)

and in form1 open event (assumes a tablename recordsource without criteria)

me.recordsource= me.recordsource & " WHERE " & nz(me.openargs,"true") 'if no openarg supplied, return all records, change to "false" if no records to be returned, replace ";" with zls if included in recordsource

If the two forms are tightly bound together (i.e. form1 is only every opened from frmEmployeeDetails) you can just use as a recordsource for form1

SELECT * FROM myTable WHERE EmpID=forms!frmEmployeeDetails.txtEmpID

no coding required
Thank you CJ_London. Great info and a good guide for me to review.
 

Users who are viewing this thread

Back
Top Bottom