Solved

Hide ODBC popup error message in Access 2007?

Posted on 2010-11-24
11
1,902 Views
Last Modified: 2013-11-27
My app uses an Access front-end with a SQL Server 2005 back end.  I have an init() VBA function in Access that runs when the main form is opened up.  That function tries to query the SQL database using the SQL Native ODBC client driver and, if that fails, then tries to use the SQL MDAC ODBC client driver.  All of my users are running Windows XP and have one or both of these client drivers installed.

When using Access 2003, this failover works great - the initial ODBC error (3151 - connection to <DB> failed) generated was trapped and no error was generated and the failover to the MDAC driver was transparent to the end user.

Now, some of the users are using Access 2007 and while the init() function is still trapping the 3151 error - Access first displays the ODBC error message "connection to <DB> failed", forcing the user to press the OK button to acknowledge the error before the error handling continues and properly fails over to the MDAC driver.

Everything still works fine but I don't want my users to have to press OK to acknowledge this error.  Any suggestions on how to avoid displaying the initial ODBC error in Access 2007?
Public Sub Init() 'Called by OPEN FORM event

On Error GoTo Check_Err
    
    Dim rs As DAO.Recordset
    Set dbCurrent = CurrentDb()
    
' First try to connect using SQL Native Client driver
    Native_Fail = False
    FMRdsn = "LCOG_FMR_ODBC_NATIVE"
    Set rs = dbCurrent.OpenRecordset(FMRdsn, dbOpenDynaset, dbSeeChanges) 'If driver unavailable, generates error 3151
    Set rs = Nothing 'close recordset
    Exit Sub

Native_No:
' If native driver unavailable, try to connect using MDAC ODBC driver
    FMRdsn = "LCOG_FMR_MDAC_ODBC"
    Set rs = dbCurrent.OpenRecordset(FMRdsn, dbOpenDynaset, dbSeeChanges) 'If driver unavailable, generates error 3151
    Set rs = Nothing 'close recordset
    Exit Sub
    
Check_Err:
    Select Case Err.Number
        Case 3151 'Unable to connect using an ODBC driver
            If FMRdsn = "LCOG_FMR_ODBC_NATIVE" Then
                Native_Fail = True 'Native Client didn't work, try MDAC
                Resume Native_No
            ElseIf Native_Fail Then 'Both MDAC and SQL Native Client ODBC driver connections failed.
                    MsgBox ("Cannot connect to LCOG SQL database, check ODBC drivers - switching to local database"), _
                    vbOKOnly + vbCritical, "Cannot connect to LCOG SQL database (Error #" & Err.Number & ")"
                    FMRdsn = "local_FMR"
            End If
        Case Else
            MsgBox Err.Description & " (" & Err.Number & ")", vbOKOnly + vbCritical, "Error"
            If Not IsAdmin Then Application.Quit
    End Select
Set rs = Nothing 'close recordset
End Sub

Open in new window

0
Comment
Question by:jeffcook_lcog
11 Comments
 
LVL 11

Expert Comment

by:patsmitty
Comment Utility
The version for Access 2007 is 12.0. So you could say:

If Application.Version = "12.0" Then
DoCmd.SetWarnings False
End If

Open in new window

0
 

Author Comment

by:jeffcook_lcog
Comment Utility
Thanks - I'm out of the office today but when I get back in, I'll try that. . .
0
 

Author Comment

by:jeffcook_lcog
Comment Utility
Well, the ODBC error still appears adding DoCmd.SetWarnings False.

Also, I was mistaken about it working in 2003 - it does the same thing in that version as well. . .
0
 
LVL 11

Expert Comment

by:patsmitty
Comment Utility
I was afraid of this. Try this instead:

Err_Open_:
if err.number <> 3151 then
MsgBox Err.number & " " & Err.Description
Endif
Resume Exit_Open_

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:jeffcook_lcog
Comment Utility
If you look at my code above, you'll see that I'm already catching the 3151 error and resuming.  The problem appears to be that the ODBC error is displayed before Access can catch it. . .  If I remove the OnError statement, I get TWO error messages - the first one being the ODBC error I mention above and the second being the Access 3151 error. . .

Thanks for your help!

Jeff
0
 
LVL 24

Accepted Solution

by:
Bitsqueezer earned 500 total points
Comment Utility

Hi,

unfortunately VBA cannot trap any error. But maybe in this case a simple solution could be to use the Form_Error event on the form where you call the Init function instead of the Init. If the form contains an access to the recordset using the SQLNCLI driver then the Form_Error event can trap some of the errors VBA is not able to catch.

But the better method at all would be to
1. never use an external ODBC connection which needs an ODBC definition in the operating system of the target computer - simply change all links to DSNless connections. The method is descibed here:
How to create a DSN-less connection to SQL Server for linked tables in Access
2. don't use two different drivers on the same database - if the user's computer has not installed the SQLNCLI driver, simply install it. Here are some hints how to test the existence of the driver on the target computer and how to install it with your application:
Test if sqlncli is installed

If you test the described registry key or anything else to see if the driver is installed you don't need to open a recordset and so you will not get an error which maybe is not trappable in Access.

Cheers,

Christian
0
 
LVL 16

Expert Comment

by:13598
Comment Utility
Have you tried?
Application.DisplayAlerts = False
......your code
Application.DisplayAlerts = True

0
 

Author Comment

by:jeffcook_lcog
Comment Utility
@13598 - there is no such command in Access VBA.  The Access equivalent is DoCmd.SetWarnings False already suggested by patsmitty above.
0
 

Author Closing Comment

by:jeffcook_lcog
Comment Utility
Christian,
  Your idea of checking for the Native Client and installing it if it is not there is a great solution.  For now, your comment gave me the idea to check for the Native Client and use it if it is there, otherwise use the MDAC client and that solves my immediate problem.  When I get a chance, though, I'll work on implementing your better solution.  Thanks!

Jeff
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now