473,670 Members | 2,434 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 19969
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
40720
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
5068
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
28474
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
34516
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
7086
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
2775
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
2199
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
1454
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
4354
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
8471
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8386
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
8903
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...
0
7421
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
5686
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
4213
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4393
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1795
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.