how to bypass the SQL login prompt in Microsoft Access using VBA

Posted on 2012-08-30
Last Modified: 2012-09-04
When I open my access database that has linked tables (via ODBC connection under System DSN) to SQL Server, I automatically have a form open at Startup.  This form contains data from the linked SQL table.  So when then form opens, I get a login prompt to enter my user name and password.  Which I have to enter each time I open my access database (one time only during each opening).  So I created a VBA function that I use in my "AutoExec" macro to attempt to pass this login info.  The macro does not fault out when I open the access database, but I am still prompted to enter the login credentials that I would like to pass using VBA.  How can I automatically send the login credentials?  Thanks.

Public Function Autoconnect()

Dim cnn1 As Object
Dim strCnn1 As String

strCnn1 = "Provider=SQLOLEDB;Data Source=xxx.x.x.xx;Initial Catalog=QA;User ID=test;Password=;"
Set cnn1 = CreateObject("ADODB.Connection")
cnn1.ConnectionString = strCnn1

end function
Question by:sxxgupta
    LVL 56

    Expert Comment

    by:Jim Dettman (Microsoft MVP/ EE MVE)
    <<When I open my access database that has linked tables >>

      You have to either:

    A. provide the username and password as part of the DSN

    B. provide the username and password as part of each tables connect property.

    C. When creating the linked table, check the "save password" check box in the dialog.  Access will cache the credentials used.  There is no way to view or change these.  For a password change, you would need to delete the table link and re-create it.


    Author Comment

    How do I do option B. via VBA because option C will be very tedious as I have a lot of linked tables.
    LVL 56

    Accepted Solution

    Take a look at the following article:

    Which has a routine called FixConnections().  You'll find it easy to modify that and add the username and password to the connect string using that procedure.

    LVL 56

    Expert Comment

    by:Jim Dettman (Microsoft MVP/ EE MVE)
    Note on the above: it actually deletes and recreates the tabledefs().  If you go that route, make sure you have a backup.

    The important thing however is to see how he's looping through the TableDefs Collection.

     In your case, all you need to do is append the USERNAME and PASSWORD arguments to the connect string and update the connect property assuming you still wish to continue using the DSN.


    Author Closing Comment

    Thanks Jim.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    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…
    I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

    779 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

    15 Experts available now in Live!

    Get 1:1 Help Now