VBA - Recordset to Array - Catch Overflow error & set to empty

mmohon

New Member
Joined
Nov 19, 2009
Messages
38
I have this code to drop a recordset in an array:
Code:
For r = 1 To rst.RecordCount
           For c = 1 To rst.Fields.Count
               If IsNull(rst(c - 1)) Or IsError(TempArray(r, c) = rst(c - 1)) Then
                   TempArray(r, c) = Empty
               Else
                   TempArray(r, c) = rst(c - 1)
               End If
           Next
           rst.MoveNext
I get overflows, I think because some fields in the Access query result in #Error. It would really make my life easy if I could catch it as it's going into the array and set it to Empty.

I tried with the IsError(rst(c-1)) and IsError(TempArray(r, c) = rst(c - 1)). Neither of those approaches worked.

Is there a better way to catch the overflow and set it to an empty cell?
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
How have you declared the array?

Also, where do you get the overflow?

You mention something about a range but I can't see anything about a range in the code.

By the way, you can get an array from a RecordSet using GetRows.
 
Upvote 0
I declared it like this in the top of my code,
Code:
Dim TempArray()
I grab a record set from an external Access database.

I ReDim the Array to match the size of the record set:
Code:
ReDim TempArray(1 To rst.RecordCount, 1 To rst.Fields.Count)
       For r = 1 To rst.RecordCount
           For c = 1 To rst.Fields.Count
               If IsNull(rst(c - 1)) Or IsError(TempArray(r, c) = rst(c - 1)) Then
                   TempArray(r, c) = Empty
               Else
                   TempArray(r, c) = rst(c - 1)
               End If
               
           Next


           rst.MoveNext


       Next
ws.Range(rng).Resize(rst.RecordCount, rst.Fields.Count).Value = TempArray

I get the overflow on queries where there are #Errors due to some division by Zeroes.
 
Upvote 0
There are errors in the recordset?

Have you tried checking for the error message string rather than using IsError?
 
Upvote 0
Yeah,

There are queries that divide by zero and return an #Error. Unfortunately I have no control over these databases, they are fed to me from another source, and I feed query data into my excel template.

My whole setup is a table with query names, parameters, and destination ranges. My script loops through the table, reaching into a specified database for those queries, and pulls the results back to a range I have specified.

It works great, until there is a division by zero~
 
Upvote 0
I tried it using ADO and a simple Access query.

The query had an expression that returned division by 0 for each record.

When I looped through the recordset it just returned blanks for those.

I never got the chance to check it they were Nulls or whatever, my laptop decided to shutdown.:eek:
 
Upvote 0
Thats ok,

I just did an "On Error Resume Next"
Skipped over the call to set the array value.

Seemed to work just fine.

Looped through my 30+ queries and filled out my excel template.
 
Upvote 0
Oh, well if that works that's the important thing.

Wonder what the problem is though.:)
 
Upvote 0

Forum statistics

Threads
1,224,508
Messages
6,179,188
Members
452,893
Latest member
denay

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top