If Statement for Close of Form

  • Thread starter Thread starter Dave Elliott
  • Start date Start date
D

Dave Elliott

I have a form named BlankChecks that I need to make sure that the user does
not close the form without printing the check.
The form has a Customers field that cant be null, it should key on this, if
there is a payee(i.e. Customer) then it has to be printed.
It also has a command close button and a command print button on it.
How can i not allow them to close the form if a payee has been entered in
the control (Customers) and they have not printed the check?
Command button for printing is named Command113 and Command button for
Close is named Command2128

Thanks,

Dave
 
I have a form named BlankChecks that I need to make sure that the user does
not close the form without printing the check.
The form has a Customers field that cant be null, it should key on this, if
there is a payee(i.e. Customer) then it has to be printed.
It also has a command close button and a command print button on it.
How can i not allow them to close the form if a payee has been entered in
the control (Customers) and they have not printed the check?
Command button for printing is named Command113 and Command button for
Close is named Command2128

Thanks,

Dave

Untested...

Add a Global variable to the Form's Code sheet (up in the Declarations
part of the code).

Option Compare Database
Option Explicit
Dim intPrinted as Integer

Code the Command Print button click event:
DoCmd.OpenReport "ReportName" etc.
intPrinted = 1

Code the form's Currrent Event:
intPrinted = 0

Code the form's Unload event:

If intPrint = 0 And IsNull([PayeeName]) Then
MsgBox "You must print the check"
Cancel = True
End If

Note: There is no guaranty that the check has actually been
successfully printed, only that you sent it to the printer.
 
Fred, tried the code, but it did not stop the form from closing or warn me
of not printing the check.
I Must be doing something wrong. The form has to have a check number and a
customer, i.e. Payee
The control for Payee is named Customers which is a drop down list
I put this code on unload of form and also on the click event of the close
command button.
Suggestions?

If intPrint = 0 And IsNull([Customers]) Then
MsgBox "Check(s)Must Be Printed"
Cancel = True
End If




fredg said:
I have a form named BlankChecks that I need to make sure that the user
does
not close the form without printing the check.
The form has a Customers field that cant be null, it should key on this,
if
there is a payee(i.e. Customer) then it has to be printed.
It also has a command close button and a command print button on it.
How can i not allow them to close the form if a payee has been entered in
the control (Customers) and they have not printed the check?
Command button for printing is named Command113 and Command button for
Close is named Command2128

Thanks,

Dave

Untested...

Add a Global variable to the Form's Code sheet (up in the Declarations
part of the code).

Option Compare Database
Option Explicit
Dim intPrinted as Integer

Code the Command Print button click event:
DoCmd.OpenReport "ReportName" etc.
intPrinted = 1

Code the form's Currrent Event:
intPrinted = 0

Code the form's Unload event:

If intPrint = 0 And IsNull([PayeeName]) Then
MsgBox "You must print the check"
Cancel = True
End If

Note: There is no guaranty that the check has actually been
successfully printed, only that you sent it to the printer.
 
Here is all my code for the form to make it not close without printing the
check after a payee has been entered.(Customers)
'On Click Event of Command Button to close form
On Error GoTo Err_Command126_Click

DoCmd.Close
If intPrint = 0 And IsNull([Customers]) Then
MsgBox "Check(s) Must Be Printed"
Cancel = True
End If



Forms!frmAutoPayrollReport.Visible = True



Exit_Command126_Click:
Exit Sub

Err_Command126_Click:
MsgBox Err.Description
Resume Exit_Command126_Click


'On Click Event of Print Checks Command Button
Dim intPrinted As Integer
On Error GoTo Err_Command89_Click

Dim stDocName As String
If IsNull(Me.ChkNo) Then
MsgBox "Check Number Needed Please.", vbOKOnly + vbInformation
Exit Sub
End If

stDocName = "Checks"
Forms!blankchecks.Visible = False
DoCmd.OpenReport stDocName, acNormal
intPrinted = 1
Forms!blankchecks.Visible = True



Exit_Command89_Click:
Exit Sub

Err_Command89_Click:
MsgBox Err.Description
Resume Exit_Command89_Click

'On Current Event of Form
intPrinted = 0

'On Unload Event of Form
If intPrint = 0 And IsNull([Customers]) Then
MsgBox "Check(s)Must Be Printed"
Cancel = True
End If
 
On Fri, 10 Sep 2004 02:29:50 GMT, Dave Elliott wrote:

See my comments intespersed below and also at the bottom..
Here is all my code for the form to make it not close without printing the
check after a payee has been entered.(Customers)
'On Click Event of Command Button to close form
On Error GoTo Err_Command126_Click

DoCmd.Close
If you use Close you should specify close what.
If intPrint = 0 And IsNull([Customers]) Then
MsgBox "Check(s) Must Be Printed"
Cancel = True
End If

The above 4 lines do not belong in this event. They belong in the
Unload event.
Also, the Command Button Click event does not have Cancel as an
argument, so Cancel = True has no meaning in this context.
Forms!frmAutoPayrollReport.Visible = True

Exit_Command126_Click:
Exit Sub

Err_Command126_Click:

If you cancel the close event, you will generate an error 2501, so you
should trap it here.
MsgBox Err.Description
Resume Exit_Command126_Click

'On Click Event of Print Checks Command Button
Dim intPrinted As Integer

The Dim IntPrint as Integer declaration must go at the very top of the
code sheet, not in an event.
On Error GoTo Err_Command89_Click

Dim stDocName As String
If IsNull(Me.ChkNo) Then
MsgBox "Check Number Needed Please.", vbOKOnly + vbInformation
Exit Sub
End If

stDocName = "Checks"
Forms!blankchecks.Visible = False
DoCmd.OpenReport stDocName, acNormal
intPrinted = 1
Forms!blankchecks.Visible = True

Exit_Command89_Click:
Exit Sub

Err_Command89_Click:
MsgBox Err.Description
Resume Exit_Command89_Click

'On Current Event of Form
intPrinted = 0

'On Unload Event of Form
If intPrint = 0 And IsNull([Customers]) Then

Upon re-reading your original post I realize you want to not close if
[Customers] Is Not Null. My mistake.
MsgBox "Check(s)Must Be Printed"
Cancel = True
End If

Here's a working code:
Note.... The lines marked with a ' ** have been changed from yours

Option Explicit
Option Comapare Database
Dim intPrinted As Integer ' **

' The above 3 lines belong at the very top of the report's code sheet,
' before the first event, NOT in an event.
_______________________________
'On Click Event of Command Button to close form
On Error GoTo Err_Command126_Click

DoCmd.Close, acForm, Me.Name ' **

Forms!frmAutoPayrollReport.Visible = True

Exit_Command126_Click:
Exit Sub

Err_Command126_Click:
If Err = 2501 Then ' **
Else ' **
MsgBox Err.Description
End If ' **
Resume Exit_Command126_Click
_________________________________________

'On Click Event of Print Checks Command Button

On Error GoTo Err_Command89_Click

Dim stDocName As String
If IsNull(Me.ChkNo) Then
MsgBox "Check Number Needed Please.", vbOKOnly + vbInformation
Exit Sub
End If

stDocName = "Checks"
Forms!blankchecks.Visible = False
DoCmd.OpenReport stDocName, acNormal
intPrinted = 1
Forms!blankchecks.Visible = True

Exit_Command89_Click:
Exit Sub

Err_Command89_Click:
MsgBox Err.Description
Resume Exit_Command89_Click
____________________________

'On Current Event of Form
intPrinted = 0
_________________________________

'On Unload Event of Form
If intPrint = 0 And Not IsNull([Customers]) Then ' **
MsgBox "Check(s)Must Be Printed"
Cancel = True
End If
___________________________

Try it now Dave.
 
Since my Report does not have a record source, instead it uses Blank ='s
Blank, where should I insert code?

fredg said:
On Fri, 10 Sep 2004 02:29:50 GMT, Dave Elliott wrote:

See my comments intespersed below and also at the bottom..
Here is all my code for the form to make it not close without printing
the
check after a payee has been entered.(Customers)
'On Click Event of Command Button to close form
On Error GoTo Err_Command126_Click

DoCmd.Close
If you use Close you should specify close what.
If intPrint = 0 And IsNull([Customers]) Then
MsgBox "Check(s) Must Be Printed"
Cancel = True
End If

The above 4 lines do not belong in this event. They belong in the
Unload event.
Also, the Command Button Click event does not have Cancel as an
argument, so Cancel = True has no meaning in this context.
Forms!frmAutoPayrollReport.Visible = True

Exit_Command126_Click:
Exit Sub

Err_Command126_Click:

If you cancel the close event, you will generate an error 2501, so you
should trap it here.
MsgBox Err.Description
Resume Exit_Command126_Click

'On Click Event of Print Checks Command Button
Dim intPrinted As Integer

The Dim IntPrint as Integer declaration must go at the very top of the
code sheet, not in an event.
On Error GoTo Err_Command89_Click

Dim stDocName As String
If IsNull(Me.ChkNo) Then
MsgBox "Check Number Needed Please.", vbOKOnly + vbInformation
Exit Sub
End If

stDocName = "Checks"
Forms!blankchecks.Visible = False
DoCmd.OpenReport stDocName, acNormal
intPrinted = 1
Forms!blankchecks.Visible = True

Exit_Command89_Click:
Exit Sub

Err_Command89_Click:
MsgBox Err.Description
Resume Exit_Command89_Click

'On Current Event of Form
intPrinted = 0

'On Unload Event of Form
If intPrint = 0 And IsNull([Customers]) Then

Upon re-reading your original post I realize you want to not close if
[Customers] Is Not Null. My mistake.
MsgBox "Check(s)Must Be Printed"
Cancel = True
End If

Here's a working code:
Note.... The lines marked with a ' ** have been changed from yours

Option Explicit
Option Comapare Database
Dim intPrinted As Integer ' **

' The above 3 lines belong at the very top of the report's code sheet,
' before the first event, NOT in an event.
_______________________________
'On Click Event of Command Button to close form
On Error GoTo Err_Command126_Click

DoCmd.Close, acForm, Me.Name ' **

Forms!frmAutoPayrollReport.Visible = True

Exit_Command126_Click:
Exit Sub

Err_Command126_Click:
If Err = 2501 Then ' **
Else ' **
MsgBox Err.Description
End If ' **
Resume Exit_Command126_Click
_________________________________________

'On Click Event of Print Checks Command Button

On Error GoTo Err_Command89_Click

Dim stDocName As String
If IsNull(Me.ChkNo) Then
MsgBox "Check Number Needed Please.", vbOKOnly + vbInformation
Exit Sub
End If

stDocName = "Checks"
Forms!blankchecks.Visible = False
DoCmd.OpenReport stDocName, acNormal
intPrinted = 1
Forms!blankchecks.Visible = True

Exit_Command89_Click:
Exit Sub

Err_Command89_Click:
MsgBox Err.Description
Resume Exit_Command89_Click
____________________________

'On Current Event of Form
intPrinted = 0
_________________________________

'On Unload Event of Form
If intPrint = 0 And Not IsNull([Customers]) Then ' **
MsgBox "Check(s)Must Be Printed"
Cancel = True
End If
___________________________

Try it now Dave.
 
Since my Report does not have a record source, instead it uses Blank ='s
Blank, where should I insert code?

fredg said:
On Fri, 10 Sep 2004 02:29:50 GMT, Dave Elliott wrote:

See my comments intespersed below and also at the bottom..
Here is all my code for the form to make it not close without printing
the
check after a payee has been entered.(Customers)
'On Click Event of Command Button to close form
On Error GoTo Err_Command126_Click

DoCmd.Close
If you use Close you should specify close what.
If intPrint = 0 And IsNull([Customers]) Then
MsgBox "Check(s) Must Be Printed"
Cancel = True
End If

The above 4 lines do not belong in this event. They belong in the
Unload event.
Also, the Command Button Click event does not have Cancel as an
argument, so Cancel = True has no meaning in this context.
Forms!frmAutoPayrollReport.Visible = True

Exit_Command126_Click:
Exit Sub

Err_Command126_Click:

If you cancel the close event, you will generate an error 2501, so you
should trap it here.
MsgBox Err.Description
Resume Exit_Command126_Click

'On Click Event of Print Checks Command Button
Dim intPrinted As Integer

The Dim IntPrint as Integer declaration must go at the very top of the
code sheet, not in an event.
On Error GoTo Err_Command89_Click

Dim stDocName As String
If IsNull(Me.ChkNo) Then
MsgBox "Check Number Needed Please.", vbOKOnly + vbInformation
Exit Sub
End If

stDocName = "Checks"
Forms!blankchecks.Visible = False
DoCmd.OpenReport stDocName, acNormal
intPrinted = 1
Forms!blankchecks.Visible = True

Exit_Command89_Click:
Exit Sub

Err_Command89_Click:
MsgBox Err.Description
Resume Exit_Command89_Click

'On Current Event of Form
intPrinted = 0

'On Unload Event of Form
If intPrint = 0 And IsNull([Customers]) Then

Upon re-reading your original post I realize you want to not close if
[Customers] Is Not Null. My mistake.
MsgBox "Check(s)Must Be Printed"
Cancel = True
End If

I have a form named BlankChecks that I need to make sure that the user
does
not close the form without printing the check.
The form has a Customers field that cant be null, it should key on
this,
if there is a payee(i.e. Customer) then it has to be printed.
It also has a command close button and a command print button on it.
How can i not allow them to close the form if a payee has been entered
in
the control (Customers) and they have not printed the check?
Command button for printing is named Command113 and Command button for
Close is named Command2128

Thanks,

Dave

Here's a working code:
Note.... The lines marked with a ' ** have been changed from yours

Option Explicit
Option Comapare Database
Dim intPrinted As Integer ' **

' The above 3 lines belong at the very top of the report's code sheet,
' before the first event, NOT in an event.
_______________________________
'On Click Event of Command Button to close form
On Error GoTo Err_Command126_Click

DoCmd.Close, acForm, Me.Name ' **

Forms!frmAutoPayrollReport.Visible = True

Exit_Command126_Click:
Exit Sub

Err_Command126_Click:
If Err = 2501 Then ' **
Else ' **
MsgBox Err.Description
End If ' **
Resume Exit_Command126_Click
_________________________________________

'On Click Event of Print Checks Command Button

On Error GoTo Err_Command89_Click

Dim stDocName As String
If IsNull(Me.ChkNo) Then
MsgBox "Check Number Needed Please.", vbOKOnly + vbInformation
Exit Sub
End If

stDocName = "Checks"
Forms!blankchecks.Visible = False
DoCmd.OpenReport stDocName, acNormal
intPrinted = 1
Forms!blankchecks.Visible = True

Exit_Command89_Click:
Exit Sub

Err_Command89_Click:
MsgBox Err.Description
Resume Exit_Command89_Click
____________________________

'On Current Event of Form
intPrinted = 0
_________________________________

'On Unload Event of Form
If intPrint = 0 And Not IsNull([Customers]) Then ' **
MsgBox "Check(s)Must Be Printed"
Cancel = True
End If
___________________________

Try it now Dave.

I have no idea what you are referring to.
You asked for help on coding a form so that it would not close if a
report was not run. I gave you that coding, using the events you
already had on the form.
If you now have difficulty with a report I would suggest you start a
new thread in the microsoft.public.access.reports news group and
describe that problem.
 
Right now this code produces the message box and then closes the form still
with checks to print.
What can I do to make it not close the form if the condition of
intPrint=0 ?
Did not understand this:
What is code sheet? Wher do I put it?

The Dim IntPrint as Integer declaration must go at the very top of the
code sheet, not in an event.



If intPrint = 0 And Not IsNull([Customers]) Then ' **
MsgBox "Check(s)Must Be Printed"
Cancel = True
End If
 
Back
Top