More Intelligent Form... Is There a Simple Way?

G

Guest

Having a couple related problems with some VB code I inherited.
1) I have a form (frmCALLED) that is called from two different forms
(frmSOURCE1 and frmSOURCE2) within a database.
2) This same form (frmCALLED) when exiting opens frmSOURCE1 even when
frmSOURCE2 was the calling form and should be returned to that form.

frmCALLED works just fine when called from frmSOURCE1, but when called by
frmSOURCE2 it errors out. I believe that it is in the code behind frmCALLED
where it is trying to populate a VALUE from frmSOURCE1 and the error happens
when it is called by frmSOURCE2 because the frmSOURCE1.user.VALUE does not
exist, but frmSOURCE2.user.value does exist.

*********************** SAMPLE CODE ********************

Private Sub Form_Load()

User.Value = Forms![frmSOURCE1]![User]

End Sub


Private Sub cmdSubmit_Click()

On Error GoTo Err_cmdSubmit_Click
Submitted.Value = True
DoCmd.Close

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frmSOURCE1"
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_cmdSubmit_Click:
Exit Sub

Err_cmdSubmit_Click:
MsgBox Err.Description
Resume Exit_cmdSubmit_Click

End Sub


Private Sub cmdExit_Click()

On Error GoTo Err_cmdExit_Click

DoCmd.Close
DoCmd.OpenForm "frmSOURCE1"

Exit_cmdExit_Click:
Exit Sub

Err_cmdExit_Click:
MsgBox Err.Description
Resume Exit_cmdExit_Click

End Sub

********************* END OF SAMPLE ******************

While I could replicate the form and its associated code and rename it for
the specific form/function it is responding to, I would like to make the one
instance of this code "more intelligent" and able to handle multiple source
calls provided the variable User.Value is available from the new source
subroutine.

In a nutshell, is there a simple way to make this code "smart" enough to know:
A) WHICH frmSOURCE# called it;
B) To gather the correct frmSOURCE#.User.VALUE; and
C) To return to the correct frmSOURCE# on exit?

Thank you in advance for your support and assistance.

Sincerely,
MJ
 
D

Douglas J. Steele

When you use DoCmd.OpenForm, you have the option of setting an OpenArgs
value: it's essentially a "freebie" that you can populate with whatever you
want. If you pass the name of the calling form, you can then use that in the
form you opened.

Use something like:

DoCmd.OpenForm "frmCalled", OpenArgs:=Me.Name

In the Load event of frmCalled, you can have code like:

Private Sub Form_Load()

If IsNull(Me.OpenArgs) = False Then
User.Value = Forms(Me.OpenArgs)![User]
End If

End Sub

I'm not sure why you're opening another form when you close frmCalled. Did
you close the calling form? If not, you can simply set focus to it.
 

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