Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Export to Excel create an empty sheet on second Export

Status
Not open for further replies.

Pasmardio

Programmer
Jun 17, 2011
1
CA
Hi there,

I have a MS-Access form displaying a list of patient and procedures performed on them. I've been asked to add an "Export to Excel" button, so our users can export the data, reorganize and filter it anyway they want.

The way I'm doing it is:
a) get a reference to the form;
Code:
 Dim frm As Form, destRecSet As DAO.Recordset, intCount As Integer, denom As Boolean
 Dim ws As Excel.Worksheet, mobjXL As Excel.Application, cheminFichier As String
 Set db = CurrentDb
 Set frm = Forms("PROCEDURE - liste procedure")
b) copy the form's recordset in a DAO.Recordset object;
Code:
Set destRecSet = frm.RecordsetClone
c) instantiate an Excel.Application object;
Code:
Set mobjXL = New Excel.Application
    
With mobjXL
  .ScreenUpdating = False
  .Visible = False
  .Workbooks.Add
  .DisplayAlerts = True
d) loop a first time on the Recorset.Fileds property to get the headings names;
Code:
For intCount = 0 To destRecSet.Fields.Count - 1
  .Cells(1, intCount + 1).Value = destRecSet.Fields(intCount).Name
  .Cells(1, intCount + 1).Interior.ColorIndex = 15
Next intCount
e) Copy the recordSet to the declared Range
Code:
  .Range("A2").CopyFromRecordset destRecSet
  .ScreenUpdating = True
End With
f) Manage the filename to save, formatting, etc. and clean-up
Code:
  mobjXL.Quit
  Set frm = Nothing
  Set mobjXL = Nothing
  destRecSet.Close
  Set destRecSet = Nothing
  Set ws = Nothing
  cheminFichier = ""
  Set ra = Nothing
End Sub

Problem is; it'll go smoothly on the firstrun, exporting the data, saving my file, formatting and closing everything, but if I try it another time, it'll only produce an empty spreadsheet, with the header line, but no records.

Looking at it in debug shows there the correct number of re3cords in the recordset object, but they wont appear in my worksheet 'till the Access form have been closed and reopened, or refreshed...

Any idea why that is?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top