Determine which form opened the current form

D

David

I would like to determine which form opened a form so I can run code.

For example:

I would like my TicketCheckIn form to execute certain code only when it was
opened by the InvoiceEdit form. What code can I use to determine which
form opened a form?
 
6

'69 Camaro

Hi, David.
I would like my TicketCheckIn form to execute certain code only when it
was opened by the InvoiceEdit form. What code can I use to determine
which form opened a form?

Use the OpenArgs( ) method. In the InvoiceEdit form, use the following code
to open the TicketCheckIn form:

DoCmd.OpenForm "TicketCheckIn", , , , , , "InvoiceEdit"

In the TicketCheckIn form, use the following code in this form's OnOpen( )
event, modify the example MsgBox to use the code you wish, then save and
compile:

Private Sub Form_Open(Cancel As Integer)

On Error GoTo ErrHandler

If (Len(Me.OpenArgs) > 0) Then
If (Me.OpenArgs = "InvoiceEdit") Then
MsgBox "Opened by InvoiceEdit." ' <-- Modify this part.
End If
End If

Exit Sub

ErrHandler:

MsgBox "Error in Form_Open( ) in" & vbCrLf & _
Me.Name & " form." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & vbCrLf & Err.Description
Err.Clear

End Sub

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips and tutorials.
Blog: http://DataDevilDog.BlogSpot.com
http://www.Access.QBuilt.com/html/expert_contributors2.html for contact
info.
 
D

Dirk Goldgar

'69 Camaro said:
Hi, David.


Use the OpenArgs( ) method. In the InvoiceEdit form, use the
following code to open the TicketCheckIn form:

DoCmd.OpenForm "TicketCheckIn", , , , , , "InvoiceEdit"

In the TicketCheckIn form, use the following code in this form's
OnOpen( ) event, modify the example MsgBox to use the code you wish,
then save and compile:

Private Sub Form_Open(Cancel As Integer)

On Error GoTo ErrHandler

If (Len(Me.OpenArgs) > 0) Then
If (Me.OpenArgs = "InvoiceEdit") Then
MsgBox "Opened by InvoiceEdit." ' <-- Modify this part.
End If
End If

Exit Sub

ErrHandler:

MsgBox "Error in Form_Open( ) in" & vbCrLf & _
Me.Name & " form." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & vbCrLf & Err.Description
Err.Clear

End Sub

Another possibility is to use the fact that, in a form's Open event, it
still isn't yet the active form, as far as Access is concerned. So you
can have code like this:

'----- start of example code -----
Private Sub Form_Open(Cancel As Integer)

Dim strPreviousForm As String

On Error Resume Next
strPreviousForm = Screen.ActiveForm.Name
On Error GoTo ErrHandler

If strPreviousFormName = "InvoiceEdit" Then
MsgBox "Opened by InvoiceEdit."
End If

Exit_Point:
Exit Sub

ErrHandler:

MsgBox "Error in Form_Open( ) in" & vbCrLf & _
Me.Name & " form." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & vbCrLf & Err.Description

Resume Exit_Point

End Sub

'----- end of example code -----
 
D

David

Thanks guys! Both are working great!


Dirk Goldgar said:
Another possibility is to use the fact that, in a form's Open event, it
still isn't yet the active form, as far as Access is concerned. So you
can have code like this:

'----- start of example code -----
Private Sub Form_Open(Cancel As Integer)

Dim strPreviousForm As String

On Error Resume Next
strPreviousForm = Screen.ActiveForm.Name
On Error GoTo ErrHandler

If strPreviousFormName = "InvoiceEdit" Then
MsgBox "Opened by InvoiceEdit."
End If

Exit_Point:
Exit Sub

ErrHandler:

MsgBox "Error in Form_Open( ) in" & vbCrLf & _
Me.Name & " form." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & vbCrLf & Err.Description

Resume Exit_Point

End Sub

'----- end of example code -----

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 

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