You Cancelled the Previous Operation Error

chewy

SuperNintendo Chalmers
Local time
Today, 04:03
Joined
Mar 8, 2002
Messages
581
I have this code

Private Sub Command4_Click()

'between 1/1/2003 AND 1/1/04
Dim myDate As Variant
myDate = "BETWEEN "
myDate = myDate & Me.StartDate
myDate = myDate & " AND "
myDate = myDate & Me.EndDate

FullDate.SetFocus
Me.FullDate.Text = myDate

DoCmd.OpenQuery "qryMain", acViewNormal
'DoCmd.Close acQuery, "qryMain"
'DoCmd.OpenReport "ComboBox2", acViewPreview
End Sub


On this line
DoCmd.OpenQuery "qryMain", acViewNormal

It give me the Error You Cancelled the Previous Operation. What is causing this to happen.

I also am having a query read from the text box what the value of the query will be. See Attached. Thanks for any ideas!
 

Attachments

  • query.jpg
    query.jpg
    40.3 KB · Views: 220
You need some error handling and catch the exception. First figure out what the error number is. Once you have figured that out you can catch the exception / error and skip it from being displayed.

Its a valid error that can happen on a report as well when you cancel a report when originally you intended to open it.

To find out the error do the following

Before the beginning of your code do this:

Code:
On Error Goto Hell

after your code but before the function / sub END do this:
Code:
Heaven:
Exit Sub


Hell:
MsgBox Err.Description,vbCritical, "Error #: " & Err.Number
Resume Heaven

That will give u the error code on the title bar of the message box such as Error #: 1215.

Once you get the error code you modify the above to this:

Code:
Heaven:
Exit Sub


Hell:
If Err.Number = YourErrorNumberHere then 
       'dont show the message
else
     MsgBox Err.Description,vbCritical, "Error #: " & Err.Number
end if
Resume Heaven

Jon
 
Try this...

Private Sub Command4_Click()
On Error GoTo Err_Command4_Click

Me.FullDate.Value = "BETWEEN " & Me.StartDate.Value & " AND " & Me.EndDate.Value

'Assuming the control source of your report is "qryMain"
DoCmd.OpenReport "ComboBox2", acViewPreview

Exit_Command4_Click:
Exit Sub

Err_Command4_Click:
If Err = 2001 Then 'You canceled the previous operation.
Exit Sub
Else
MsgBox Err.Number & " - " & Err.Description
Resume Exit_Command4_Click
End If

End Sub

'HTH
 
Your criteria line in the query will be interpreting your date as text that is not date qualified.

You will need to include the date # qualifier either end of your date.

eg:

Me.FullDate.Value = "BETWEEN #" & Me.StartDate.Value & "# AND #" & Me.EndDate.Value & "#"

However, looking at your query structure, it would be much easier to simply enter your criteria line in your query as:

Between [forms]![frmdate].[startdate] And [forms]![frmdate].[enddate]

That way you can avoid messing with the concatenation code.
If the format of StartDate and EndDate are set within the form, you can avoid type mismatch errors.

HTH

Brad.
 
Thanks Brad that did it. I was using ! instead of the period. What is the difference?
 
You can still use '!' that wasnt the original problem.

Jon
 
it's strange. I was sending it with the "#" signs before and it didn't work either. Oh well. It works now!
 
Hence the reason I dont use access at all no more. SQL Server and VB is soo much better. Better controls, flexibility, power. Access is a tool :) literally a piece of sh**.
 
good to hear from you again Mission! Havent heard from you in a while
 
ola Mr. Chewy.

Ya I sneak in every once in a while to annoy rich of course :o.
Hope everything on your end is looking good.

Jon
 
working on a new database now. Mostly taking pieces from old databases that I have made. Slow but steady. Gives me something to do aroundhere
 
hey! you could probably answer this. When I want to run a query I use the following code and then open the subsequent report.

Private Sub Command4_Click()

If StartDate > EndDate Then
MsgBox "Start date can not be after end date"
Else
DoCmd.OpenQuery "qryMain"
DoCmd.Close acQuery, "qryMain"
DoCmd.OpenReport "rptMain", acViewPreview

End If
End Sub


How can I just run the query behind a command button without opening and closing it?
 
Queries are always running.

If your rptMain takes data from this query than why not just bound this query to that report? That way any changes in the query change the report?

Jon
 
You can also set the reports recordsource property during runtime in the on open event of the report

Me.Recordsource="qryMain"

Jon
 
well I be jiggered! I was doing that all along and didnt realize it. I was just adding a step. Thanks guys!
 

Users who are viewing this thread

Back
Top Bottom