Pass a variable back to calling form

L

Linda

I would like to run the code in a form based on the answer in the second
form. On the first form (CDM) the user would select a command button saying
they want to run this option but only for a specific department. The code in
the CDM form would then open a department form (TESTDEPT) where they could
select the department from a list. I want this value saved and passed back
to the the CDM code to finish the process. So far I get the second form to
open and assign the correct value but I can't get the value to go back to the
CDM code. I have tried several different directions. The code below is
where I am at right now. Any help would be greatly appreciated.

In the first form (form_CDM) I have the following code:

Private Sub cmdCANbyDept_Click()
' ask for the dept and then display a query based on that dept
Dim strFilter As String
Dim strDocName As String
DoCmd.OpenForm "testdept", acNormal, , , , acDialog
strFilter = "[department]= '" & strSelectDept & "'"
strDocName = "dstCANs"
DoCmd.OpenForm strDocName, acFormDS, , [strFilter], acFormEdit,
acWindowNormal

End Sub

In the second form (form_testdept) I have the following code:

Option Compare Database
Public strSelectDept As String

Private Sub cmdSelectDept_Click()
strSelectDept = ""
strSelectDept = lstSelectDept.Value
DoCmd.Close

End Sub
 
R

Rick Brandt

Linda said:
I would like to run the code in a form based on the answer in the
second form. On the first form (CDM) the user would select a command
button saying they want to run this option but only for a specific
department. The code in the CDM form would then open a department
form (TESTDEPT) where they could select the department from a list.
I want this value saved and passed back to the the CDM code to finish
the process. So far I get the second form to open and assign the
correct value but I can't get the value to go back to the CDM code.
I have tried several different directions. The code below is where I
am at right now. Any help would be greatly appreciated.

In the first form (form_CDM) I have the following code:

Private Sub cmdCANbyDept_Click()
' ask for the dept and then display a query based on that dept
Dim strFilter As String
Dim strDocName As String
DoCmd.OpenForm "testdept", acNormal, , , , acDialog
strFilter = "[department]= '" & strSelectDept & "'"
strDocName = "dstCANs"
DoCmd.OpenForm strDocName, acFormDS, , [strFilter], acFormEdit,
acWindowNormal

End Sub

In the second form (form_testdept) I have the following code:

Option Compare Database
Public strSelectDept As String

Private Sub cmdSelectDept_Click()
strSelectDept = ""
strSelectDept = lstSelectDept.Value
DoCmd.Close

End Sub

The calling code that opens a dialog form wll pause until the dialog form is
closed or made hidden. So don't close the dialog form. Put a button on it
that only makes it hidden. Then your calling code can get the value from it
and then close it.
 
L

Linda

Rick,

I thought it was something like that but I am new at this and I am not
getting the syntax correct for either hiding the second form or closing the
second form once I get back to the first form. Can you spell it out for me?
Thanks.


Rick Brandt said:
Linda said:
I would like to run the code in a form based on the answer in the
second form. On the first form (CDM) the user would select a command
button saying they want to run this option but only for a specific
department. The code in the CDM form would then open a department
form (TESTDEPT) where they could select the department from a list.
I want this value saved and passed back to the the CDM code to finish
the process. So far I get the second form to open and assign the
correct value but I can't get the value to go back to the CDM code.
I have tried several different directions. The code below is where I
am at right now. Any help would be greatly appreciated.

In the first form (form_CDM) I have the following code:

Private Sub cmdCANbyDept_Click()
' ask for the dept and then display a query based on that dept
Dim strFilter As String
Dim strDocName As String
DoCmd.OpenForm "testdept", acNormal, , , , acDialog
strFilter = "[department]= '" & strSelectDept & "'"
strDocName = "dstCANs"
DoCmd.OpenForm strDocName, acFormDS, , [strFilter], acFormEdit,
acWindowNormal

End Sub

In the second form (form_testdept) I have the following code:

Option Compare Database
Public strSelectDept As String

Private Sub cmdSelectDept_Click()
strSelectDept = ""
strSelectDept = lstSelectDept.Value
DoCmd.Close

End Sub

The calling code that opens a dialog form wll pause until the dialog form is
closed or made hidden. So don't close the dialog form. Put a button on it
that only makes it hidden. Then your calling code can get the value from it
and then close it.
 
R

Rick Brandt

Linda said:
Rick,

I thought it was something like that but I am new at this and I am not
getting the syntax correct for either hiding the second form or
closing the second form once I get back to the first form. Can you
spell it out for me? Thanks.

On the calling form...

DoCmd.OpenForm "testdept", acNormal, , , , acDialog
Me!TextBox = Forms!testdept!TextBoxName
DoCmd.Close acForm, "testdept"

On the button that "appears to" close the second form...

Me.Visible = True
 

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