Sorry Hal I did not see the step you used to clear out the cells. That part gave me the piece I needed and it is working perfect now.
Thanks again! ![]()
[EMAIL PROTECTED] wrote:
[EMAIL PROTECTED] wrote:
Lonnie,
Be sure to set the DAO reference and the Excel reference. This works in
Access/Excel 2000. Please see comment about where to set column and row
reference. This code currently uses A1 as the starting point.
Good Luck
Hal
Function QrytoExcel(strFile As String, strSheet As String, strQuery As
String)
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim xlApp As Excel.Application
Dim xlWkb As Excel.Workbook
Dim xlSht As Excel.Worksheet
Dim strCurrentField As String
Dim varCurrentValue As Variant
Set db = CurrentDb()
Set rst = db.OpenRecordset(strQuery, dbOpenForwardOnly)
'OpenRecordsetOutput rstTemp
DoCmd.Hourglass True
If Assistant.Visible = True Then
With Assistant
.On = True
.Animation = msoAnimationCheckingSomething
.Visible = True
End With
End If
'**************************************************************************
'Open Spreadsheet
'**************************************************************************
' Start Excel Application
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
' Open File
xlApp.Workbooks.Open (strFile)
'************************************************************************************
'Clear Spreadsheet
'************************************************************************************
'
'Set Sheet = xlApp.activeworkbook.Sheets("ScheduleChangesDetail")
With xlApp
.Sheets(strSheet).Select
.Range("A2:F65536").Select 'Note range selected may need to be
changed
.Selection.ClearContents
End With
Set xlSht = xlApp.ActiveWorkbook.Sheets(strSheet)
j = 1
'Loop through the Microsoft Access field names and create
'the Microsoft Excel labels.
For i = 0 To rst.Fields.Count - 1
varCurrentValue = rst.Fields(i).Name
xlSht.Cells(j, i + 1).Value = varCurrentValue
Next i
j = 2
' Loop through the Microsoft Access records and copy the records
' to the Microsoft Excel spreadsheet.
Do Until rst.EOF
For i = 0 To rst.Fields.Count - 1
varCurrentValue = rst(i)
xlSht.Cells(j, i + 1).Value = varCurrentValue 'Make changes here to
control riw and column
Next i
rst.MoveNext
j = j + 1
Loop
xlApp.Workbooks.Application.Visible = False
xlApp.Application.ActiveWorkbook.Save
xlApp.Application.ActiveWorkbook.Close
xlApp.Quit
' Clear the object variable.
Set xlSht = Nothing
Set xlWkb = Nothing
Set xlApp = Nothing
DoCmd.Hourglass False
With Assistant
.On = True
.Animation = msoAnimationEmptyTrash
.Visible = True
End With
End Function
Hal McGee
Engineering Data Manager
Group Engineering - Process and Compliance
Seating Products Group
B/E Aerospace
Winston-Salem, NC
Lonnie Johnson
<[EMAIL PROTECTED]
om> To
Sent by: AccessDevelopers
AccessDevelopers@ <[email protected]>
yahoogroups.com cc
Subject
08/02/2005 08:36 [AccessDevelopers] Writing to an
AM excel file
Please respond to
AccessDevelopers@
yahoogroups.com
I have a recordset that I would like to write as rows in an excel
spreadsheet.
I want to start on a certain line.
The columns are already predefined and match my recordset.
This will run each month so it will need to overwrite the prior month's
data.
What is the best way to go about this?
May God bless you beyond your imagination!
Lonnie Johnson
ProDev, Professional Development of MS Access Databases
Visit me at ==> http://www.prodev.us
Yahoo! Mail
Stay connected, organized, and protected. Take the tour
Please zip all files prior to uploading to Files section.
YAHOO! GROUPS LINKS
Visit your group "AccessDevelopers" on the web.
To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
May God bless you beyond your imagination!
Lonnie Johnson
ProDev, Professional Development of MS Access Databases
Visit me at ==> http://www.prodev.us
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
Please zip all files prior to uploading to Files section.
SPONSORED LINKS
| Microsoft access database | Free microsoft access database | Microsoft access developer |
| Microsoft access | Microsoft access database design | Microsoft access database training |
YAHOO! GROUPS LINKS
- Visit your group "AccessDevelopers" on the web.
- To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
- Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
