Using doCmd.openform and a Global variable in place of form name

smercer

Registered User.
Local time
Tomorrow, 00:34
Joined
Jun 14, 2004
Messages
442
Hi all

I am trying to open the same popup dialog box form from different forms for the same purpose(Print confirmation), and I don't want to copy the same form and use different copies for different forms.

The way I would like to get around this is to use a global variable and set the variable according to the form that opened the popup and when user clicks cancel the dialog form will close and the corresponding form in Print preview to close and the original form that opened these to reopen in normal mode


The variable in question is Previous_Form
Here is my code for a button in the control panel form:
Code:
Private Sub cmd_Print_Book_list_Click()
'Summery:
'This will print the form "frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup"

'*********************************************************************************************************

DoCmd.OpenForm "frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup", acNormal, , , acHidden 'this will open the form "frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup" in print preview
Form_frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup.OnClose = ""
Form_sfrm_Books_Descriptions_in_Store_Datasheet_Popup.OnLoad = ""
DoCmd.OpenForm "frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup", acPreview, , , acFormReadOnly
DoCmd.Maximize ' this will maximize "frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup"
DoCmd.OpenForm "dlg_print_conferm", acNormal, , , acFormReadOnly 'this will open the print conferm dialog box
Previous_Form = """frm_Control_Panel"""
DoCmd.Close acForm, "frm_Control_Panel", acSaveNo 'this will close form "frm_Control_Panel"
End Sub

Here is the code in dialog box:
Code:
Sub cmdCancel_Click()

DoCmd.Close acForm, "dlg_print_conferm", acSaveNo
DoCmd.Close acForm, "Form_frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup", acSaveNo
DoCmd.OpenForm Previous_Form, acNormal, , , acFormEdit, acNormal
DoCmd.Maximize

End Sub
[B]'__________________________________________[/B]
Private Sub cmdOK_Click()
On Error GoTo NoPrinter
DoCmd.Close acForm, "dlg_print_conferm", acSaveNo
    
With Form_frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup
    DoCmd.PrintOut acPrintAll, , , acDraft, 1, True
End With

DoCmd.Close acForm, "Form_frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup", acSaveNo

DoCmd.OpenForm Previous_Form, acNormal, , , acFormReadOnly, acNormal
DoCmd.Maximize
Exit Sub
NoPrinter:
MsgBox "You do not have your printer ready"
DoCmd.Close acForm, "Form_frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup", acSaveNo
DoCmd.OpenForm Previous_Form, acNormal, , , acFormReadOnly, acNormal
DoCmd.Maximize
End Sub

Does any one have any ideas?

Thanks in advance
 
Your object names are so long plus all the underscores gave me a headache so I confess, I didn't look at the code.

You can use the OpenArgs argument of the OpenForm method to pass the name of the current form. That way the called form will know which form to "return" to.

DoCmd.OpenForm "yourform",...some number of commas, Me

Me is the current form and the OpenArgs is the last argument. I didn't want to open Access to verify the number of commas necessary.
 
Pat Hartman said:
You can use the OpenArgs argument of the OpenForm method to pass the name of the current form. That way the called form will know which form to "return" to.

Couldn't work out the OpenArgs but then had the idea of using a if statement and have the doCmd.openform and variable in that.

Thanks for helping pat, I got it working.
 
Rich said:
You're going to run into problems with printing the Forms too

Hi Rich

I would be interested why I would have problems

I have changed the coding to this(in dialog box):

Code:
Sub cmdCancel_Click()

DoCmd.Close acForm, "dlg_print_conferm", acSaveNo
DoCmd.Close acForm, "frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup", acSaveNo

If Previous_Form = "frm_Control_Panel" Then
    DoCmd.OpenForm "frm_Control_Panel", acNormal, , , acFormReadOnly, acNormal
    DoCmd.Close acForm, "frm_Book_Entry_form", acSaveNo
    
    Else
    If Previous_Form = "frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup" Then
    DoCmd.OpenForm "frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup", acNormal, , , , acWindowNormal 'will open the datasheet "frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup" into a new window
    Form_sfrm_Record_selected_FormView.RecordSource = "qry_Popup_Record_Selected" 'will change source in sfrm_Record_selected_FormView to "qry_Popup_Record_Selected"
    Form_sfrm_Books_Descriptions_in_Store_subform_DatasheetView.Visible = False 'will hide the form "sfrm_Books_Descriptions_in_Store_subform_DatasheetView"

    Form_frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup.OnClose = "[Event Procedure]"
    Form_frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup.OnLoad = "[Event Procedure]"
    End If
End If

DoCmd.Maximize

End Sub

Private Sub cmdOK_Click()
On Error GoTo NoPrinter
DoCmd.Close acForm, "dlg_print_conferm", acSaveNo
    
With Form_frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup
    DoCmd.PrintOut acPrintAll, , , acDraft, 1, True
End With

DoCmd.Close acForm, "Form_frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup", acSaveNo

If Previous_Form = "frm_Control_Panel" Then
    DoCmd.OpenForm "frm_Control_Panel", acNormal, , , acFormReadOnly, acNormal
    DoCmd.Maximize
    
    Else
    If Previous_Form = "frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup" Then
    DoCmd.OpenForm "frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup", acNormal, , , , acWindowNormal 'will open the datasheet "frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup" into a new window
    Form_sfrm_Record_selected_FormView.RecordSource = "qry_Popup_Record_Selected" 'will change source in sfrm_Record_selected_FormView to "qry_Popup_Record_Selected"
    Form_sfrm_Books_Descriptions_in_Store_subform_DatasheetView.Visible = False 'will hide the form "sfrm_Books_Descriptions_in_Store_subform_DatasheetView"

    Form_frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup.OnClose = "[Event Procedure]"
    Form_frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup.OnLoad = "[Event Procedure]"
    End If
End If

Exit Sub
NoPrinter:
MsgBox "You do not have your printer ready"
DoCmd.Close acForm, "Form_frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup", acSaveNo
If Previous_Form = "frm_Control_Panel" Then
    DoCmd.OpenForm "frm_Control_Panel", acNormal, , , acFormReadOnly, acNormal
    DoCmd.Maximize
    
    Else
    If Previous_Form = "frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup" Then
    DoCmd.OpenForm "frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup", acNormal, , , , acWindowNormal 'will open the datasheet "frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup" into a new window
    Form_sfrm_Record_selected_FormView.RecordSource = "qry_Popup_Record_Selected" 'will change source in sfrm_Record_selected_FormView to "qry_Popup_Record_Selected"
    Form_sfrm_Books_Descriptions_in_Store_subform_DatasheetView.Visible = False 'will hide the form "sfrm_Books_Descriptions_in_Store_subform_DatasheetView"

    Form_frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup.OnClose = "[Event Procedure]"
    Form_frm_Nest_frm_Books_Descriptions_in_Store_DatasheetView_Popup.OnLoad = "[Event Procedure]"
    End If
End If

End Sub

I have not tried it yet with the printer because I do not want to waste paper and ink.

I assume you are referring to printing the wrong form, is that correct?

Thanks Rich for your advice :)
 
No, printing any Form is not recommended, Reports are designed to be printed, forms aren't
 

Users who are viewing this thread

Back
Top Bottom