Thread: Query Macro
View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Steve Steve is offline
external usenet poster
 
Posts: 1,814
Default Query Macro

Thanks Tim, I will try out and let you know how it works.

"Tim Williams" wrote:

dim SQL as string, MyTable as string


MyTable = Range("A1").Text
SQL= "SELECT POSTINGSEQ, AUDTUSER, AUDTORG, COMMENT " & _
"FROM `C:\Documents and Settings\swells\Desktop\GL
Detail`.`" & MyTable & "`"



..CommandText = Array(SQL)




You don't need the chr(10)+chr(13)

Tim.


"Steve" wrote in message
...
Here is something I came up with and maybe you can help here.

Sub SelectTable()
'
Range("A4").Select
With ActiveSheet.QueryTables.Add(Connection:=Array(Arra y( _
"ODBC;DSN=MS Access Database;DBQ=C:\Documents and
Settings\swells\Desktop\GL Detail.mdb;DefaultDir=C:\Documents and
Settings\swells\D" _
), Array("esktop;DriverId=25;FIL=MS
Access;MaxBufferSize=2048;PageTimeout=5;")) _
, Destination:=Range("A4"))
.CommandText = Array( _
"SELECT POSTINGSEQ, AUDTUSER, AUDTORG, COMMENT" & Chr(13) &
"" &
Chr(10) & "FROM `C:\Documents and Settings\swells\Desktop\GL
Detail`.`GLPJC -
BIR`" _
)
.Name = "Query from MS Access Database"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
End Sub

I would like to be able to change the table called "GLPJC - BIR" in
the FROM
statement to whatever field I want. In the beginning of the code, I
thought
of getting the table name from a specific cell using the following:

MyTable = Range("A1").Text

Any suggestions on how I can do this in the FROM statement?



"Steve" wrote:

Is there a way to create a macro that would look in a certain cell
in a
worksheet to pick which query in Access to use? All queries in
Access would
have the same layout. I also want to use parameters that are in
certain cell
in the same worksheet.