[AccessD] CANCEL WILL NOT CANCEL

Borge Hansen pcs at azizaz.com
Thu Feb 16 23:12:26 CST 2006


Joe,
Perhaps the following:

The code is associated with a command button.
A command button does not have a cancel event

Put the code in a procedure that has a cancel event,
for example Report_OnOpen and reference the form where the txt controls reside
in your if test

or

since you are using the code on a form where you gather selection criteria
parameters for the recordsource of some report, then instead of cancel :

issue the message box warning
return focus to the text control in question
and do an "exit sub", something like:


If IsNull(Me.txtAccStartDate) or len(Me.txtAccStartDate) =  0 Then  'Confirm
Start Date is not null
    MsgBox( "There is no Start Date Entered." & vbCrLf & "" & vbCrLf & "Please
select a Start Date",  vbCritical, "Input Validation")
    Me.txtAccStartDate.setfocus
    exit sub
End If

forget about the lngRetval1


Also:
I handle the case of no data selected to report on like this:

On the OnNoData event in the report:

Private Sub Report_NoData(Cancel As Integer)
On Error GoTo ErrorHere
            Cancel = True
ErrorExit:
    DoCmd.SetWarnings True
    Exit Sub
ErrorHere:
  MsgBox "Error in Report_NoData " & ": " & Err.Number & " " & Err.Description
  Resume ErrorExit
End Sub


This will generate a 2501 error
So, on the command button code on the 'Report Parameter Form: where you are
firing up the report, include the following in the error handler:

ErrorHere:
  Select Case Err
    Case 2501    'you cancelled open report
    MsgBox "The current set of selection criteria result in no data to be
reported on. Try to change the selection criteria and then run the report
again!", vbInformation, "NO DATA TO REPORT ON"
    Resume ErrorExit
    Case Else
    MsgBox "Error in cmdSave_Click " & ": " & Err.Number & " " & Err.Description
    Resume ErrorExit
  End Select

....hope this helps

borge
gold coast, australia

----- Original Message ----- 
From: "Joe Hecht" <jmhecht at earthlink.net>
To: "'Access Developers discussion and problem solving'"
<accessd at databaseadvisors.com>
Sent: Friday, February 17, 2006 2:10 PM
Subject: [AccessD] CANCEL WILL NOT CANCEL


> The following code is supposed to check that
>
>
>
> 1. The start date is not null
> 2. The end date is not null.
>
>
>
> If either is null I want a message box( that works) and when
> user clicks ok they go back to form.
>
>
>
> It is not working. Can any one see why?
>
>
>
> Thanks
>
>
>
> Private Sub cmdAccEndDate_Click()
>
> Dim lngRetval As Long
>
> Dim strStDocName As String
>
> Dim lngRetval1 As Long
>
> Dim lngRetval2 As Long
>
> Dim CANCEL As Boolean
>
>
>
>
>
>      Me.txtAccEndDate = Me.calAccEndDate  'Sets end date
>
>
>
>     If IsNull(Me.txtAccStartDate) Then  'Confirm Start Date
> is not null
>
>             lngRetval1 = MsgBox( _
>
>             "There is no Start Date Entered." & vbCrLf & ""
> & vbCrLf & "Please select a Start Date", _
>
>             vbOKOnly + vbCritical + vbDefaultButton1, _
>
>             "PLEASE ENTER START DATE")
>
>
>
>             Select Case lngRetval1
>
>             Case vbOK
>
>             CANCEL = True
>
>
>
>             End Select
>
>         End If
>
>
>
>     If IsNull(Me.txtAccEndDate) Then  'Confirm End Date is
> not null
>
>
>
>             lngRetval1 = MsgBox( _
>
>             "There is no End Date Entered." & vbCrLf & "" &
> vbCrLf & "Please select a Start Date", _
>
>             vbOKOnly + vbCritical + vbDefaultButton1, _
>
>             "PLEASE ENTER END DATE")
>
>
>
>             Select Case lngRetval1
>
>             Case vbOK
>
>             CANCEL = True
>
>             End Select
>
>     End If
>
>
>
>      If Me.txtAccStartDate > Me.txtAccEndDate Then
>
>         lngRetval = MsgBox( _
>
>         "The start date must be the same as, or before the
> end date of the report." & vbCrLf & "" & vbCrLf & "Please
> review  the selected dates and try your report again", _
>
>          vbOKOnly + vbCritical + vbDefaultButton1, _
>
>          "INCORECT DATE INPUT")
>
>
>
>         Select Case lngRetval
>
>             Case vbOK
>
>         End Select
>
>     Else
>
>         '              Dim stDocName As String
>
>
>
>         strStDocName = "30_rptAccidentRegister"
>
>         DoCmd.OpenReport strStDocName, acPreview
>
>         DoCmd.RunCommand acCmdZoom100
>
>
>
>
>
> Joe Hecht
>
> jmhecht at earthlink.net




More information about the AccessD mailing list