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

Russ05

New member
Local time
Today, 15:43
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
 

SHANEMAC51

Active member
Local time
Today, 08:43
Joined
Jan 28, 2022
Messages
310
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
 

Gasman

Enthusiastic Amateur
Local time
Today, 06:43
Joined
Sep 21, 2011
Messages
14,260
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.
 

bastanu

AWF VIP
Local time
Yesterday, 22:43
Joined
Apr 13, 2010
Messages
1,402
You want to move your code on Form1 from the Form Load to Form Open event (in addition to what Gasman suggested).
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 01:43
Joined
Feb 19, 2002
Messages
43,257
Why do you need any code at all in the "called" form? You should be using the Where argument for this to make the form automatically open to the correct record. The OpenArgs is used for different things.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 06:43
Joined
Feb 19, 2013
Messages
16,605
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
 

Russ05

New member
Local time
Today, 15:43
Joined
Jan 19, 2022
Messages
7
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.
 

Russ05

New member
Local time
Today, 15:43
Joined
Jan 19, 2022
Messages
7
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

Top Bottom