craigglenn
Technical User
I have searched and labored over this simple task. I am having trouble getting out of a report if the user wants to cancel the report based on clicking cancel. Here is a look at the onopen event code. thanks in advance for your input.
Option Compare Database
Private Sub Report_NoData(cancel As Integer)
MsgBox ("No records found for specified route."
cancel = True
End Sub
Private Sub Report_Open(cancel As Integer)
Dim routeN As Integer
Dim rday As Integer
Dim theQuery As String
Dim dayField As String
GetRouteDay rday 'prompt for route day
If rday = 0 Then
DoCmd.CancelEvent 'this does not work!!!
End If
GetRouteNum routeN 'prompt for route number
Me.RNUM.ControlSource = "=" & routeN & "" 'Set the route Number on Report
Select Case rday
Case 1
dayField = "PropSwpMonday"
Me.OrderBy = "[PropPriorityMonday]"
Me.PropRouteNum.ControlSource = "=""Monday"""
Case 2
dayField = "PropSwpTuesday"
Me.OrderBy = "[PropPriorityTuesday]"
Me.PropRouteNum.ControlSource = "=""Thuesday"""
Case 3
dayField = "PropSwpWednesday"
Me.OrderBy = "[PropPriorityWednesday]"
Me.PropRouteNum.ControlSource = "=""Wednesday"""
Case 4
dayField = "PropSwpThursday"
Me.OrderBy = "[PropPriorityThursday]"
Me.PropRouteNum.ControlSource = "=""Thursday"""
Case 5
dayField = "PropSwpFriday"
Me.OrderBy = "[PropPriorityFriday]"
Me.PropRouteNum.ControlSource = "=""Friday"""
Case 6
dayField = "PropSwpSaturday"
Me.OrderBy = "[PropPrioritySaturday]"
Me.PropRouteNum.ControlSource = "=""Saturday"""
Case 7
dayField = "PropSwpSunday"
Me.OrderBy = "[PropPrioritySunday]"
Me.PropRouteNum.ControlSource = "=""Sunday"""
End Select
Me.OrderByOn = True
theQuery = "Select * From mainProperty Where " & dayField & "=" & routeN & ";"
Me.RecordSource = theQuery
End Sub
Private Function GetRouteNum(routeN) As Integer
On Error GoTo ErrorHandler
Dim message, title
message = "Enter Route Number:"
title = "Route Number"
routeN = InputBox(message, title)
Exit Function
ErrorHandler:
Select Case Err.Number
Case 13
MsgBox ("You must enter a valid route number"
Case Else
MsgBox ("You must enter a valid route number"
End Select
Resume
End Function
Private Function GetRouteDay(rday) As Integer
On Error GoTo ErrorHandler
Dim message, title, default
message = "Enter number for day of route:" + (Chr(13)) + "1. Monday" + (Chr(13)) + "2. Tuesday" + (Chr(13)) + "3. Wednesday" + (Chr(13)) + "4. Thursday" + (Chr(13)) + "5. Friday" + (Chr(13)) + "6. Saturday" + (Chr(13)) + "7. Sunday"
title = "Route day"
default = "1"
InvalidDay:
rday = InputBox(message, title, default)
Select Case rday
Case "1", "2", "3", "4", "5", "6", "7"
Exit Function
Case Else
MsgBox ("You must enter a valid Day Number"
GoTo InvalidDay
End Select
ErrorHandler:
Dim response As String
Select Case Err.Number
Case 13
response = MsgBox("Cancel Report", vbYesNo)
If response = vbYes Then
rday = 0
Exit Function
End If
Case Else
MsgBox ("You must enter a valid day"
End Select
Resume
End Function
Option Compare Database
Private Sub Report_NoData(cancel As Integer)
MsgBox ("No records found for specified route."
cancel = True
End Sub
Private Sub Report_Open(cancel As Integer)
Dim routeN As Integer
Dim rday As Integer
Dim theQuery As String
Dim dayField As String
GetRouteDay rday 'prompt for route day
If rday = 0 Then
DoCmd.CancelEvent 'this does not work!!!
End If
GetRouteNum routeN 'prompt for route number
Me.RNUM.ControlSource = "=" & routeN & "" 'Set the route Number on Report
Select Case rday
Case 1
dayField = "PropSwpMonday"
Me.OrderBy = "[PropPriorityMonday]"
Me.PropRouteNum.ControlSource = "=""Monday"""
Case 2
dayField = "PropSwpTuesday"
Me.OrderBy = "[PropPriorityTuesday]"
Me.PropRouteNum.ControlSource = "=""Thuesday"""
Case 3
dayField = "PropSwpWednesday"
Me.OrderBy = "[PropPriorityWednesday]"
Me.PropRouteNum.ControlSource = "=""Wednesday"""
Case 4
dayField = "PropSwpThursday"
Me.OrderBy = "[PropPriorityThursday]"
Me.PropRouteNum.ControlSource = "=""Thursday"""
Case 5
dayField = "PropSwpFriday"
Me.OrderBy = "[PropPriorityFriday]"
Me.PropRouteNum.ControlSource = "=""Friday"""
Case 6
dayField = "PropSwpSaturday"
Me.OrderBy = "[PropPrioritySaturday]"
Me.PropRouteNum.ControlSource = "=""Saturday"""
Case 7
dayField = "PropSwpSunday"
Me.OrderBy = "[PropPrioritySunday]"
Me.PropRouteNum.ControlSource = "=""Sunday"""
End Select
Me.OrderByOn = True
theQuery = "Select * From mainProperty Where " & dayField & "=" & routeN & ";"
Me.RecordSource = theQuery
End Sub
Private Function GetRouteNum(routeN) As Integer
On Error GoTo ErrorHandler
Dim message, title
message = "Enter Route Number:"
title = "Route Number"
routeN = InputBox(message, title)
Exit Function
ErrorHandler:
Select Case Err.Number
Case 13
MsgBox ("You must enter a valid route number"
Case Else
MsgBox ("You must enter a valid route number"
End Select
Resume
End Function
Private Function GetRouteDay(rday) As Integer
On Error GoTo ErrorHandler
Dim message, title, default
message = "Enter number for day of route:" + (Chr(13)) + "1. Monday" + (Chr(13)) + "2. Tuesday" + (Chr(13)) + "3. Wednesday" + (Chr(13)) + "4. Thursday" + (Chr(13)) + "5. Friday" + (Chr(13)) + "6. Saturday" + (Chr(13)) + "7. Sunday"
title = "Route day"
default = "1"
InvalidDay:
rday = InputBox(message, title, default)
Select Case rday
Case "1", "2", "3", "4", "5", "6", "7"
Exit Function
Case Else
MsgBox ("You must enter a valid Day Number"
GoTo InvalidDay
End Select
ErrorHandler:
Dim response As String
Select Case Err.Number
Case 13
response = MsgBox("Cancel Report", vbYesNo)
If response = vbYes Then
rday = 0
Exit Function
End If
Case Else
MsgBox ("You must enter a valid day"
End Select
Resume
End Function