How can I run a query(dqy file) from an Excel macro

B

Bill B

I have queries stored in -- \Application Data\Microsoft\Queries. I'd like to
run them from an Excel macro. Does anyone know how to code?

thanks,
Bill B
 
G

Gary Brown

I ran a macro and modified it a little.
'/=================================================/
Sub Macro1()
Dim strConnection As String
Dim strFullFileName As String, strFolder As String
Dim strQueryName As String

strQueryName = "DeleteMe"
strFolder = "D:\Temp\"

strFullFileName = _
strFolder & strQueryName & ".dqy"
strConnection = "FINDER;" & strFullFileName

With ActiveSheet.QueryTables.Add( _
Connection:=strConnection, _
Destination:=Range("A1"))
.Name = strQueryName
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
End Sub
'/=================================================/
HTH,
Gary Brown
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top