473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Error Handling/Trapping Question

JodiPhillips
Hello again,

Its been a while since I've asked a question, I've been learning steadily reading the How To's and the questions in this forum, as well as reading my Access VBA and Access bibles till they are now in tatters!.

I'm stuck with understanding the error handling process by VB6.5 (in particular for MSAccess 2003).

The following code is a test code only, so that I could understand things before I go on to something more meaty. It is probably the 5th incarnation or attempt and as you can see I have resorted to If..Then..Else and a SubFunction to try and handle this type of error. The error I'm referring to is the nasy Err 13 Data Type Mismatch error which occurs when I deliberately enter a non numeric character in the text box on the form. (The from has one command button and one text box)

Expand|Select|Wrap|Line Numbers
  1. Sub Command0_Click()
  2.  
  3.             Dim aNumberbox As Double
  4.         Dim NotANumber As Variant
  5.                         aNumberbox = 0
  6.            NotANumber = CVErr(13)
  7.  
  8.      aNumberbox = CheckData(Me.Text1)
  9.  
  10.       If IsError(aNumberbox) Then
  11.          Select Case aNumberbox
  12.                 Case NotANumber
  13.                MsgBox "Error: not a number."
  14.             Case Else
  15.                MsgBox "Unknown Error."
  16.          End Select
  17.       Else
  18.       aNumberbox = Me.Text1
  19.           MsgBox ("You entered the number " & aNumberbox)
  20.           MsgBox ("Why not try it again?")
  21.       End If
  22.    End Sub
  23.  
  24.    Function CheckData(aNumberbox As Integer) As Integer
  25.      Dim NotANumber As Integer
  26.  
  27.       If Not IsNumeric(aNumberbox) Then
  28.          CheckData = NotANumber
  29.             End If
  30.    End Function
Despite trying to handle the error with a very simple error handle subroutine in my previous attempt:

Expand|Select|Wrap|Line Numbers
  1. Sub Command0_Click()
  2.  
  3. Dim aNumber As Double
  4. On Error GoTo ErrorHandle
  5.  
  6. aNumber = CDbl(Me.Text1)
  7. ExitHere:
  8.   MsgBox ("You entered the number " & aNumber)
  9.  MsgBox ("Why not try it again?")
  10.  
  11.  
  12. ErrorHandle:
  13.   MsgBox ("Please enter a number.")
  14.   Resume ExitHere
  15. End Sub
  16.  
No matter what I do the Built-In Error Message (as unhelpful as they are :P) still occur.

Both sets of code of course break at the "aNumber = CDbl(Me.Text1)" line.

Would really appreciate it if anyone can help with this :)

JP
Jul 31 '08 #1
5 1775
Sorry but can't get what u want exactly
anyways a dirty solution is here

Sub Command0_Click()

Dim aNumber As Double
On Error GoTo ErrorHandle

aNumber = CDbl(Me.Text1)
ExitHere:
MsgBox ("You entered the number " & aNumber)
MsgBox ("Why not try it again?")
Me.Text1.SetFocus
Exit Sub

ErrorHandle:
MsgBox ("Please enter a number.")
Resume ExitHere
End Sub
Jul 31 '08 #2
Sorry but can't get what u want exactly
anyways a dirty solution is here

Sub Command0_Click()

Dim aNumber As Double
On Error GoTo ErrorHandle

aNumber = CDbl(Me.Text1)
ExitHere:
MsgBox ("You entered the number " & aNumber)
MsgBox ("Why not try it again?")
Me.Text1.SetFocus
Exit Sub

ErrorHandle:
MsgBox ("Please enter a number.")
Resume ExitHere
End Sub

Hello anuragshrivastava

Thank you for your reply.

What I am trying to achieve is to stop the built-in error handling from occuring when the code encounters an error. The variable aNumber is set as a double to capture all digits (both whole and decimal) and the code will work fine with a number entered. However, enter a text character and the built-in error handler returns a type mismatch error, which is expected due to the type of data entered not being numeric.

I'm trying to stop the in-built mismatch error from reporting, and the mismatch error to be handled specifically by the ErrorHandle code block.

Can the mismatch type error be trapped and handled programmatically or will I have to respecify the data type of the anumber variable as perhaps a variant? If I have to change the data type to variant I will need to go with something similar to the first code block posted above using If...then...else (which still doesn't work to stop the in-built error handling).

Thanks again.

JP
Jul 31 '08 #3
Hello anuragshrivastava

Thank you for your reply.

What I am trying to achieve is to stop the built-in error handling from occuring when the code encounters an error. The variable aNumber is set as a double to capture all digits (both whole and decimal) and the code will work fine with a number entered. However, enter a text character and the built-in error handler returns a type mismatch error, which is expected due to the type of data entered not being numeric.

I'm trying to stop the in-built mismatch error from reporting, and the mismatch error to be handled specifically by the ErrorHandle code block.

Can the mismatch type error be trapped and handled programmatically or will I have to respecify the data type of the anumber variable as perhaps a variant? If I have to change the data type to variant I will need to go with something similar to the first code block posted above using If...then...else (which still doesn't work to stop the in-built error handling).

Thanks again.

JP
Then u can simply check whetetehr the text enterd is numeric or not thru ISNumeric function

Just copy the following spurce of code before u chek

If Not IsNumeric(Me.Text1) Then

MsgBox "Sorry wrong entry"
Text1= ""
Text1.setfocus
Else
aNumberbox = CheckData(Me.Text1)
End If
Jul 31 '08 #4
Hello again anuragshrivastava, :)

Thank you again for replying.

Thanks also for the code tweak. I've probably not asked the question correctly in the first place. I'm actually looking not for an actual solution but an understanding of how to deal with errrors within VB in a very general sense. The code at the top is just a test code to see how the whole error trapping and handling event is carried out, and to show that I have at least tried to do this on my own. I picked the dreaded error 13 data type mismatch to test as it seems to be the one that will most often cause issues if a user doesn't understand or makes an error with data entry. I guess where I've got myself very confused is from the different methodologies of error handling between different versions of VB (2003 - 2005 for instance) and looking at things like C++ etc.

I do appreciate you taking the time to respond and I apologise if I have wasted your time.
Jul 31 '08 #5
I am afraid I can't help u thru the whole exception handler methodology in VB.

Anyways what u can do is to create a new exception handler and may be write the following

ExceptionHandler:
If Err.Number =13
MsgBox "Please enter the number correctly"
End if

If anything else u want to know please feel free to ask.
Aug 1 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Erencans | last post by:
Hi to all, I want to error handling in ASP. But i think that ASP is not enough for error handling. I have got two chance. 1. I can prapare an error page and control throught IIS. 2. I can use on...
8
by: Pete | last post by:
I'm trying to improve my code so that when I open a recordset object, I can absolutely guarantee it is closed and is set = Nothing. I have read some old threads and they all say to use the...
2
by: Steve Richfield | last post by:
My error handler works GREAT. However, VBA seems to have some bugs/features that are causing it fits. The little snippet that I put at the end of each routine looks like this: Error_Handler: If...
3
by: Nathan Bloomfield | last post by:
Hi there, I am having difficulty with a piece of code which would work wonders for my application if only the error trapping worked properly. Basically, it works as follows: - adds records...
4
by: Keith | last post by:
I have the following code in the On No Data event of a report: **** On Error GoTo err_trap MsgBox "No items matching criteria.", vbInformation, gcApplication Cancel = True err_trap: If...
13
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently...
9
by: 47computers | last post by:
Pretty new to PHP, I recently started learning about error trapping. As of right now, I include the following into a page in my website: -------BEGIN PASTE-------- error_reporting(E_ERROR |...
3
by: Jim Armstrong | last post by:
Hello all - This is driving me crazy. I have a table called tblClients - very simple, has the following fields: taxID (PK) ClientName SalesName The main form of my application allows a...
2
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.