Passing an Argument back to an open form

G

Guest

I have two forms. Frm_ActMstr and Frm_NewCont. On Frm_ActMstr there are 3
fields that can be populated by frm_NewCnt.Contact_ID. The way it should
work is that when a user double clicks on the blank ContactName1,
ContactName2, or ContactName3 field in FrmActMstr, Frm_NewCont pops open.
They enter the contact information. Then when they close the frm_NewCont,
the Frm_NewCont.Contact_ID should populate the appropriate
frm_ActMstr.ContactID1, frm_ActMstr.ContactID2, or frm_ActMstr.ContactID3
field and frm_ActMstr should refresh.

What I don't understand is how to get the Contact_ID into the appropriate
Contact1, Contact2, or Contact3 field depending on which was originally
double clicked in frm_ActMstr.

Can someone give me a hand here?

Thank you!
 
G

Guest

Emma,

You could use global variables, but I find this technique to be more reliable
Open the Frm_NewCont using "acDialog" as the value for the WindowMode
parameter in the Docmd.openform method. By using this WindowMode, code in
your doubleclick event will be paused, waiting for you to close (or hide)
frm_NewCont.

The critical piece of this is the Hide portion. On frm_NewCont, when they
close the form (close or cancel buttons click event) , don't close it, just
hide it (me.visible = False). This will allow the code in your main form to
continue processing, at which time you can populate the correct control. In
addition to this, to avoid having to enter the same code in more than one
textboxes doubleclick event, I think I would create a subroutine to handle
this for you. Since I normally have a Cancel and a Save or Close button on
forms like this (frm_NewCont), I would also set the forms tag property
(me.Tag = "Cance" or me.Tag = "Saved") so that I can check it from my
subroutine to see whether the addition of the new contact was accomplished or
cancelled. It and the associated code might look like:

Private Sub txt_ContactName1_dblClick(Cancel as integer)

'Pass the textbox control to the subroutine
Call GetNewContact(me.txt_ContactName1)

End Sub

Private Sub GetNewContact(ctrl as textbox)

docmd.OpenForm "frm_NewCont",,,,, acDialog

'Check to make sure the form is still loaded.
if currentproject.allforms("frm_NewCont").IsLoaded = False then exit sub

'Check to see if the new contact was cancelled
if forms("frm_NewCont").Tag = "Cancel" then Exit Sub

'If not cancelled, then the new contact was saved
ctrl.text = forms("frm_NewCont").Controls("controlName").Value

'Close frm_NewCont
docmd.close acform, "frm_NewCont")

End Sub

HTH
Dale
 
M

Morris

What I don't understand is how to get the Contact_ID into the appropriate
Contact1, Contact2, or Contact3 field depending on which was originally
double clicked in frm_ActMstr.

Can someone give me a hand here?


What I don't understand is how to get the Contact_ID into the appropriate
Contact1, Contact2, or Contact3 field depending on which was originally
double clicked in frm_ActMstr.

Can someone give me a hand here?

Don't know if that's the best practice, but I would do it this way:

1) when opening Frm_NewCont I would pass the name of the control that
has been clicked (Contact1, Contact2 or Contact3) as OpenArgs
argument, then assign it to something: strInvokingControl =
Me.OpenArgs

2) when closing the form Frm_NewCont I would first hide it with:
Me.Visible = False, then
3) I would populate the control on the form Frm_ActMstr:
Forms("ActMstr").controls(strInvokingControl) = lngSomePopulatedID

I hope that's enough to explain my idea, if not, let me know

Morris
 
G

Guest

Hi Dale,

Thank you for your reply. However, the contact information is still not
populating in Frm_ActMster. Frm_NewContact opens great and I put the Save
and Cancel button on it and that works wonderfully also. But when the form
closes or gets hidden in this case, no contact information appears.

I've edited your code slightly because I am actually trying to pass the
frm_NewCont.Contact_ID to a hidden text box on frm_ActMstr.BPV_Contact3_ID
whose control source is =[cbo_bpv_contact_3].[column](0) and
Frm_ActMstr.ContactName3 is actually a combo box.

So here is the code for GetNewcContact:

Private Sub GetNewContact(ctrl As TextBox)

DoCmd.OpenForm "frm_NewContacts", , , , acFormAdd, acDialog

'Check to make sure the form is still loaded.
If CurrentProject.AllForms("frm_NewContacts").IsLoaded = False Then Exit
Sub

'Check to see if the new contact was cancelled
If Forms("frm_NewContacts").Tag = "Cancel" Then Exit Sub

'If not cancelled, then the new contact was saved
ctrl.Text = Forms("frm_NewContacts").Controls("Contact_ID").Value

'Close frm_NewCont
DoCmd.Close acForm, "frm_NewContacts"

End Sub

Then in Frm_ActMstr I have this on the double click even of the combo box:

Private Sub CBO_BPV_CONTACT_3_DblClick(Cancel As Integer)
On Error GoTo Err_cbo_BPV_Contact_3_DblClick

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "Frm_NewContacts"
stLinkCriteria = "[Contact_ID] =" & Me![BPV_Contact3_ID]

If IsNull(Me!BPV_Contact3_ID) Then

Call GetNewContact(Me.BPV_Contact3_ID)

Else

DoCmd.OpenForm stDocName, acNormal, , stLinkCriteria

End If

Exit_cbo_BPV_Contact_3_DblClick:
Exit Sub

Err_cbo_BPV_Contact_3_DblClick:
msgbox Err.Description
Resume Exit_cbo_BPV_Contact_3_DblClick
End Sub
 
G

Guest

So in the double_click event of the blank cbo_ContactName1, (I don't want to
pass the contact_name but rather the contact_ID which is in a hidden text box
on frm_ActMstr). I have an if statement because if the contact name is blank
(null OpenArg value) I want to add a contact, if it is not, and the user
double clicks I want them to be able to edit the contact as follows? I think
this is how it goes:

Private Sub CBO_BPV_CONTACT_3_DblClick(Cancel As Integer)
On Error GoTo Err_cbo_BPV_Contact_3_DblClick


Dim stDocName As String
Dim stLinkCriteria As String
Dim MyContactOpenArgs As String

MyContactOpenArgs = Me!BPV_Contact3_ID

stDocName = "Frm_NewContacts"
stLinkCriteria = "[Contact_ID] =" & Me![BPV_Contact3_ID]

If IsNull(MyOpenArgs) Then

DoCmd.OpenForm stDocName, acNormal, , , acFormAdd, acDialog,
MyContactOpenArgs

Else

DoCmd.OpenForm stDocName, acNormal, , stLinkCriteria, acFormEdit,
acDialog

End If

Exit_cbo_BPV_Contact_3_DblClick:
Exit Sub

Err_cbo_BPV_Contact_3_DblClick:
msgbox Err.Description
Resume Exit_cbo_BPV_Contact_3_DblClick
End Sub

Then what? Do I use the close event of frm_newContact and assign the new
value from Frm_NewContact.Contact_ID to MyContactOpenArgs?

Private Sub Form_Close()

Me.Visible = False

Forms("SSBPVBioAccountMaster").Controls(MyContactOpenArgs) = Me.Contact_ID

If CurrentProject.AllForms("frmssbpvbioaccountmaster").IsLoaded Then

Forms!frm_actmstr.cbo_bpv_Contact_3.Requery

End If

End Sub
 
D

Dale Fye

Emma,

Two potential problems.

1. You initially indicated that the controls you were double clicking in
were text boxes, so in the GetNewContact subroutine, I made that assumption.
If these are in fact combo boxes, that changes things a little.

Private Sub GetNewContact(ctrl As ComboBox)

DoCmd.OpenForm "frm_NewContacts", , , , acFormAdd, acDialog

'Check to make sure the form is still loaded.
If CurrentProject.AllForms("frm_NewContacts").IsLoaded = False Then Exit
Sub

'Check to see if the new contact was cancelled
If Forms("frm_NewContacts").Tag = "Cancel" Then Exit Sub

'If not cancelled, then the new contact was saved
ctrl.requery
ctrl.value = Forms("frm_NewContacts").Controls("Contact_ID").Value

'Close frm_NewCont
DoCmd.Close acForm, "frm_NewContacts"

End Sub

2. Because you have provided two ways for your program to get to the
NewContacts form, have you checked which method is actually being used?
Insert a breakpoint in both branches of the If statement to make sure that
the method being used is the GetNewContact subroutine.

HTH
Dale

Emma Aumack said:
Hi Dale,

Thank you for your reply. However, the contact information is still not
populating in Frm_ActMster. Frm_NewContact opens great and I put the Save
and Cancel button on it and that works wonderfully also. But when the
form
closes or gets hidden in this case, no contact information appears.

I've edited your code slightly because I am actually trying to pass the
frm_NewCont.Contact_ID to a hidden text box on frm_ActMstr.BPV_Contact3_ID
whose control source is =[cbo_bpv_contact_3].[column](0) and
Frm_ActMstr.ContactName3 is actually a combo box.

So here is the code for GetNewcContact:

Private Sub GetNewContact(ctrl As TextBox)

DoCmd.OpenForm "frm_NewContacts", , , , acFormAdd, acDialog

'Check to make sure the form is still loaded.
If CurrentProject.AllForms("frm_NewContacts").IsLoaded = False Then
Exit
Sub

'Check to see if the new contact was cancelled
If Forms("frm_NewContacts").Tag = "Cancel" Then Exit Sub

'If not cancelled, then the new contact was saved
ctrl.Text = Forms("frm_NewContacts").Controls("Contact_ID").Value

'Close frm_NewCont
DoCmd.Close acForm, "frm_NewContacts"

End Sub

Then in Frm_ActMstr I have this on the double click even of the combo box:

Private Sub CBO_BPV_CONTACT_3_DblClick(Cancel As Integer)
On Error GoTo Err_cbo_BPV_Contact_3_DblClick

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "Frm_NewContacts"
stLinkCriteria = "[Contact_ID] =" & Me![BPV_Contact3_ID]

If IsNull(Me!BPV_Contact3_ID) Then

Call GetNewContact(Me.BPV_Contact3_ID)

Else

DoCmd.OpenForm stDocName, acNormal, , stLinkCriteria

End If

Exit_cbo_BPV_Contact_3_DblClick:
Exit Sub

Err_cbo_BPV_Contact_3_DblClick:
msgbox Err.Description
Resume Exit_cbo_BPV_Contact_3_DblClick
End Sub

--
www.bardpv.com
Tempe, Arizona


Dale Fye said:
Emma,

You could use global variables, but I find this technique to be more
reliable
Open the Frm_NewCont using "acDialog" as the value for the WindowMode
parameter in the Docmd.openform method. By using this WindowMode, code
in
your doubleclick event will be paused, waiting for you to close (or hide)
frm_NewCont.

The critical piece of this is the Hide portion. On frm_NewCont, when
they
close the form (close or cancel buttons click event) , don't close it,
just
hide it (me.visible = False). This will allow the code in your main form
to
continue processing, at which time you can populate the correct control.
In
addition to this, to avoid having to enter the same code in more than one
textboxes doubleclick event, I think I would create a subroutine to
handle
this for you. Since I normally have a Cancel and a Save or Close button
on
forms like this (frm_NewCont), I would also set the forms tag property
(me.Tag = "Cance" or me.Tag = "Saved") so that I can check it from my
subroutine to see whether the addition of the new contact was
accomplished or
cancelled. It and the associated code might look like:

Private Sub txt_ContactName1_dblClick(Cancel as integer)

'Pass the textbox control to the subroutine
Call GetNewContact(me.txt_ContactName1)

End Sub

Private Sub GetNewContact(ctrl as textbox)

docmd.OpenForm "frm_NewCont",,,,, acDialog

'Check to make sure the form is still loaded.
if currentproject.allforms("frm_NewCont").IsLoaded = False then exit
sub

'Check to see if the new contact was cancelled
if forms("frm_NewCont").Tag = "Cancel" then Exit Sub

'If not cancelled, then the new contact was saved
ctrl.text = forms("frm_NewCont").Controls("controlName").Value

'Close frm_NewCont
docmd.close acform, "frm_NewCont")

End Sub

HTH
Dale


--
Don''t forget to rate the post if it was helpful!

Email address is not valid.
Please reply to newsgroup only.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top