Solved

Retreiving data from an Access Database using Microsoft Excel and VB macros

Posted on 2006-11-10
3
302 Views
Last Modified: 2013-12-25
I've recently been given a project to fix which pulls data from tables in an Access database and pastes them into an Excel worksheet.

The problem I'm having is that it only copies some of the data.  There are 1700 records in the table of the databse but when pasted into Excel only pastes around 1500 rows.  The following code is used to import the cases but there doesn't appear to be any set range that stops it importing all of the records.

    '=================== Import Cases from Access ======================
    Set rst = New ADODB.Recordset
    rst.CursorLocation = adUseServer
   
    rst.Open Source:="Table_Cases", ActiveConnection:=cnn, CursorType:=adOpenDynamic, LockType:=adLockOptimistic, Options:=adCmdTable
   
    Worksheets("Escalation Workspace").Select 'Activate
         
    Range("A3").CopyFromRecordset rst


My main question would be is there any limit to the amount of data that can be pulled from Access?

Many thanks
0
Comment
Question by:ward20
3 Comments
 
LVL 65

Accepted Solution

by:
rockiroads earned 500 total points
ID: 17913471
Im not aware of any limits in this
as a test can u try this

first, perform a SELECT * FROM Table_Cases
it shouldnt make a different but try it anyway
then verify the number of records using recordcount property



rst.Open Source:="SELECT * FROM Table_Cases", ActiveConnection:=cnn, CursorType:=adOpenDynamic, LockType:=adLockOptimistic, Options:=adCmdTable

'now add ths
msgbox "Number of Records " & rst.RecordCount


What value do u get in the msgbox?
If u get -1, then try this

rst.MoveLast
rst.MoveFirst
msgbox "Number of Records " & rst.RecordCount



Now what value do u get?


Next, try a looping method
Im trying to remember exact syntax of checking it but its something like this
Paste 1000 rows at a time


dim lRows as long

lRows = 3

do while rst.EOF = False
    Range("A" & lRows).CopyFromRecordset rst, 1000
    lRows = lRows + 1000
loop

0
 
LVL 11

Expert Comment

by:leclairm
ID: 17914965
This is the CopyFromRecordset syntax.

returnValue = SomeRange.CopyFromRecordset(Recordset, MaxRows, MaxColumns)

CopyFromRecordset returns the number of rows copied.  So you can assign it to a variable to compare against the number of records in your recordset.

Is this a new excel sheet or are you pasting it into an existing one??
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 17916704
See if the missing rows contain NULL values.  Excel does not like NULLS.

Leon
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

770 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