Solved

Hide ODBC popup error message in Access 2007?

Posted on 2010-11-24
11
1,946 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:Bruce Smith
ID: 34210787
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
ID: 34240835
Thanks - I'm out of the office today but when I get back in, I'll try that. . .
0
 

Author Comment

by:jeffcook_lcog
ID: 34261685
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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 11

Expert Comment

by:Bruce Smith
ID: 34262154
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
 

Author Comment

by:jeffcook_lcog
ID: 34262322
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
ID: 34328803

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
ID: 34343744
Have you tried?
Application.DisplayAlerts = False
......your code
Application.DisplayAlerts = True

0
 

Author Comment

by:jeffcook_lcog
ID: 34346215
@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
ID: 34346286
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

813 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

8 Experts available now in Live!

Get 1:1 Help Now