473,699 Members | 2,433 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error Handling/Trapping Question

JodiPhillips
26 New Member
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 1784
anuragshrivastava64
66 New Member
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.SetFoc us
Exit Sub

ErrorHandle:
MsgBox ("Please enter a number.")
Resume ExitHere
End Sub
Jul 31 '08 #2
JodiPhillips
26 New Member
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.SetFoc us
Exit Sub

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

Hello anuragshrivasta va

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 programmaticall y 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...els e (which still doesn't work to stop the in-built error handling).

Thanks again.

JP
Jul 31 '08 #3
anuragshrivastava64
66 New Member
Hello anuragshrivasta va

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 programmaticall y 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...els e (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.Te xt1) Then

MsgBox "Sorry wrong entry"
Text1= ""
Text1.setfocus
Else
aNumberbox = CheckData(Me.Te xt1)
End If
Jul 31 '08 #4
JodiPhillips
26 New Member
Hello again anuragshrivasta va, :)

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
anuragshrivastava64
66 New Member
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

ExceptionHandle r:
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
2253
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 error resume next. For 1. This is a general handling. I want to handle local error. Maybe i do special thing when error occured. For 2. I have to write alot of lines code. I have to control every line.
8
3377
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 functional equivalent of the following code: Public Sub CloseRecordset() On Error GoTo Sub_Error Dim rs As Recordset rs.FindFirst "This Will Error"
2
2378
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 Error_Handler("<routine name>", Err) = acDataErrDisplay Then On Error GoTo 0 Stop: Resume ' Press twice to view the problem.
3
6896
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 from rsSource into rsDest - if it finds a key violation then it deletes the current record from rsDest and adds the new record from rsSource. This works perfectly - but only for the first found duplicate record, it brings up the error
4
9448
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 Err.Number = 2501 Then Exit Sub
13
4478
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 runs into problems on the system where it will actually be used, and since I used so little error-trapping it dies very ungracefully. I will of course try to fix whatever is causing the error and add error-trapping to the functions where the...
9
2107
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 | E_PARSE); set_error_handler("SendErrorReport"); function SendErrorReport($errorNumber, $errorMessage, $errorFile, $errorLine, $vars) {
3
5169
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 user to select a client and
2
19474
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 will be writing this article intended for those who are in the same level, or maybe lower, of my technical knowledge. I would be using layman's words, or maybe, my own words as how I understand them, hoping, you will understand it the same way that...
0
2897
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 will be writing this article intended for those who are in the same level, or maybe lower, of my technical knowledge. I would be using layman's words, or maybe, my own words as how I understand them, hoping, you will understand it the same way that...
0
8687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8615
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9174
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9034
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7750
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6534
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5874
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3057
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2347
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.