Hi Gary,
Yes, I do have another suggestion. I steer away from
TransferSpreadsheet because of the very issue you describe. I use ADO
when I pass data to Excel. This technique is a little more
sophisticated than TransferSpreadSheet but offers a lot more control and
reliability. I will give you a sample code below. First, you need to
make sure you have Mdac2.5 and Mdac2.6 loaded on the computer you will
run this code. Mdac2.5 is the last version of Mdac that contains Jet
for interfacing with Access. Mdac2.6 is an upgrade for 2.5 which really
makes a difference in the ADO code useability. I think if you load 2.6
without having 2.5, the ADO may not work. You should be able to find
Mdac at MSDN or Google. Anyway, you make a reference in Tools/References
to Microsoft ActiveX Data Objects 2.6. Then here is the sample code:
-----------------------------------------------------------
Sub DataToExcelADO()
Dim DB As Database, RS1 As Recordset, strSql As String
Dim j As Integer, t As Integer, RetVal as Variant
Dim cn As New ADODB.Connection, RS As New ADODB.Recordset
Set DB = CurrentDb
strSourcePath = Left(DB.Name, Len(DB.Name) - Len(Dir(DB.Name)))
strSourcePath = strSourcePath & "testExcel.xls;"
RS.CursorLocation = adUseClient
cn.Mode = adModeReadWrite
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strSourcePath & _
"Extended Properties=""Excel 8.0;HDR=NO;"""
'---clear old data first
DoEvents
For j = 2 To 50 'clear old data from Excel Sheet
strSql = "SELECT * FROM [Sheet1$A" & j & ":U" & j & "]"
RS.Open strSql, cn, adOpenDynamic, adLockPessimistic
For k = 0 To RS.Fields.Count - 1: RS(k) = Null: Next
RS.Update
RS.Close
Next
'---populate Sheet1
Set RS1 = DB.OpenRecordset("tblTestData")
f = RS1.Fields.Count
j = 2
DoEvents
Do While Not RS1.EOF
strSql = "SELECT * FROM [Sheet1$A" & j & ":U" & j & "]"
RS.Open strSql, cn, adOpenDynamic, adLockPessimistic
For k = 0 To RS1.Fields.Count - 1
RS(k) = RS1(k)
Next
RS.Update
RS.Close
j = j + 1
RS1.MoveNext
t = t + 1
RetVal = SysCmd(acSysCmdSetStatus, t)
Loop
RS1.Close
cn.Close
End Sub
---------------------------------------------------------
Note that this technique does not use automation. You are referencing a
specific Excel workbook to write to. You can't do that with
TransferSpreadsheet. There are a few Gotcha's though. First, you have
to pre-populate the Excel sheet you are going to write to, save, then
delete the fake data. This preps the sheet for ADO. Then usually, you
can leave the cells in General format, but sometimes ADO complains if
you write data from a Date/Time field in an Access Table. So format
those columns in Excel as Date or Time.
The next Gotcha is that ADO will only write one row at a time to Excel.
If you try Select * from your table of 500 rows, it will write the first
row 500 times to Excel. So you have to close the ADO recordset var RS
for each row. But it is lightening fast. You can write 500 rows in
about 5 seconds, if that. Also note, that for the Excel file that I
reference, you have to end it with a semi colon ";"
strSourcePath = strSourcePath & "testExcel.xls;"
Then, the sql statement
strSql = "SELECT * FROM [Sheet1$A" & j & ":U" & j & "]"
references the sheet you will write to. You can name the sheet anything
you want in Excel, but you have to use the same name in this sql
statement. I am writing here to columns A through U. j is the row
counter. Note in the example code, I start j at Row 2 then I increment
j. I also use
RetVal = SysCmd(acSysCmdSetStatus, t)
to monitor the progress of the Do Loop. SysCmd write to the status bar
at the bottom of Access. But back to the sql statement, you can write
your data to any column, row you want by referencing it in the sql
statement
strSql = "SELECT * FROM [Sheet1$H" & j & ":AD" & j & "]"
This write to columns H through AD. Just make sure you have the same
number of columns in your table. Just a word of FYI, ADO is very
touchy. It does a lot of complaining about data types. So start out
with just text fields, like say a table with 5 columns of text. So your
sql statement would be
strSql = "SELECT * FROM [Sheet1$A" & j & ":E" & j & "]"
The Excel workbook can be open or close. Works much faster if the
workbook is closed. Once you are done passing your data, you can then
use automation to do other stuff. I do this all the time.
HTH
Rich
*** Sent via Developersdex
http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!