How to suppress SQL Server Login prompt if append/connection fails?

Anonymous
2016-10-03T23:23:41+00:00

I have been figuring out how to link my SQL Server back end tables to my Access front end using VBA (not via DSN).  I think I worked all of that out in the following post:

https://answers.microsoft.com/en-us/msoffice/forum/msoffice\_access-mso\_winother/how-to-connect-access-front-end-to-sql-server/fe7ff42a-f21d-4ffa-bf1a-bb961064f575

I have it up and running, and it works great.  I'm just thinking through how to handle some errors, and have worked through a lot of that.  But one that I haven't figured out how to handle is if the SQL Server connection fails.  When I test this (by putting in an incorrect password), 

And then, it dumps them out into a dialog box where they can try new passwords!

I don't like this at all!  How can I suppress these dialog boxes?!  I already have error handling defined and SetWarnings = False.  My full code (simplified/censored for posting) is as follows:

Public Function CreateSQLLinkedTable(strSourceTableName As String, strNewTableName As String) As Boolean

On Error GoTo OnError

Dim tdf As TableDef

Dim strConnect As String, strMsg As String

Dim fRetval As Boolean

Dim myDB As Database

    ' turn system warnings off

    DoCmd.SetWarnings False

    ' set database vars

    Set myDB = CurrentDb

    Set tdf = myDB.CreateTableDef(strNewTableName)

    ' define connect string and source table

    With tdf

        .Connect = "ODBC;Driver={SQL Server};server=MyServer;database=MyDatabase;uid=MyUserID;Pwd=MyPassword;"

        .SourceTableName = strSourceTableName

    End With

    ' execute appending the table

    myDB.TableDefs.Append tdf

    ' set bool var to True

    fRetval = True

    ' turn system warnings back on

    DoCmd.SetWarnings True

ExitProgram:

    ' this block of code will run if there are no errors

    ' set the return value = to fRetval (which was set to True above)

    CreateSQLLinkedTable = fRetval

    Exit Function

OnError:

    MsgBox "There was an error connecting to the SQL Server data source. Error = " & Err & ", Description: " & Err.Description

    'exit

    Call CloseFormsAndQuit

End Function

Thanks!

Emily

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
Answer accepted by question author
Anonymous
2016-10-05T10:32:52+00:00

What is your connection string? You can't really use the same one you're using for relinking to make an ADO connection. I use this resource:

https://www.connectionstrings.com/access/

FWIW, I've used this method often to ensure that my connections are live. It will work, if your connection string is correct.

Sorry about the ConnectionString snafu - mine was air code as well.

Was this answer helpful?

0 comments No comments

8 additional answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  4. Anonymous
    2016-10-04T12:22:40+00:00

    If you must do this, I'd try to open a connection to the SQL Server BEFORE running the relink routines. You can try to create an ADO connection, and if that fails alert the user. You'd have to set a reference to the Microsoft ActiveX Data Objects library before doing this:

    On Error GoTo ErrorHandler

    Dim con As New ADODB.Connection

    con.Connection = "Your connection string here"

    con.Open

    ErrorHandler:

    '/ alert the user here

    The ADODB.Connection object also provides you with an Error collection if this occurs, which can give you more information about why the connection failed. To access that collection you'd do this:

    Dim err As ADODB.Error

    For each err in con.Errors

      '/ get info here

    Next err

    ADODB.Connection info: https://msdn.microsoft.com/en-us/library/ms681519(v=vs.85).aspx

    Error example: https://support.microsoft.com/en-us/kb/167957

    Was this answer helpful?

    0 comments No comments