Article ID: 184591
Article Last Modified on 10/10/2006
Sub Macro1()
With ActiveSheet.QueryTables.Add(Connection:=Array(Array( _
"ODBC;CollatingSequence=ASCII;" & _
"DBQ=D:\Program Files\Microsoft Office\Office;" & _
"DefaultDir=D:\Program Files\Microsoft Office\Office;Delete" _
), Array("d=1;Driver={Microsoft dBase Driver (*.dbf)};" & _
"DriverId=533;FIL=dBase III;ImplicitCommitSync=Yes;" & _
"MaxBufferSize=512;MaxScanRows=8;P"), Array( _
"ageTimeout=600;SafeTransactions=0;Statistics=0;Threads=3;" & _
"UserCommitSync=Yes;")), Destination:=Range("A1"))
.Sql = Array( _
"SELECT Customer.CUSTMR_ID, Customer.COMPANY, Customer.CITY," & _
"Customer.REGION" & Chr(13) & "" & Chr(10) & _
"FROM `D:\Program Files\Microsoft Office\Office`\Customer.dbf " & _
"Customer")
.FieldNames = True
.RefreshStyle = xlInsertDeleteCells
.RowNumbers = False
.FillAdjacentFormulas = False
.RefreshOnFileOpen = False
.HasAutoFormat = True
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SavePassword = True
.SaveData = True
End With
End Sub
If you run Macro1 in Excel 98 Macintosh Edition, you will receive the error
message described in the "Symptoms" section. To adapt this macro so it will
run in both Excel 97 for Windows and Excel 98 Macintosh Edition, change it
to the following:
Sub cross_plat()
Dim mytable As QueryTable
Dim opsys As String
opsys = Application.OperatingSystem
If InStr(opsys, "Windows") > 0 Then
Set mytable = ActiveSheet.QueryTables.Add(Connection:=Array(Array( _
"ODBC;CollatingSequence=ASCII;" & _
"DBQ=D:\Program Files\Microsoft Office\Office;" & _
"DefaultDir=D:\Program Files\Microsoft Office\Office;Delete" _
), Array("d=1;Driver={Microsoft dBase Driver (*.dbf)};" & _
"DriverId=533;FIL=dBase III;ImplicitCommitSync=Yes;" & _
"MaxBufferSize=512;MaxScanRows=8;P"), Array( _
"ageTimeout=600;SafeTransactions=0;Statistics=0;Threads=3;" & _
"UserCommitSync=Yes;")), Destination:=Range("A1"))
mytable.Sql = Array( _
"SELECT Customer.CUSTMR_ID, Customer.COMPANY, Customer.CITY," & _
"Customer.REGION" & Chr(13) & "" & Chr(10) & _
"FROM `D:\Program Files\Microsoft Office\Office`\Customer.dbf " & _
"Customer")
Else
Set mytable = ActiveSheet.QueryTables.Add(Connection:= _
"ODBC;DRIVER={Microsoft 3.01 dBASE PPC};DATABASE=" & _
"HD:Microsoft Office 98:Sample Files:Sample Databases" _
, Destination:=Range("A1"))
mytable.Sql = Array( _
"SELECT CUSTOMER.CUSTMR_ID, CUSTOMER.COMPANY, CUSTOMER.CONTACT," & _
"CUSTOMER.CON_TITLE, CUSTOMER.ADDRESS, CUSTOMER.CITY," & _
"CUSTOMER.REGION, CUSTOMER.ZIP_CODE, CUSTOMER.COUNTRY," & _
"CUSTOMER.PHONE, CUSTOMER.FAX" & vbLf & "FROM CUSTOMER CUSTOMER")
End If
With mytable
.FieldNames = True
.RefreshStyle = xlInsertDeleteCells
.RowNumbers = False
.FillAdjacentFormulas = False
.RefreshOnFileOpen = False
.HasAutoFormat = True
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SavePassword = True
.SaveData = True
End With
End Sub
NOTE: The change in the Cross_Plat macro is the addition of an If-Then-Else
structure that tests for the operating system under which Excel is running.
Based on the operating system, the macro will use one of two connection
strings.
Additional query words: XL98 query XL97
Keywords: kbdtacode kberrmsg kbfaq kbprb KB184591