PDA

View Full Version : Vb Code To Add A Query


johnsonn239
02-13-2008, 04:43 PM
I need to write a macro that when it's run, it will delete the old query and then import a new database query from another file on a server. I don't know hardly anything about the code for VB and adding a query. I've tried to read about it and I see a lot of ODBC and SLQ stuff but I don't know what that stuff is or how to use it in my code.

By the way, I have tried recording a macro and looking at the code the wizard spits out to see how it works but I can't get the code to work when I attempt to make it ambiguous so it'll work when run in any file. I always get an error that says General ODBC error or it just won't imput the new query.

Here is the code I'm using as of now.

'With ActiveSheet.QueryTables.Add(Connection:= _
"ODBC;DSN=Excel Files;DBQ=equipmentname" _
, Destination:= Range("QueryStart").Offset(1, 0))
.CommandText = strSelect4
.Name = "Query from Excel Files"
.FieldNames = False
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = False
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With

Thanks in advance for you help,
Nic