473,473 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Subscript out of range problem

122 New Member
Can anyone see what's wrong with this code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command278_Click()
  2. On Error GoTo Err_Command278_Click
  3.  
  4.     Dim stDocName As String
  5.     Dim whereArray()
  6.     Dim stLinkCriteria, fromLName, toLName, strMsg As String
  7.     Dim counter, i As Integer
  8.  
  9.     Select Case optionPrintWhat.Value
  10.     Case 1
  11.         stDocName = "rptPastMenus"
  12.         counter = 0
  13.         If Not IsNull(Me![txtDateSelectFrom]) Then
  14.             whereArray(counter) = "menu.date >=#" & Me![txtDateSelectFrom] & "#"
  15.             counter = counter + 1
  16.         End If
  17.         If Not IsNull(Me![txtDateSelectTo]) Then
  18.             whereArray(counter) = "menu.date <=#" & Me![txtDateSelectTo] & "#"
  19.             counter = counter + 1
  20.         End If
  21.         If Not IsNull(Me![chkSubmitted]) Then
  22.             If IsNull(Me![chkNotSubmitted]) Then
  23.                 whereArray(counter) = "menu.submitted"
  24.                 counter = counter + 1
  25.             End If
  26.         End If
  27.         If Not IsNull(Me![chkNotSubmitted]) Then
  28.             If IsNull(Me![chkSubmitted]) Then
  29.                 whereArray(counter) = "menu.submitted IS NULL"
  30.                 counter = counter + 1
  31.             End If
  32.         End If
  33.         If Not IsNull(Me![cboProviderSelectFrom]) Then
  34.             fromLName = Left(Me![cboProviderSelectFrom], InStr(Me![cboProviderSelectFrom], ",") - 1)
  35.             whereArray(counter) = "provider.lname >='" & fromLName & "'"
  36.             counter = counter + 1
  37.         End If
  38.         If Not IsNull(Me![cboProviderSelectTo]) Then
  39.             toLName = Left(Me![cboProviderSelectTo], InStr(Me![cboProviderSelectTo], ",") - 1)
  40.             whereArray(counter) = "provider.lname >='" & toLName & "'"
  41.             counter = counter + 1
  42.         End If
  43.  
  44.         'Display contents of array (for debugging)
  45.  
  46.         strMsg = "Items in array:" & vbCr & vbCr
  47.         For i = 0 To counter
  48.             strMsg = strMsg & i & ":" & vbTab & whereArray(i) & vbCr
  49.         Next i
  50.         MsgBox strMsg, vbOKOnly + vbInformation, "Where Conditions"
  51.  
  52.  
  53.     Case 2
  54.         stDocName = "rptChildReEnrollment"
  55.     Case 3
  56.         stDocName = "rptClaimSummaryForm"
  57.     Case 4
  58.         stDocName = "rptProviderDetail"
  59.     Case 5
  60.         stDocName = "rptProvMailingLabels"
  61.     Case Else
  62.         stDocName = "rptParentMailingLabels"
  63.     End Select
  64.  
  65.     DoCmd.OpenReport stDocName, acViewPreview, , stLinkCriteria
  66.  
  67. Exit_Command278_Click:
  68.     Exit Sub
  69.  
  70. Err_Command278_Click:
  71.     MsgBox Err.Description
  72.     Resume Exit_Command278_Click
  73.  
  74. End Sub
I'm trying to create an array of strings based on choices made by the user and then concatenate the strings into one string that I can use as the Where Condition for the DoCmd.OpenReport method.

Currently, this is throwing an error on line 14 telling me the "Subscript is out of range." I do not have Option base 1 set, so it should default to option base 0, right? So, if counter equals 0, then whereArray(0) should be the first location in the array, shouldn't it? Where is my logic failing?
Aug 13 '08 #1
4 2853
Annalyzer
122 New Member
Update: I got this code to work by declaring the array as a fixed-size array, but I don't understand why that worked. Isn't it better to declare a dynamic array if you don't know how many elements will be stored in the array? If someone can explain this to me, I would be grateful.
Aug 13 '08 #2
Krandor
50 New Member
You are using an empty array and then you are trying to insert a value into an array cell that doesn't exist. Big problem.

If you want to dynamically add a new element to the array each time you loop, then you have to use
Redim Preserve YourArray(1 to Ubound(YourArray)+1)
just before your data insertion.

Make sure and code
Redim Preserve YourArray(1 to 1)
before your loop so the array is at least initialized, otherwise it will fail on the very first attempt.
Aug 13 '08 #3
Annalyzer
122 New Member
Thanks! I'll give that a try.
Aug 14 '08 #4
NeoPa
32,556 Recognized Expert Moderator MVP
As a general rule, redimming an array in each iteration of a loop is not to be encouraged. If it is important to save as much space as possible, then consider redimming every 10 iterations or so.
Aug 15 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: andy.rich | last post by:
I am getting the following error and I do not know why. Can anyone help? -------------------------------------------------------- this is what appears on the screen...
2
by: jv | last post by:
I have a form that is used every day to create quotes. However, every 2 weeks or so a user would randomly get an error 9 - subscript out range message. I'm not using any array in this form. Does...
8
by: VB Programmer | last post by:
I'm acutally using VB6, not VB.NET, but I couldn't find the newsgroup for version 6.... I need help for something that should be simple. I keep getting a "subscript out of range" error and...
51
by: Pedro Graca | last post by:
I run into a strange warning (for me) today (I was trying to improve the score of the UVA #10018 Programming Challenge). $ gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc...
0
by: wennerd | last post by:
I am getting a Subscript out of range after executing the following command Set oRstPick = connTemp.Execute(sSQL) It is a simple query: sSQL = "SELECT idCategory, categoryDesc, idParentCategory...
6
by: josh | last post by:
Hi I've a dubt! when we have overloaded functions the compiler chooses the right being based on the argument lists...but when we have two subscript overloaded functions it resolves them being...
6
by: Andy | last post by:
Hi all, I started developing a little app on my Mac using XCode some month ago. The app is running fine on my mac like a sharm. Now I am nearly ready and yesterday I moved the whole source code...
4
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 ...
1
by: godhulirbalaka | last post by:
Dear all, I have a problem with MSFlexGrid. I have few field and one MSFlexGrid So i want if i doubleclick any row then the datas of that row distribute to those field I already wrote the command...
6
by: brandon01 | last post by:
Keep getting "subscript out of range" any idea why? This function is looped btw.. Thx... Private Sub getBday() bDay = List1.List(nextBday) bDayArr = Split(bDay, " - ") Text1.Text =...
0
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,...
1
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...
0
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...
0
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
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...
0
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 ...
0
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...

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.