OpenArgs

noodnutt

New member
Local time
Tomorrow, 00:54
Joined
Oct 2, 2024
Messages
6
Hi Guys

I have two command Buttons on two different forms that access the same form "fDcust"

One command button (On the Main Menu Form)
One Command Button on the (Search Enquiry Form)

The Main Menu CmdBtn is as follows:
Code:
Private Sub lBtnEdit_Click()
    DoCmd.OpenForm "fDcust", , , , , acDialog
End Sub

When "fDcust" opens from the Main Menu, it does so to a new record, but!

In the Form Open Event I have the following:
Code:
Private Sub Form_Open(Cancel As Integer)  
    With Me
        .Move Left:=1500, Top:=2000, Width:=12000, Height:=6500
    End With
    DoCmd.GoToRecord , , acNewRec
End Sub

The problem now is, when I open "fDCust" from my Search Enquiry form:
Goto Command.png

It goes to a new record instead of going to the required record.

Can I open the same form with separate Open Arguments from two different Command Buttons?

TIA
Mark.

PS: I originally had a New Record Command button in fDcust, but! I deleted it as I wanted the form to open to a new record when it was accessed from the Main Menu.
 
Can I open the same form with separate Open Arguments from two different Command Buttons?

Should be able to do so. However, from the code you showed us in the Form_Open routine you are forcing a new record no matter what is in any "OpenArgs" offering. In fact, I don't see an "open args" reference in the Form_Open routine you showed us. The proper way to pass an OpenArgs argument is, from the opener code, the 7th argument of a DoCmd.OpenForm is the OpenArgs (text) argument. In the form being opened, you have something that reads Me.OpenArgs (which is passed as a string). I don't see anything like that here.

You can have a button click for each relevant button, but only one Form_Open routine on the given form.

EDIT: OR I could have mis-interpreted your question slightly in that you didn't really mean "OpenArgs" but different individual opening arguments. My answer stands in this sense: You can have different arguments for the OPENFORM command from multiple places, but the code in your Form_Open routine, if it relates to the relevant form, can never give you anything except a new record because of the DoCmd.GoToRecord that is unconditionally executed. And the way that you WOULD select a specific record (your "required record") WOULD involve "OpenArgs" usage to test whether a record indicator/selector had been passed in as the 7th argument of the .OpenForm command.
 
Last edited:
You can do this...
Code:
Private Sub Form_Open(Cancel As Integer)
    If IsNumeric(Me.OpenArgs) Then
        GoToID CLng(Me.OpenArgs)
    Else
        DoCmd.GoToRecord , , acNewRec
    End If
End Sub
... so if you pass a valid numeric ID in OpenArgs, it navigates to that row, else it adds a row.

But you might also set the Form.DataEntry property equal to True. Then on open it will go to a new record by default, and you can do...
Code:
Private Sub Form_Open(Cancel As Integer)
    If IsNumeric(Me.OpenArgs) Then
        Me.DataEntry = false
        GoToID CLng(Me.OpenArgs)
    End If
End Sub
 
EDIT: OR I could have mis-interpreted your question slightly in that you didn't really mean "OpenArgs" but different individual opening arguments.
Hi @The_Doc_Man

I appreciate your prompt response: Your latter assumption is correct. "fDcust" needs to open with two different Statements/Arguments.

1. Accessing via the "Main Menu" opens to a new record
2. Accessing via the "Search Enquiry" opens to the selected record.
 
Last edited:
Hi @MarkK

Thank you for your help. Awesome workaround.

I will throw both ideas at it and see which runs smoother.

Watch this space.

Regards
Namesake

Edit: Just out of curiosity, I get an error on GoToID.?? Am I meant to replace this with my record field.??
 
Last edited:
do you have Autonumber field on your table?
you need to.
add the autonumber to your search form.

on the click of a button on the search form:

Code:
If Nz(Me.txtAutnumberTextbox, 0) <> 0
    Docmd.OpenForm ""fDcust",,,"[AutonumberFieldName] = " & Me.txtAutonumberTextbox,,acDialog
End If

while on the Main form (adding record)
Code:
    Docmd.OpenForm ""fDcust",,,"[AutonumberFieldName] = 0",,acDialog

the Open event of "fDcust" will be simply:
Code:
Private Sub Form_Open(Cancel As Integer)  
With Me
 .Move Left:=1500, Top:=2000, Width:=12000, Height:=6500
End With
End Sub
 
Hi

I managed to find a workaround; this works as expected without issue.

Code:
strCriteria = "lnLocNo = " & Nz(Me.lnLocNo, 0)
DoCmd.OpenForm "fDcust", WhereCondition:=strCriteria, WindowMode:=acDialog

I have this behind the "Go To" cmdBtn on the "Search" Form, whereas the "Main Menu" cmdBtn just has a standard OpenForm.

Apologies for wasting your time guys. Will happily vote your answers up once I'm allow to.

Cheers
Mark.
 
The OpenForm method has the option to open the form in edit mode or in add mode. Just use the correct argument. No code is necessary.

Add mode:
docmd.OpenForm "formName",acNormal,,,acFormAdd,acDialog,,"openargsvalue"

Edit mode:
docmd.OpenForm "FormName",acNormal,,"CustomerID = " & Me.CustomerID,acFormAdd,acDialog,,"openargsvalue"
 
Hi Pat
I appreciate you spelling out the differences.
I have bookmarked this for future reference.
Thanks again
Mark.
 
There is a lot about Access we all need to know but we tend to learn it piecemeal. Intellisense is a huge help to us as we are writing code. Sure wish I had it when I was learning COBOL 50+ years ago;) Always review all the options for a method each time you use it until you can remember what is available.

You're welcome. Good luck on your journey.
 

Users who are viewing this thread

Back
Top Bottom