> Actually, the use of the & operator rather then the + operator has
different effects, and has had for quite some time now. You may want
to read up on them in Help. And they apply equally in VB code as well
as Access' Query builder, and SQL strings. Again, I strongly
encourage you to read up on the differences!
Thanks for the tip. Apparently the main difference is that is that & will
force string concatenation while + will add or concatonate, depending on the
expressions involved. But I seem to have trouble with & in compiled queries
for some reason.
Personally, I've given up on doing what you are doing using that
methodology. There's a routine I use called "ExportToExcel" that I've
been using for the last decade or so and it seems to work very well.
I've posted it in this newsgroup a few times. You may want to do a
google search on that name and give it a shot.
I looked for that routine and found different code titled ExportToExcel, but
nothing that looked like your official version. I'd be interested to see
it, but I've come up with my own favorite ExportToExcel function:
Public Function ExportToExcel(strXlsPath As String, _
strSheetName As String, _
strSource As String) As Boolean
On Error GoTo HandleErr
Dim db As DAO.Database
Dim qdfs As DAO.QueryDefs
Dim qdf As DAO.QueryDef
Dim strSql As String
Set db = CurrentDb
Set qdfs = db.QueryDefs
Set qdf = qdfs("qryExportToExcel")
strSql = "SELECT * INTO [Excel 8.0;Database=" & _
strXlsPath & "].[" & _
strSheetName & "] FROM [" & _
strSource & "];"
qdf.SQL = strSql
qdf.Execute (dbFailOnError)
ExportToExcel = True
Exit_Here:
On Error Resume Next
Set qdf = Nothing
Set qdfs = Nothing
Set db = Nothing
Exit Function
HandleErr:
Select Case Err.Number
Case Else
Debug.Print Err.Description
End Select
ExportToExcel = False
Resume Exit_Here
End Function
Here's why I like this (correct me where I'm wrong - I'm sure you know more
about this that I do):
1. It utilizes the JET engine so it's fast.
2. It's versatile because the string variables can be set to anything.
3. It's easy to use because it provides a WYSIWYG view of the export - just
look at the strSource table or query.
I'm using this in a loop where I never know what I'm going to have - data
returned by Query1 and strSheetName are created on the fly. I use the
Scripting Runtime to create a new strXlsPath when I run out of room in
(>255) in the workbook.