VBA - Cancelling Dialog Box Run-time error 1004 - vba

I am attempting to have my end sub when I click cancel. However, I am getting a Run-Time Error when I do so.
I am just trying to close the dialog box which I did with an Else: Exit Sub statement. However, it still shows the Run-Time Error, while it should just exit sub.

This was a simple fix of just adding an ErrorHandler like so:
Public Sub Run_Split()
On Error GoTo ErrHandler
'Call Private Subs
Exit Sub
ErrHandler:
MsgBox "User Cancelled Program", vbCritical
End Sub
Thank you to everyone that has helped me on this site in the past, your help as allowed me to be somewhat self taught in VBA and I was able to solve this without asking for assistance.

Related

What to do if Error 91 shows up at vbModeless?

So basically VBA is giving me "Erro 91" and pointing out to the "Window_Designer.Show vbModeless". What I want is a basic userform to show up so I can fill the numbers, but whenever I run the code, the error pops up. What am I doing wrong?
Private Sub Custom_Window_Click()
'Call custom window userform
Windows_and_Secondary_Frames.Hide
Window_Designer.Show vbModeless
End Sub

How to avoid MS Access error: the command or action Paste is not available now

I have just upgraded from MS Access 2013 to 2019 and my duplicate record buttons have stopped working. I get the error "The command or action Paste is not available now". Have looked through the Trust Center for anything that might be upsetting it but I cannot see anything. I created a new button using the Duplicate Record wizard, and that has the same problem. So it's not my code.
Private Sub Command115_Click()
On Error GoTo Err_Command115_Click
DoCmd.RunCommand acCmdSelectRecord
DoCmd.RunCommand acCmdCopy
DoCmd.RunCommand acCmdRecordsGoToNew
DoCmd.RunCommand acCmdSelectRecord
DoCmd.RunCommand acCmdPaste
Exit_Command115_Click:
Exit Sub
Err_Command115_Click:
MsgBox Err.Description
Resume Exit_Command115_Click
End Sub
After several hours of hairpulling. I have arrived at a work around. Just putting a pause of point one second before the paste avoids the error message. So it seems there is some kind of timing issue. Might be some kind of record locking problem that only occurs on slower computers.
Private Sub Copy_Record_Click()
On Error GoTo Err_Copy_Record_Click
DoCmd.RunCommand acCmdSelectRecord
DoCmd.RunCommand acCmdCopy
DoCmd.RunCommand acCmdRecordsGoToNew
DoCmd.RunCommand acCmdSelectRecord
Pause (0.1)
DoCmd.RunCommand acCmdPaste
Exit_Copy_Record_Click:
Exit Sub
Err_Copy_Record_Click:
MsgBox Err.Description
Resume Exit_Copy_Record_Click
End Sub

How to suppress error message after cancel report print?

I used DoCmd.RunCommand acCmdPrint to print a report. If I print the report, ok. But if I cancel, Access show a error and stop run the button code.
I used DoCmd.SetWarnings (False) but don't suppress this error.
How can I do?
You need to check the error code in your error handler, and if it's "Operation cancelled", ignore it.
Option Explicit
Sub Something()
On Error GoTo Trap
'DoCmd...
Leave:
On Error GoTo 0
Exit Sub
Trap:
If Err.Number <> 2501 Then MsgBox Err.Description, vbCritical
Resume Leave
End Sub

visual basic for application - error handling

I have never worked with VBA, therefore this could by an easy task, but I can't get it working :-) Simply I need to catch a run time error in following code :
Private Sub CheckBox1_Click()
ActiveWorkbook.Sheets("(X)").Unprotect
ActiveWorkbook.Sheets("(X)").Select
ActiveSheet.Range("F18").Select
'here comes the error
ActiveSheet.Range("$A$2:$X$310").AutoFilter Field:=1, Criteria1:="1"
Sheets("(40 UKÁŽKA)").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub
Welcome to old school error handling.
What you are looking for is either
Sub MySub()
On Error Resume Next
DoMyStuff()
End Sub
if you want to swallow this issue, e.g equivalent of Try Catch End
or
Sub MySub()
On Error Goto ErrorHandler
DoMyStuff()
KeepGoing :
DoSomeMoreStuffAnyway()
Exit Sub
ErrorHandler:
HandleTheError()
Exit Sub
You can get the Error number from the variable Err
You can also fix and call Resume to go to the next line of code after the error happened
You can also do Resume To a lable e.g. Resume KeepGoing in the above.
More info here MSDN VBA Error Handling
Use On Error. See this for a good discussion of the topic.
On Error GoTo ErrHandler
<do things>
On Error GoTo 0 'Turn default error handling back on
Exit Sub
ErrHandler:
If Error.Number = xxxx Then
<error processing>
End If
(then Resume )(or Resume Next) (or do nothing & let sub end)
End Sub

Error Handler - Exit Sub vs. End Sub

Why would I want to get out of an Error Handler (after handling) with an Exit Sub instead of just letting it go to the End Sub?
I'm sure it's simple. I just don't understand. Thanks for any help.
Example:
Public Sub SubA()
On Error Goto ProcError
''# other code
MsgBox FuncA()
ProcExit:
Exit Sub
ProcError:
MsgBox Err.Description
Resume ProcExit
End Sub
Your ProcExit label is your place where you release all the resources whether an error happened or not. For instance:
Public Sub SubA()
On Error Goto ProcError
Connection.Open
Open File for Writing
SomePreciousResource.GrabIt
ProcExit:
Connection.Close
Connection = Nothing
Close File
SomePreciousResource.Release
Exit Sub
ProcError:
MsgBox Err.Description
Resume ProcExit
End Sub
Typically if you have database connections or other objects declared that, whether used safely or created prior to your exception, will need to be cleaned up (disposed of), then returning your error handling code back to the ProcExit entry point will allow you to do your garbage collection in both cases.
If you drop out of your procedure by falling to Exit Sub, you may risk having a yucky build-up of instantiated objects that are just sitting around in your program's memory.