473,888 Members | 1,279 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Subscript Out of Range" When Setting Array Elements

I'm trying to populate an array with the records from a recordset. But
I'm getting an error. Here's the code:

Dim db As Database, qd As QueryDef, rs As Recordset
Dim MyArray() As Double, i As Integer

Set db = CurrentDb
Set qd = db.QueryDefs("M yQuery")
Set rs = qd.OpenRecordse t
rs.MoveFirst

i = 0
For i = 0 To rs.RecordCount - 1
MyArray(i) = rs![PaymentI] 'Error occurs here
rs.MoveNext
i = i + 1
Next i

Any help would be greatly appreciated.

Nov 13 '05 #1
5 19983
On 27 Feb 2005 17:23:18 -0800, "eliffman" <el******@gmail .com> wrote:

You have not yet dimensioned the array.
If you know the number of elements, write:
Dim MyArray(20) as Double
or to allocate dynamically, write:
Redim MyArray(rs.Reco rdCount)
and don't forget to:
Erase MyArray
at the bottom of your procedure.

And consider using Currency rather than Double.

-Tom.

I'm trying to populate an array with the records from a recordset. But
I'm getting an error. Here's the code:

Dim db As Database, qd As QueryDef, rs As Recordset
Dim MyArray() As Double, i As Integer

Set db = CurrentDb
Set qd = db.QueryDefs("M yQuery")
Set rs = qd.OpenRecordse t
rs.MoveFirst

i = 0
For i = 0 To rs.RecordCount - 1
MyArray(i) = rs![PaymentI] 'Error occurs here
rs.MoveNext
i = i + 1
Next i

Any help would be greatly appreciated.


Nov 13 '05 #2
On 27 Feb 2005 17:23:18 -0800, "eliffman" <el******@gmail .com> wrote:
I'm trying to populate an array with the records from a recordset. But
I'm getting an error. Here's the code:

Dim db As Database, qd As QueryDef, rs As Recordset
Dim MyArray() As Double, i As Integer

Set db = CurrentDb
Set qd = db.QueryDefs("M yQuery")
Set rs = qd.OpenRecordse t
rs.MoveFirst

i = 0
For i = 0 To rs.RecordCount - 1
MyArray(i) = rs![PaymentI] 'Error occurs here
rs.MoveNext
i = i + 1
Next i

Any help would be greatly appreciated.


The cause of your problem is that you haven't specified the dimensions of your
array before trying to use it. You have a second problem in that you are
using rs.RecordCount incorrectly. .RecordCount is only guaranteed to return
the number of records read from the recordset so far, not the total number of
records in the recordset. Finally, it's redundant to initialize a variable
prior to using it in a For loop, though it won't hurt anything.

I'm going to suggest you use a collection instead of an array, but first I'll
show you a way to fix the code using the array...

=====
Dim dbs As DAO.Database, DAO.qdf As QueryDef, DAO.rst As Recordset
Dim MyArray() As Double
Dim lngIndex As Long

Set dbs = CurrentDb
Set qdf = dbs.QueryDefs(" MyQuery")
Set rst = qdf.OpenRecords et

If rst.RecordCount > 0 Then
' RecordCount always greater than zero if recordset has records,
' zero otherwise.
Do Until rst.EOF
' Expand array to make room for new element, and keep existing data.
Redim Preserve MyArray(0 To lngIndex)
MyArray(lngInde x) = rst![PaymentI]

rst.MoveNext
lngIndex = lngIndex + 1
Loop
End If

rst.Close
Set rst = Nothing
Set qdf = Nothing
Set dbs = Nothing

=====

In cases like this, I generally find it makes more sense to use a collection
than an array, though.

=====
Dim dbs As DAO.Database, DAO.qdf As QueryDef, DAO.rst As Recordset
Dim colPayments As New VBA.Collection
Dim lngIndex As Long

Set dbs = CurrentDb
Set qdf = dbs.QueryDefs(" MyQuery")
Set rst = qdf.OpenRecords et

If rst.RecordCount > 0 Then
' RecordCount always greater than zero if recordset has records,
' zero otherwise.
Do Until rst.EOF
' If you don't specify .Value, the field object gets added instead
' of the value.
colPayments.Add rst![PaymentI].Value ' <- .Value is important here!
rst.MoveNext
lngIndex = lngIndex + 1
Loop
End If

rst.Close
Set rst = Nothing
Set qdf = Nothing
Set dbs = Nothing

=====

Nov 13 '05 #3
Thanks so much - it worked perfectly. I didn't realize I needed to
redefine the array since it initially contains no data. The collections
method seems like a better way to go, but I am using this function to
pass an array to the IRR function.

Nov 13 '05 #4
rkc
Steve Jorgensen wrote:
Dim dbs As DAO.Database, DAO.qdf As QueryDef, DAO.rst As Recordset
Dim MyArray() As Double
Dim lngIndex As Long


I've always wondered why people that insist on using type
prefixes as a naming convention accept dim dbs as Database and qdf as
QueryDef, reject index as long in favour of lngIndex, but again accept
MyArray() as double instead of dblarrMyArray. I know it's just
aircode, but come on, use a prefix or don't.


Nov 13 '05 #5
On Mon, 28 Feb 2005 03:36:12 GMT, rkc <rk*@rochester. yabba.dabba.do. rr.bomb>
wrote:
Steve Jorgensen wrote:
Dim dbs As DAO.Database, DAO.qdf As QueryDef, DAO.rst As Recordset
Dim MyArray() As Double
Dim lngIndex As Long


I've always wondered why people that insist on using type
prefixes as a naming convention accept dim dbs as Database and qdf as
QueryDef, reject index as long in favour of lngIndex, but again accept
MyArray() as double instead of dblarrMyArray. I know it's just
aircode, but come on, use a prefix or don't.


Right - I would usually use "Dim adblMyArray() As Double".
Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
40793
by: Dan R Brown | last post by:
I have a large form that is generated dynamically in a jsp using xml / xslt. So, to break up this form into several "tabbed" sections, I break up the form using <div> tags. Each <div style="display:none"> can be displayed by setting the style attribute to "display:", or hidden with "display:none". This gives the illusion that the person filling out the form is switching from page to page...without the overhead of extra hits on the server,...
13
5094
by: Mike Austin | last post by:
Hi all. Just working on a small virtual machine, and thought about using vector iterators instead of pointer arithmetic. Question is, why does an iterator plus any number out of range not generate a out_of_range exception? Maybe this is a gcc issue? I'm using gcc version 3.3.3 (cygwin special). Here's the full sample code: #include <iostream>
8
28505
by: sam | last post by:
hey everybody, this is my first time posting here. i'm pretty new to python and programming in general (as you'll soon work out for yourselves...) i'm trying to code a version of a selection sort and the heart of the code is as follows (no_lines is simply the number of items to be sorted, read out of an input file): for j in range(0, no_lines):
5
34529
by: ubg001 | last post by:
Hey all, I am using Access 2007 and tried to import an excel file by using the Import Wizard. I completed all steps and received an error when I clicked "Finish"... the error came back as "Subscript Out of Range" I googled and lived the issue, but did not find any helpful information. Any suggestions out there J -
4
7112
by: Han | last post by:
when I exe my project in vs.net2005,I got the error following: Debug Assertion Failed! Program:........ File:c:\program files\microsoft visual studio 8\vc\include\vector Line:756 Expression:vector subscript out of range.
17
2794
by: David C. Ullrich | last post by:
Having a hard time phrasing this in the form of a question... The other day I saw a thread where someone asked about overrideable properties and nobody offered the advice that properties are Bad. So maybe we've got over that. I suppose properties could have Bad consequences if a user doesn't know they exist and think that a certain property of an object is just an ordinary attribute. But that applies to
3
2207
by: Lax | last post by:
Isn't it "technically" meaningless to call C a "row major language," since there are no such things as multidimensional arrays in C. In C you can define arrays of arrays, and the way that the declarations work (first index closest to identifier defines the largest arrays) makes it seems like C has row-major multidimensional arrays No need to mention "row major" when discussing C if one talks about "arrays of arrays" and the way C's...
13
1477
by: kj | last post by:
Is there a special pythonic idiom for iterating over a list (or tuple) two elements at a time? I mean, other than for i in range(0, len(a), 2): frobnicate(a, a) ?
7
4380
by: tallman | last post by:
Hi all, I am using the mlabwrap module in python for my thesis and I have encountered an error : some times when I call my matlab function from my python code it stops and shows this error " mlabrap.error: Subscript indices must be either real positive integers or logicals" and the line where this error occured:"l=mlab.tetris_learn(summ)" The funny thing is that this error occurs sometimes after this line was executed with no problem...
0
9800
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11186
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10439
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9597
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7148
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6015
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4642
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3252
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.