Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return value from form called from another form 1

Status
Not open for further replies.

mdav2

Programmer
Aug 22, 2000
363
GB
What am I trying to acheive? I want to call a search form (Form B) from another form (Form A). Basically the user chooses a value in form B which is then returned to textbox in Form A.

I can think of a couple of ways of doing this:

1) Have a public variable that is set by Form B then Form A picks this value up when Form B is closed

2) Have form B populate the textbox directly in form A before closing.

I am not fond of these methods but can't find anything that resembles the good old fashioned return variable. I would be making the call to the search screen in various places so it would be a generic form (favours option 1). I am sure I have done this a different way but its years since I wrote any VBA so I've probably forgotten it.

Thanks in advance.

Mark.

Mark Davies
Warwickshire County Council
 

You could call a function that opens the second form and, using DoEvents, pause the function unti the form
closes. The function can then return the value you want, but I think you'll still have to store it in a public
variable somewhere.


Randy
 
How are ya mdav2 . . .

[blue]randy700[/blue] has the Idea only I'd open Form B [blue]Modal[/blue]. This way you don't need [DoEvents] and the code in Form A stops running until you close Form B.

If you use the last arguement (OpenArgs) of DoCmd.OpenForm to send the calling formName & ControlName, Form B can directly update the textbox in Form A.

[blue]Your Thoughts?[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
As shown there are several ways to do it. This is the one I use. I make all my Pop up forms dialog with an "OK" and "Cancel" button. "Cancel" closes the form, "Ok" hides the form (invisible) but still open.

Here are the order of events
1. calling form opens the pop up form in dialog
(code execution stop in the calling form because a dialog form is open)
2. after the user selects Ok or Cancel execution returns to calling form
3. Check to see if the calling form is loaded (it is hidden). If it is loaded then you can grab any value off the form.
4. If it is loaded then get the value and close the hidden form. If it is not loaded then the user must have hit cancel and there is nothing to do.

So something like from the calling form:

Code:
Private Sub Command0_Click()
  Dim strFrm As String
  strFrm = "frmPopUp"
  DoCmd.OpenForm strFrm, , , , , acDialog
  If CurrentProject.AllForms(strFrm).IsLoaded Then
    'set value
    Me.Text1 = Forms(strFrm).Text0
    DoCmd.Close acForm, strFrm
  End If
End Sub
[code]

from the pop up
[code]
Private Sub cmdCancel_Click()
  DoCmd.Close acForm, Me.Name
End Sub

Private Sub cmdOK_Click()
  Me.Visible = False
End Sub
 
Thanks for all the responses, pity Access doesn't have the ability to return values like I have used in other languages. It also is a pain that you can only pass one opening parameter (excluding the filter) in OPENARGS.

Have come up with a solution to make the calling form a function. In the OPEANARGS I pass the form name and the control you want to populate. Bit messy having to split the openargs but it works and I am happy.

Mark Davies
Warwickshire County Council
 
It also is a pain that you can only pass one opening parameter

You can bodge up a delimited string to hold two parameters:
Code:
Private Sub Form_Load()
  Private Const DELIMITER As String = ":"
  Dim strPara1 As String
  Dim strPara2 As String

  intPos = InStr(Me.OpenArgs, DELIMITER)
  strPara1 = Left$(Me.OpenArgs, intPos - 1)
  strPara2 = Mid$(Me.OpenArgs, intPos + 1)
End Sub
and with a bit more work you could make it handle any number of parameters.

pity Access doesn't have the ability to return values like I have used in other languages

If you're the same Mark Davies as posts in the Fox forum then I agree wholeheartedly and add that it's so easy to wrap multiple return values into a VFP object too.

Geoff Franklin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top