Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / May 2006

Tip: Looking for answers? Try searching our database.

array question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nick I - 24 May 2006 14:36 GMT
I have an array that can go up to 25 columns and 9 rows in size. I am taking
the contents of this array and placing the data into an Excel sheet through
VB.

Sometimes, however, the array may be only 24 or 23 columns in size since
there is no data in the last few columns. When I run my loop that fills the
data, it kicks out with an error "Subscript out of range".

Instead of getting this error, I would just like the function to place a "0"
in the Excel sheet. Is there a way to tell Access that if I get the
"Subscript out of range" error, to have to go ahead an just put a 0 where I
want?

I tried asking if the array is equal to Null....but apparently subscript out
of range and NULL are not the same thing.

Here is a portion of the code below:

NumberForCell = 0
           Down = 0
           Across = 2
           
                     
           'Outer loop will be the across loop
           
           For Across = 2 To 25
               
               Down = 0
               Letter = LetterReturn(Across + 1)
                               
               For Down = 0 To 9
               
                   NumberForCell = Down + 14
                   
                   If IsNull(aryData(Across, Down)) Then
                       .Cells.Range(Letter & CStr(NumberForCell)) = 0
                   Else
                       .Cells.Range(Letter & CStr(NumberForCell)) =
aryData(Across, Down)
                   
                   End If
               
               Next Down
               
           Next Across

Thanks for your help!

Nick
Klatuu - 24 May 2006 14:50 GMT
The subscript out of range is not cause by there being no data in an array
element.  It is caused because the index you are using is greater than the
number of elements that exist.  I don't believe the problem is in how you are
retreiving data from the array, but how you are building the array.  My guess
would be that either you are diminsioning the array incorrectly or you are
using ReDim to dynamically create the array and, in either case, there are
not the number of elements you expect or you are not indexing correctly.

I suggest you review VBA help for Declaring Arrays to ensure you are using
them correctly.

Now, one other hint.

   If IsNull(aryData(Across, Down)) Then
       .Cells.Range(Letter & CStr(NumberForCell)) = 0
   Else
       .Cells.Range(Letter & CStr(NumberForCell)) = aryData(Across, Down)
   End If

can be replaced with

   Nz(.Cells.Range(Letter & CStr(NumberForCell)) = aryData(Across, Down), 0)

> I have an array that can go up to 25 columns and 9 rows in size. I am taking
> the contents of this array and placing the data into an Excel sheet through
[quoted text clipped - 45 lines]
>
> Nick
Nick I - 24 May 2006 14:55 GMT
I know why I am getting the error, I guess I need to figure out a way to
handle it.

Using ReDim does not help, because I still have to fill the Excel sheet with
0's in column 25, even though there may not be any column 25 in the query.
See what I'm saying?

If I ReDim the array correctly to 24 when there are only 24 columns, I won't
get an error, but I won't get the 0's I need in column 25.

> The subscript out of range is not cause by there being no data in an array
> element.  It is caused because the index you are using is greater than the
[quoted text clipped - 68 lines]
> >
> > Nick
Klatuu - 24 May 2006 15:08 GMT
I see what you are saying.  Why would the Redim Preserve not work?
That way, regardless of the number of columns in the query, you could Redim
the array without lossing any data.

> I know why I am getting the error, I guess I need to figure out a way to
> handle it.
[quoted text clipped - 78 lines]
> > >
> > > Nick
Nick I - 24 May 2006 15:36 GMT
How would I do that?

I put this in my code and I still got a subscript error:

Dim aryData
   
       aryData = objRST.GetRows(10)
       ReDim Preserve aryData(UBound(aryData) + 4)

Only this time I got it before the loop. (FYI - In the case I am testing,
the objRST.GetRows(10) function will correspond to an array with 10 rows and
22 columns. Sometimes this column value could be up to 26).

Regardless of the output of my query....23, 24, or 25 columns....I have to
update 25 columns in the Excel sheet I am populating. If there is no data in
the array, then I need to put 0's in those spots.

I just can't seem to figure out how to code that last "If".

Is there a way I could do it through Error Handling? I am not familiar with
much Error Handling.

Thanks for your help Klatuu.

Nick

> I see what you are saying.  Why would the Redim Preserve not work?
> That way, regardless of the number of columns in the query, you could Redim
[quoted text clipped - 82 lines]
> > > >
> > > > Nick
Klatuu - 24 May 2006 16:02 GMT
Your Redim is specifying only the number of rows, not the number of columns.

ReDim Preserve aryData(UBound(aryData) + 4, 25)

> How would I do that?
>
[quoted text clipped - 108 lines]
> > > > >
> > > > > Nick
deltris - 24 May 2006 18:06 GMT
If your array is built from  a query in Access it sounds like its coming from
a crosstab.  You can normally tell the query to produce a specified number
of columns using the Columns Heading property for the query (for example you
have a query that returns info under columns headed 1, 2, 3 .... 22 etc.  
Sometimes more or less)  if you always want to ensure you have columns 1 to
25 simply edit the query (right click in query design) and specify 1,2,3 ...
,23,24,25 in the column heading line.  That way you guarantee the size of the
returned array is always 25 columns, regardless of wether it has info or not
(no info could be checked by len function to get around datatypes etc using a
variant variable).  

Hope this helps

T Delaney, Ireland.

> I have an array that can go up to 25 columns and 9 rows in size. I am taking
> the contents of this array and placing the data into an Excel sheet through
[quoted text clipped - 45 lines]
>
> Nick
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.