Error Handling on the Cancel of the OutputTo action

G

Guest

Have the following:

Me.Visible = False
DoCmd.OpenQuery "SelectCategory", acViewNormal, acReadOnly
DoCmd.OutputTo acOutputQuery, SelectCategory, , , yes
DoCmd.Close acQuery, "selectcategory"
DoCmd.Close acForm, "SelectCategory"
DoCmd.OpenForm "Main_Search", acNormal
DoCmd.Maximize

When the OutputTo prompt comes up and you choose to cancel instead of
exporting the data out of Access, I am receiving the following:

Run-time error '2501':
The OutputTo action was canceled. Then I can end or debug.

How do I force it passed this so that it gets to the close query etc and
gets the user back to the main form?

Thanks in advance for the help!
 
A

Alex Dybenko

Hi,
i normally do this a following way - I make a new Make-Table query based on
SelectCategory query and before DoCmd.OutputTo I create a temporary table
with query results. then I DoCmd.OutputTo this temporary table and after
all delete it
HTH

--
Best regards,
___________
Alex Dybenko (MVP)
http://alexdyb.blogspot.com
http://www.PointLtd.com
 
J

John Spencer

Add some error-handling to your code

Generally that might look something like:

Private Sub ThisIsYourSub()

On Error goto MyErrorHandler

Me.Visible = False
DoCmd.OpenQuery "SelectCategory", acViewNormal, acReadOnly
DoCmd.OutputTo acOutputQuery, SelectCategory, , , yes
DoCmd.Close acQuery, "selectcategory"
DoCmd.Close acForm, "SelectCategory"
DoCmd.OpenForm "Main_Search", acNormal
DoCmd.Maximize

Exit Sub

MyErrorHandler:
If Err.Number = 2501 then
Resume Next
Else
Msgbox Err.Number & ":" & Err.Description
End if
End Sub
 
G

Guest

Thanks so much! Worked like a charm.

John Spencer said:
Add some error-handling to your code

Generally that might look something like:

Private Sub ThisIsYourSub()

On Error goto MyErrorHandler

Me.Visible = False
DoCmd.OpenQuery "SelectCategory", acViewNormal, acReadOnly
DoCmd.OutputTo acOutputQuery, SelectCategory, , , yes
DoCmd.Close acQuery, "selectcategory"
DoCmd.Close acForm, "SelectCategory"
DoCmd.OpenForm "Main_Search", acNormal
DoCmd.Maximize

Exit Sub

MyErrorHandler:
If Err.Number = 2501 then
Resume Next
Else
Msgbox Err.Number & ":" & Err.Description
End if
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top