Force RTE for testing

santosm
3StarLounger
Posts: 253
Joined: 19 Apr 2010, 09:01
Location: Indiana, USA

Force RTE for testing

Post by santosm »

Hi All,
Is there a way to force a run time error so that I can test different ways of error handling?

Thanks,
Mark
Thanks,
Mark

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Force RTE for testing

Post by HansV »

Yes:

Err.Raise 1234

if you want to simulate the error with error number 1234.
Best wishes,
Hans

santosm
3StarLounger
Posts: 253
Joined: 19 Apr 2010, 09:01
Location: Indiana, USA

Re: Force RTE for testing

Post by santosm »

Hi Hans,
I entered that into a button on-click routine and I always get application-defined or object-defined error box. Is that what I should expect or am I doing something wrong?

Thanks,
Mark

Code: Select all

Private Sub Command34_Click()
On Error GoTo ErrHandler
Dim frmCurrentForm As Form, frmName As String
Set frmCurrentForm = Screen.ActiveForm

    frmName = frmCurrentForm.Name
    'MsgBox frmName
    
    Err.Raise 1004
    
    MyPrintScreen "o:\Projects\db_admin\errors\" & frmName & "_" & Format(Now(), "ddmmmyyyy_hhmm") & ".bmp"
    
Exit Sub
ErrHandler:
    RecordError
    MsgBox Err.Number & "-" & Err.Description
    Exit Sub
End Sub
Thanks,
Mark

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Force RTE for testing

Post by HansV »

The error message associated with error number 1004 is "Application-defined or object-defined error", so yes, that's the expected result.
Which error would you like to simulate?
Best wishes,
Hans

santosm
3StarLounger
Posts: 253
Joined: 19 Apr 2010, 09:01
Location: Indiana, USA

Re: Force RTE for testing

Post by santosm »

Hi Hans,
I guess that I would like to get various errors based on the number. I will try some different ones.

Thanks,
Mark
Thanks,
Mark

santosm
3StarLounger
Posts: 253
Joined: 19 Apr 2010, 09:01
Location: Indiana, USA

Re: Force RTE for testing

Post by santosm »

Hi Hans,
I pulled up a list of codes and entered 3040. Which if I am correct should be a disk I/O error. I get this.
You do not have the required permissions to view the files attached to this post.
Thanks,
Mark

santosm
3StarLounger
Posts: 253
Joined: 19 Apr 2010, 09:01
Location: Indiana, USA

Re: Force RTE for testing

Post by santosm »

On another note, does a msgbox being up on screen qualify as the active window? I am trying to do a screen capture of the error when it pops up and save that BMP to disk.

Thanks,
Mark
Thanks,
Mark

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Force RTE for testing

Post by HansV »

santosm wrote:Hi Hans,
I pulled up a list of codes and entered 3040. Which if I am correct should be a disk I/O error. I get this.
Apparently Err.Raise only "knows" generic VBA errors. Try this version:

Err.Raise Number:=3040, Description:=AccessError(3040)
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Force RTE for testing

Post by HansV »

santosm wrote:On another note, does a msgbox being up on screen qualify as the active window?
Yes, it does when I test it.
Best wishes,
Hans

santosm
3StarLounger
Posts: 253
Joined: 19 Apr 2010, 09:01
Location: Indiana, USA

Re: Force RTE for testing

Post by santosm »

Thanks, the method you listed does pull the correct error number and description now. For the screen capture thing I am trying to do, it doesn't ways grab the msgbox window, it grabs the whole screen. Not sure why.

Code: Select all

Private Sub Command34_Click()
On Error GoTo ErrHandler
Dim frmCurrentForm As Form, frmName As String
Set frmCurrentForm = Screen.ActiveForm

    frmName = frmCurrentForm.Name
    'MsgBox frmName
    
    Err.Raise Number:=3040, Description:=AccessError(3040)
    
    
Exit Sub
ErrHandler:
    MsgBox Err.Number & "-" & Err.Description
    RecordError 'this code does the screen capture
    Exit Sub
End Sub
Screen Capture code attached as text file.
You do not have the required permissions to view the files attached to this post.
Thanks,
Mark

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Force RTE for testing

Post by HansV »

The line

MyPrintScreen sPath

will only run when the user has closed the MsgBox - all code execution is temporarily suspended while the message box is displayed. I don't think there is a way around that.
Best wishes,
Hans

BenCasey
4StarLounger
Posts: 495
Joined: 13 Sep 2013, 07:56

Re: Force RTE for testing

Post by BenCasey »

santosm wrote:On another note, does a msgbox being up on screen qualify as the active window? I am trying to do a screen capture of the error when it pops up and save that BMP to disk.

Thanks,
Mark
Yes, it is modular too which means you have to close it before you can continue.

Way round it? Write your own message box.
Regards, Ben

"Science is the belief in the ignorance of the experts."
- Richard Feynman

BenCasey
4StarLounger
Posts: 495
Joined: 13 Sep 2013, 07:56

Re: Force RTE for testing

Post by BenCasey »

HansV wrote:
santosm wrote:Hi Hans,
I pulled up a list of codes and entered 3040. Which if I am correct should be a disk I/O error. I get this.
Apparently Err.Raise only "knows" generic VBA errors. Try this version:

Err.Raise Number:=3040, Description:=AccessError(3040)
You can use your own number. See the Help File. Here is an extract and an Example.
Required. Long integer that identifies the nature of the error. Visual Basic errors (both Visual Basic-defined and user-defined errors) are in the range 0–65535. The range 0–512 is reserved for system errors; the range 513–65535 is available for user-defined errors. When setting the Number property to your own error code in a class module, you add your error code number to the vbObjectError constant. For example, to generate the error number 513, assign vbObjectError + 513 to the Number property.

Const MyContextID = 1010407 ' Define a constant for contextID.
Function TestName(CurrentName, NewName)
If Instr(NewName, "bob") Then ' Test the validity of NewName.
' Raise the exception
Err.Raise vbObjectError + 513, "MyProj.MyObject", _
"No ""bob"" allowed in your name", "c:\MyProj\MyHelp.Hlp", _
MyContextID
End If
End Function
To see the help article, type in "err.raise" on a line, put the focus on the line and press the F1 key
Regards, Ben

"Science is the belief in the ignorance of the experts."
- Richard Feynman

santosm
3StarLounger
Posts: 253
Joined: 19 Apr 2010, 09:01
Location: Indiana, USA

Re: Force RTE for testing

Post by santosm »

Thanks Guys,
I did finally figure out that I was clicking OK before the screen capture was happening. So, this method of storing a picture of the fault won't work but I will still stamp the error info into a table and that will work fine for now. Thanks for all the pointers!

Mark
Thanks,
Mark