473,402 Members | 2,046 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,402 software developers and data experts.

How to trap for ODBC errors

Seth Schrock
2,965 Expert 2GB
I have a database that its BE is SQL Server. Occasionally, I get ODBC errors, but they aren't going through my error trap. This is what I have right now:
Expand|Select|Wrap|Line Numbers
  1. On Error GoTo Error_Handler
  2.  
  3. 'Procedure code here...
  4.  
  5. Exit_Procedure:
  6.     Exit Sub
  7.  
  8.  
  9. Error_Handler:
  10.     Call ErrorMessage(Err.Number, Err.Description, "Module Name")
  11.     Resume Exit_Procedure
  12.     Resume
  13.  
The sub that gets called is:
Expand|Select|Wrap|Line Numbers
  1. Public Sub ErrorMessage (ErrorNumber as Integer, ErrorDescription as string, _
  2.                         Module as string, Optional CodeGroup as Integer = 0)
  3.  
  4. dim strMsg as string
  5.  
  6. strMsg = "Please contact the database administrator and provide the following information:" & _
  7.          vbCrLf & vbCrLf & _
  8.          "Error Number: " & ErrorNumber & VbCrLf & _
  9.          "Description: " & ErrorDescription & VbCrLf & _
  10.          "Module: " & Module & VbCrLf & _
  11.          "Code Group: " & CodeGroup
  12.  
  13. msgbox strmsg, Buttons: = vbCritical, Title: = "My Application"
For some reason though, I'm still getting the normal ODBC error messages instead of my custom one. How do I get the error to go through my trap?
Apr 11 '13 #1

✓ answered by ADezii

You need to use the Errors collection to trap specific Open Database Connectivity (ODBC) Errors. Assuming you are using DAO:
Expand|Select|Wrap|Line Numbers
  1. Function TestODBCErr()
  2. Dim errX As DAO.Error
  3. On Error GoTo ODBCErrHandler
  4.  
  5. 'Function Coding
  6.  
  7. Exit_function:
  8.   Exit Function
  9.  
  10. ODBCErrHandler:
  11.   If Errors.Count > 1 Then    'ODBC related
  12.     For Each errX In DAO.Errors
  13.       Debug.Print "ODBC Error"
  14.       Debug.Print errX.Number
  15.       Debug.Print errX.Description
  16.     Next errX
  17.   Else     'VBA related
  18.     Debug.Print "VBA Error"
  19.     Debug.Print Err.Number
  20.     Debug.Print Err.Description
  21.   End If
  22.     Resume Exit_function
  23. End Function

2 10074
ADezii
8,834 Expert 8TB
You need to use the Errors collection to trap specific Open Database Connectivity (ODBC) Errors. Assuming you are using DAO:
Expand|Select|Wrap|Line Numbers
  1. Function TestODBCErr()
  2. Dim errX As DAO.Error
  3. On Error GoTo ODBCErrHandler
  4.  
  5. 'Function Coding
  6.  
  7. Exit_function:
  8.   Exit Function
  9.  
  10. ODBCErrHandler:
  11.   If Errors.Count > 1 Then    'ODBC related
  12.     For Each errX In DAO.Errors
  13.       Debug.Print "ODBC Error"
  14.       Debug.Print errX.Number
  15.       Debug.Print errX.Description
  16.     Next errX
  17.   Else     'VBA related
  18.     Debug.Print "VBA Error"
  19.     Debug.Print Err.Number
  20.     Debug.Print Err.Description
  21.   End If
  22.     Resume Exit_function
  23. End Function
Apr 11 '13 #2
Seth Schrock
2,965 Expert 2GB
Thanks ADezii. I just realized that I have fixed the error that I was having so now I can't test for it. However, I think that if I have any problems that I will be able to figure them out so I'll go ahead and select your answer as best answer.
Apr 11 '13 #3

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

Similar topics

2
by: Steve | last post by:
I normally use MySQL with PHP, but I'm delving into connecting to Access with ODBC (for a database that I already have set up at work), and I'm running into a couple of errors. I'm just trying to...
0
by: doli | last post by:
Hi, I have the following piece of code which iterates through the potential errors: i =0 For Each error_item in myConn.Errors DTSPackageLog.WriteStringToLog myConn.Errors(i).Description...
3
by: paul_tomlin | last post by:
We connect via terminal services to an excel spreadsheet that has an ODBC link to an access database using a pivot table, when we try to refresh the data within the excel spreadsheet we receive the...
1
by: eight02645999 | last post by:
hi i have a piece of code: .... def connectdb(sql): import dbi import odbc import sys try:
3
by: Detlev Ahlgrimm | last post by:
Hi! I have to use MS-Access2000 as a frontend for an oracle database. And I dont want to see these ORA-xxxx Popups if an error occures. So I use something like the following in access (it should...
14
by: Abhi | last post by:
FYI: This message is for the benefit of MS Access Community. I found that this prblem has been encounterd by many but there is hardly any place where a complete solution is posted. So I thought...
1
by: paul_tomlin | last post by:
We connect via terminal services to an excel spreadsheet that has an ODBC link to an access database using a pivot table, when we try to refresh the data within the excel spreadsheet we receive the...
3
by: Andrew Banks | last post by:
Is it possible to trap all errors within a web app and do the following Send the user to another page "Error.aspx" Send an email to myself with the filename and as much information about the...
3
by: Greg Strong | last post by:
Hello All, Is there any way to close an ODBC connection via DSN without completely closing the Access front-end? I'm doing some testing with using Access as a front-end to Oracle 10g Express...
3
by: jimatqsi | last post by:
I have a client running Access 2003 with a SQL back end. They are reporting a number of ODBC errors recently and complaining the system is crawling. (Each ODBC error takes about 1 minute to come up)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.