Connecting to SQL Server with PSWD and USID automatically

Dscalf1

New Member
Joined
Jun 14, 2015
Messages
40
Hey I have written a macro that automatically pulls a query from access into excel now I am trying to make it where the user does not have to enter the SQL Server user ID and password every time they open the excel sheet because a lot of the users do not know the password.. I have copied my vba code below and any help would be greatly appreciated: (The bolded part is what is not working)

Sub BreadLinePerShift()
ActiveSheet.Range("L1") = Format(Now(), "mm/dd/yyyy hh:nn")


'Step 1: Declare your variables
Dim MyDatabase As DAO.Database
Dim MyQueryDef As DAO.QueryDef
Dim MyRecordset As DAO.Recordset
Dim i As Integer

'Step 2: Identify the database and query
Set MyDatabase = DBEngine.OpenDatabase _
("\\bburockfsxxpp01\public\New Production Folder\WinSPC3.accdb")
Set MyQueryDef = MyDatabase.QueryDefs("Bread Line Per")


'Step 3:Prepare the connection string
cnn.Open _
ConnectionString:="Provider=SQLOLEDB.1;" & _
"Data Source=BBUROCKWSP" & strServerName & ";Initial Catalog=winsp" & strDatabaseName & _
";User Id=wins" & strSQLUsername & ";Password=bbu.win" & strSQLPassword

'Step 4: Define the Parameters
With MyQueryDef
.Parameters("[Start Date]") = Range("B1").Value
.Parameters("[End Date]") = Range("B2").Value
End With

'Step 5: Open the query
Set MyRecordset = MyQueryDef.OpenRecordset

'Step 6: Clear previous contents
ActiveSheet.Range("A5:E10000").ClearContents

'Step 7: Copy the recordset to Excel
ActiveSheet.Range("A6").CopyFromRecordset MyRecordset


'Step 8: Add column heading names to the spreadsheet
For i = 1 To MyRecordset.Fields.Count
ActiveSheet.Cells(5, i).Value = MyRecordset.Fields(i - 1).Name


'Step 9: Sort the data in column
Range("A5:E60").Sort _
Key1:=Range("B5"), Header:=xlYes


'Step 10: Refresh entire workbook
ActiveWorkbook.RefreshAll


Next i
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,168,022
Messages
5,856,852
Members
431,837
Latest member
megantang

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
Top