
Originally Posted by
MSP77079
Hi Tuba,
You have 2 option for doing this.
One option is to use an 'external data query' (see selections in Excel under Data >> Import External Data), which will run either an OLEDB or ODBC query.
The other option is to use 'Automation'; this is a "pure VBA" solution in which you would basically be "controlling" Access using VBA in Excel.
Most people use the first option. There are advantages and disadvantages to both. If getting the data into Excel is only the first step, and you will also have a bunch of manipulations in Excel, then the 'Automation' method is probably better for you because it provides complete control over the entire process.
On the other hand, if you are not too confident in your VBA ability, then the 'external data query' option has the advantage that you can get almost to the final solution simply by recording a macro.
For either method to work, you must have a minimal level of competence in SQL.
So working with your suggestions and with some of the links you've provided, I was able to make a little bit of progress. I was able to record the macro using the "Import External Data" as you suggested, and I think that this will probably work out for what I want to do. However, I'm trying to clean up the code, using something you suggested in one of the other posts, and now I'm getting an error message. here is my code.
Dim FirstArray(4) As String
FirstArray(1) = """ODBC;DSN=MS Access Database;"
FirstArray(2) = "DBQ=C:\My Data\Reserve Analysis Redesign\closure.mdb;"
FirstArray(3) = "DefaultDir=C:\My Data\Reserve Analysis Redesign;"
FirstArray(4) = "DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;"""
With ActiveSheet.QueryTables.Add(Connection:=Array(FirstArray), Destination:=Range("A1"))
.CommandText = Array("SELECT `Claim Closure Information`.Claim_Number" & Chr(13) & "" & Chr(10) & "FROM `C:\My Data\Reserve Analysis Redesign\closure`.`Claim Closure Information` `Claim Closure Information`" & Chr(13) & "" & Chr(10) & "WHERE (`Claim Closure Information`.Claim_N" _
, "umber='602-161388')")
.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
Now, Everything is the same as was recorded except for the begining, where I'm trying to use that "FirstArray" string. I'm just trying to clean up that begining part. However, when I do this, I get an error reading "Incomplete Datasource" when the code comes to ".Refresh Background Query:=false".
Any idea? Am i just missing something? Oh yea, here is the code that I recorded that actually works.
With ActiveSheet.QueryTables.Add(Connection:=Array(Array( _
"ODBC;DSN=MS Access Database;DBQ=C:\My Data\Reserve Analysis Redesign\closure.mdb;DefaultDir=C:\My Data\Reserve Analysis Redesign;Dri" _
), Array("verId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;")), _
Destination:=Range("A1"))
.CommandText = Array( _
"SELECT `Claim Closure Information`.Claim_Number" & Chr(13) & "" & Chr(10) & "FROM `C:\My Data\Reserve Analysis Redesign\closure`.`Claim Closure Information` `Claim Closure Information`" & Chr(13) & "" & Chr(10) & "WHERE (`Claim Closure Information`.Claim_N" _
, "umber='602-161388')")
.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
Any help is much appreciated.
Bookmarks