Link to home
Start Free TrialLog in
Avatar of Sanjay
SanjayFlag for United States of America

asked on

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

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
cnn1.Open

end function
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

<<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.

Jim.
Avatar of Sanjay

ASKER

How do I do option B. via VBA because option C will be very tedious as I have a lot of linked tables.
ASKER CERTIFIED SOLUTION
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.

Jim.
Avatar of Sanjay

ASKER

Thanks Jim.