passing information between forms

C

Cox Newsgroups

Is it possible to programatically open a form from another form, then have
the code wait until the 2nd form closed, passing back a value.

Example:

I have a form with a listbox of clients.
Normally, a user will click a clients name in the listbox,
then choose a command button to log a client event, which would open the
client event form.

However, if the user tries to log an event without first choosing a
client, they are warned that they need to choose a client first.. or would
they like to add a new client.

If they click YES, to add a client, I want to open a customer form, allowing
the new entry. Upon saving the new customer, I would like customer form to
close, opening the log event form with the new customer ID attached.

In order for this to happen, I need the addCustomer form to pass the new
customersID to one of the forms, preferrably to the customerlist form with
the listbox of names. I sat this because my customer event form is based on
the value of the listbox in that form.

Does this make sense to you gurus?
 
N

NKTower

Here's how I would do it. There may be a simpler way, but
I'd rather have it easy to follow.

In your "main" form, create an unbound text control.
In this example, it is named
txt_HiddenNewCustomerID
and (after testing) set the VISIBLE property = FALSE

Add the following code. I have "wrapped" source
with continues (trailing _ ) so that it will fit
the forum display area.

' ------------------------------------
Option Compare Database
Option Explicit

Private Sub cmd_Open_Customer_Form_Click()
Dim str_OpenArgs As String
' Clear previous value, if any
Me.txt_HiddenNewCustomerID.Value = Null
' Tell form who called it
str_OpenArgs = "Callback_MAIN"
Debug.Print "Opening Customer Form"
DoCmd.OpenForm "Customer Form", acNormal, , , , _
acDialog, str_OpenArgs
If Not IsNull(Me.txt_HiddenNewCustomerID.Value) Then
Debug.Print "back in MAIN with " & _
Me.txt_HiddenNewCustomerID.Value
' requery the combo box
' Make use of txt_HiddenNewCustomerID as needed
End If
End Sub

' ------------------

In the Customer Form, add code like this
' -----------------
Option Compare Database
Option Explicit

Private Sub Form_Close()

If Me.OpenArgs = "Callback_MAIN" Then
If CurrentProject.AllForms("Main").IsLoaded Then
Debug.Print "@Form Customer Form.Close event"
Forms![Main].Form!txt_HiddenNewCustomerID.Value = _
Me.txt_Add_This_Customer_ID
End If
End If
End Sub
' -----------------

What makes it work...

In the "MAIN" form, which I have called MAIN
a) the acDialog in the FormOpen event passes cntrol to the Customer Form
such that execution of code in the calling routine will be suspended until
the Customer Form is closed.

b) Using OpenArgs is a way of telling the Customer Form "who called it".
This is so that we can determine whether we need to execute some code in the
Form_Close event of Customer Form. I presume that other places in your
application may make use of Customer Form as well, and we only want to
execute the special code if called from what I'm calling MAIN.

In the "Customer Form" which is used to Add/Change/Delete customers, etc.
a) In the Form_Close event
1) Take a look at Me.OpenArgs to see where we came from. You could make
use of a SELECT statement here if you have different things to do dependng
upn where yu came from.

2) Probably redundant, but also probably prudent - make sure the
calling form is still open

3) poke the new value into the hidden control in the main form.
Note: This example doesn't detect that it is a 'new' customer, that's up
to you.

Back in the MAIN form, upon return
a) test to see if the Customer Form poked a value into the hidden control.
If so, make use of it

b) remember to requery the combo box if necessary

This is what is displayed in the debug trace...
Opening Customer Form
@Form Customer Form.Close event
back in MAIN with abcde
 
J

John Ingato

Thanks NK. That helped alot.

NKTower said:
Here's how I would do it. There may be a simpler way, but
I'd rather have it easy to follow.

In your "main" form, create an unbound text control.
In this example, it is named
txt_HiddenNewCustomerID
and (after testing) set the VISIBLE property = FALSE

Add the following code. I have "wrapped" source
with continues (trailing _ ) so that it will fit
the forum display area.

' ------------------------------------
Option Compare Database
Option Explicit

Private Sub cmd_Open_Customer_Form_Click()
Dim str_OpenArgs As String
' Clear previous value, if any
Me.txt_HiddenNewCustomerID.Value = Null
' Tell form who called it
str_OpenArgs = "Callback_MAIN"
Debug.Print "Opening Customer Form"
DoCmd.OpenForm "Customer Form", acNormal, , , , _
acDialog, str_OpenArgs
If Not IsNull(Me.txt_HiddenNewCustomerID.Value) Then
Debug.Print "back in MAIN with " & _
Me.txt_HiddenNewCustomerID.Value
' requery the combo box
' Make use of txt_HiddenNewCustomerID as needed
End If
End Sub

' ------------------

In the Customer Form, add code like this
' -----------------
Option Compare Database
Option Explicit

Private Sub Form_Close()

If Me.OpenArgs = "Callback_MAIN" Then
If CurrentProject.AllForms("Main").IsLoaded Then
Debug.Print "@Form Customer Form.Close event"
Forms![Main].Form!txt_HiddenNewCustomerID.Value = _
Me.txt_Add_This_Customer_ID
End If
End If
End Sub
' -----------------

What makes it work...

In the "MAIN" form, which I have called MAIN
a) the acDialog in the FormOpen event passes cntrol to the Customer Form
such that execution of code in the calling routine will be suspended until
the Customer Form is closed.

b) Using OpenArgs is a way of telling the Customer Form "who called it".
This is so that we can determine whether we need to execute some code in
the
Form_Close event of Customer Form. I presume that other places in your
application may make use of Customer Form as well, and we only want to
execute the special code if called from what I'm calling MAIN.

In the "Customer Form" which is used to Add/Change/Delete customers, etc.
a) In the Form_Close event
1) Take a look at Me.OpenArgs to see where we came from. You could make
use of a SELECT statement here if you have different things to do dependng
upn where yu came from.

2) Probably redundant, but also probably prudent - make sure the
calling form is still open

3) poke the new value into the hidden control in the main form.
Note: This example doesn't detect that it is a 'new' customer, that's up
to you.

Back in the MAIN form, upon return
a) test to see if the Customer Form poked a value into the hidden control.
If so, make use of it

b) remember to requery the combo box if necessary

This is what is displayed in the debug trace...
Opening Customer Form
@Form Customer Form.Close event
back in MAIN with abcde



Cox Newsgroups said:
Is it possible to programatically open a form from another form, then
have
the code wait until the 2nd form closed, passing back a value.

Example:

I have a form with a listbox of clients.
Normally, a user will click a clients name in the listbox,
then choose a command button to log a client event, which would open the
client event form.

However, if the user tries to log an event without first choosing a
client, they are warned that they need to choose a client first.. or
would
they like to add a new client.

If they click YES, to add a client, I want to open a customer form,
allowing
the new entry. Upon saving the new customer, I would like customer form
to
close, opening the log event form with the new customer ID attached.

In order for this to happen, I need the addCustomer form to pass the new
customersID to one of the forms, preferrably to the customerlist form
with
the listbox of names. I sat this because my customer event form is based
on
the value of the listbox in that form.

Does this make sense to you gurus?
 

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