Solved

CopyFromRecordset unable to handle fields with null

Posted on 2008-10-01
3
844 Views
Last Modified: 2013-11-29
Method 'CopyFromRecrodset' of ojbect 'Range' failed.  If I remove fields containing null values, this code works.  The data recordset can and will have fields with a null values is there a workaround?  I really need a quick method to export a large recordset to Excel.

   
CellCnt = 1
        For Each fld In rs.Fields
               xlSheet.Cells(1, CellCnt).Value = fld.Name
               xlSheet.Cells(1, CellCnt).Interior.ColorIndex = 33
               xlSheet.Cells(1, CellCnt).Font.Bold = True
               xlSheet.Cells(1, CellCnt).BorderAround xlContinuous
               CellCnt = CellCnt + 1
        Next fld
    
        rs.MoveFirst
        xlSheet.Cells(2, 1).CopyFromRecordset rs
        DoEvents
 
    ' Auto fit columns.
        xlSheet.Cells.EntireColumn.AutoFit
        xlSheet.Cells.Select
        xlSheet.Cells.AutoFilter

Open in new window

0
Comment
Question by:tipvernonia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 22617476

you will need to individually fill out the cells with the specific  rs.fields value

             xlSheet.Cells(2, 1).value=nz(rs("fieldname"),"")  '< for text field
0
 

Author Comment

by:tipvernonia
ID: 22623877
I already used this method.  I was looking for a faster method.  When there are 10,000 row with 28 columns this method is very slow.  
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 22625004
try using the nz function in the query for the field that has null values
use the query for the recordset and use the copyfromrecordset to fill the excel file with values
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question