OpenArgs - passing text depending on what button was presses (1 Viewer)

Danick

Registered User.
Local time
Today, 11:57
Joined
Sep 23, 2008
Messages
351
I have a command button on a form that open a second form and use OpenArgs to pass the ID that was in the first form.

Is it possible to have multiple command buttons on the first form that will open the second form as before, but insert predetermined text (not parameters from first form) in certain fields of the second form?

For instance, there is a text field on the second form that the user will type either "Chat" or "email" or "Call". So instead of having 3 separate forms that will populate that field on load, I'd like to have three buttons that will populate that same form depending on which button is pressed.

Is that possible to pass text instead of parameters to the second form?
Thanks
 
Last edited:

Minty

AWF VIP
Local time
Today, 16:57
Joined
Jul 26, 2013
Messages
10,371
OpenArgs is a string, so you can pass pretty much whatever you like with it.

If you set the open args to
Code:
1|Fred|23/04/2018|234
You can use the Split function to extract the four options separated by | from the string
 

Danick

Registered User.
Local time
Today, 11:57
Joined
Sep 23, 2008
Messages
351
OpenArgs is a string, so you can pass pretty much whatever you like with it.

If you set the open args to
Code:
1|Fred|23/04/2018|234
You can use the Split function to extract the four options separated by | from the string

Thanks but how do I get the different fields in the second form to insert those words in the appropriate fields?

Here is what I have:
On the first form, command button to open second form with the contactID
Code:
DoCmd.OpenForm "frmNewCall", , , , acFormAdd, , Me.ContactID

On load of 2nd form
Code:
Me.ContactID = Me.OpenArgs
Me.Subject.Value = Me.Subject.Value & "Call"

How can I change that so that I can have for example 2 buttons on the first form that will populate the Subject value to either "Call" or "email" depending on which button I press.
 
Last edited:

Minty

AWF VIP
Local time
Today, 16:57
Joined
Jul 26, 2013
Messages
10,371
Lets change your open args to

Code:
DoCmd.OpenForm "frmNewCall", , , , acFormAdd, , Me.ContactID & "|Call"

Then in on load
Code:
Me.[ContactID] = Split(Me.OpenArgs, "|")(0)
Me.[Subject] = Split(Me.OpenArgs, "|")(1)
 

Danick

Registered User.
Local time
Today, 11:57
Joined
Sep 23, 2008
Messages
351
Lets change your open args to

Code:
DoCmd.OpenForm "frmNewCall", , , , acFormAdd, , Me.ContactID & "|Call"

Then in on load
Code:
Me.[ContactID] = Split(Me.OpenArgs, "|")(0)
Me.[Subject] = Split(Me.OpenArgs, "|")(1)

WOW - that was easy. Thank you so much. This works exactly the way I want it to work.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 16:57
Joined
Jul 9, 2003
Messages
16,282
That's not really how OpenArgs was intended to be used!
There's no need to use OpenArgs like this, because there's a much better way.

Code:
Option Compare Database
Option Explicit

Private Sub btnCall_Click()
    Call fOpenForm("Call")
End Sub

Private Sub btnChat_Click()
    Call fOpenForm("Chat")
End Sub

Private Sub btnEmail_Click()
    Call fOpenForm("Email")
End Sub

Private Function fOpenForm(strContType As String)

Dim strFrmName As String
strFrmName = "frmNewCall"

    DoCmd.OpenForm strFrmName, , , , acFormAdd
    With Forms(strFrmName)
        .txtContactID.Value = Me.txtContactID
        .txtSubject.Value = strContType
    End With
    
End Function      'fOpenForm
 

Danick

Registered User.
Local time
Today, 11:57
Joined
Sep 23, 2008
Messages
351
Thanks Uncle Gizmo.
I can see how this would be the "correct" way to do this and looks a whole lot more professional. For me though, modifying two lines of code to my OpenArgs is a whole lot easier than all this Function and VBA. I'll keep this in mind though the next time I have to do something like this.

The only concern I'm having now is why shouldn't I use OpenArgs in this way? Is it just bad policy or can it crash the application, reduce performance, etc,...
 

MarkK

bit cruncher
Local time
Today, 08:57
Joined
Mar 17, 2004
Messages
8,184
I don't know. I don't mind concatenating OpenArgs like Minty does there, and then splitting them out again in the newly opened form. Maybe a matter of taste. The bigger problem is with a form opened acDialog, which pauses your calling code.
Mark
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 11:57
Joined
May 21, 2018
Messages
8,543
That's not really how OpenArgs was intended to be used!
Really? Who says? What was it intended for, if not to pass information to be used in code? It is a pretty common technique used often.

Now as pointed out it is only needed if the form is open dialog. I can not think of a reason to use it otherwise, but maybe there is.
 

Danick

Registered User.
Local time
Today, 11:57
Joined
Sep 23, 2008
Messages
351
I don't know. I don't mind concatenating OpenArgs like Minty does there, and then splitting them out again in the newly opened form. Maybe a matter of taste. The bigger problem is with a form opened acDialog, which pauses your calling code.
Mark

So I'm not really sure what you mean with this.

After one of the buttons is pressed in the first form, the first form is still left open in the background. So I suppose the user can leave the second form opened, go back to the first form, and then select a different button. I tried this and all that happens is the 2nd form gets the focus again, but doesn't update the fields to the new button selected. I can see that could be an issue. I don't really want to make the 2nd form a popup. It's fine for me, but I can see how some may find it a problem (if that's what you mean by having a problem from opened acDialog).
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 16:57
Joined
Jul 9, 2003
Messages
16,282
For me though, modifying two lines of code to my OpenArgs is a whole lot easier than all this Function and VBA.

I don't see anything in my code that is more complicated than your code. In fact I would argue my method is a simpler, easier to understand, as in seeing what it does and it is also self documenting.

From the nature of your comment I assume you haven't written a function?

Therefore you must have 3 command buttons calling a slightly different version of the code three times.

This means if you make any changes, you have to change it in 3 places, generally something to be avoided.

Notice also, my solution does not require any code in the Load Event of the other form. Everything is neatly contained in one form, in one place.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 16:57
Joined
Jul 9, 2003
Messages
16,282
I tried this and all that happens is the 2nd form gets the focus again, but doesn't update the fields to the new button selected.

In my version of the code pressing another button updates the textbox with the new Value associated with the button pressed... I haven't investigated why, but I would assume it's because your method requires the use of the form load event. As you're not actually loading the form again, then the load event is not triggered because the form is already loaded.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 11:57
Joined
May 21, 2018
Messages
8,543
As Uncle Gizmo said, if you are not opening the form in dialog simply manipulate the properties of the open form, because you can. No need for openargs.

If the form is open in dialog, you cannot manipulate it because code execution from the calling form stops until the dialog form in closed. That is the only reason to use open args (unless someone provides a reason). If you need to pass multiple values then use a delimeter so you can split it out. There is no reason to use openargs on a non dialog form. As Gizmo points out it is far easier and efficient to just manipulate the properties.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 11:57
Joined
May 21, 2018
Messages
8,543
If a form is open in Dialog I will argue that openargs is the best method to manipulate form properties. Your other option is to set a public variable ahead oftime, but this is not very encapsulated leading to other problems. If doing this is a more modern environment like vb.net you can instantiate the form, set the properties, and then open it, but Access is pretty primitive.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 11:57
Joined
Feb 19, 2002
Messages
43,305
Never dirty a form with code before the user dirties it by typing.

Regardless of how the arguments are obtained, they should not be applied to form controls prior to the Form's dirty event.

When you dirty a form in the load event or current event, both of which fire before the user takes any action, you can end up creating "empty" records unless you have sufficient code in the form's BeforeUpdate event to prevent the incomplete data from being saved.
 

JHB

Have been here a while
Local time
Today, 17:57
Joined
Jun 17, 2012
Messages
7,732
I have a command button on a form that open a second form and use OpenArgs to pass the ID that was in the first form.
..
I think you should use the event Activate instead, example attached.
 

Attachments

  • Activate.accdb
    456 KB · Views: 34

Users who are viewing this thread

Top Bottom