Hi,
I am new to macros and trying out my luck for the first time. My requirement is to run a query against sql server db to retrieve data. The data will be retrieved\refreshed each time a Form Button is pressed. I have added the query\DB connection using Data
Connection Wizard. But it would not work for all users as sharing of passwords might be an issue. So I am using Macros and have the below code right now but it is throwing run-time error '1004' at the connection statement.
Sub Submit_Refresh()
With ActiveSheet.QueryTables.Add(Connection:= _
"Provider=SQLOLEDB.1;Persist Security Info=True;Password=Pass123;User ID=dbuser;Initial Catalog=testdb;Data Source=sqldb;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=qalab;Use Encryption for Data=False;Tag with column collation
when possible=False;" _
, Destination:=Range("J1"))
.Sql = Array( _
"select BG_STATUS, BG_PRIORITY, BG_BUG_ID from BUG" _
)
.FieldNames = False
.RefreshStyle = xlOverwriteCells
.RowNumbers = False
.FillAdjacentFormulas = True
.RefreshOnFileOpen = False
.HasAutoFormat = True
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SavePassword = True
.SaveData = True
End With
End Sub