Odbc/sql server access in Excel - repeatedly asking for password

rossbritton26

New Member
Joined
Jul 28, 2011
Messages
33
Hi,

I have created a macro which creates a SQL Server connection and returns data as requested - this works fine, however, my macro involves running the same code a number of times - also works fine, just with different connections - still all working!

However, because I am making a number of connections I am prompted for a password each time the macro creates one the connections.
The password is always the same, is there I way I can maybe add to my code below so it won't require me to input the password each time, e.g. maybe the password somewhere in the code?

Code:
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
        "ODBC;DRIVER=SQL Server;SERVER=(my server name here);UID=(user name here);;APP=2007 Microsoft Office system;WSID=2FDCZ2J;DATABASE=" & strDBName)), Destination:=Range("$A$1")).QueryTable
        .CommandText = Array("sql query here"")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = True
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .SourceConnectionFile = _
        "C:\Users\" & Application.UserName & "\AppData\Roaming\Microsoft\Queries\" & strDBName & ".dsn"
        .ListObject.DisplayName = "Table_" & strDBName & "_(table name here)"
        .Refresh BackgroundQuery:=False
    End With

Thanks.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
I think in here somewhere

"ODBC;DRIVER=SQL Server;SERVER=(my server name here);UID=(user name here);;APP=2007 Microsoft Office system;WSID=2FDCZ2J;DATABASE=" & strDBName)), Destination:=Range("$A$1")).QueryTable

you need

Password=, well the variant that works with server / access, looks like it is missing between the double semi colons
 
Upvote 0
Thanks for the response mole999,

I did try that originally, after reading your response I searched again and found an example where Pwd was used, not Password.

This seemed to work!

Thanks for your prompt :)
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,876
Members
449,056
Latest member
ruhulaminappu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top