Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Subscript out of range - Access 2003 - VBA

Posted on 2008-10-14
5
Medium Priority
?
1,078 Views
Last Modified: 2013-11-29
Hello,
     There are a couple of records in the database setup as a test for this.  My code seems to be correct, not sure why I am getting the subscript out of range...  Is there something I am missing with dynamic arrays in Access 2003/VBA?

Here is my code... Thanks..

Private Sub CmdEmailPersonnelInterests_Click()
    On Error GoTo Err_CmdEmailPersonnelInterests_Click
        Dim sSQL_Archaelogy As String
        Dim db As DAO.Database
        Dim rs As DAO.Recordset
        Dim i As Integer
       
 
 
        'create sql query, load results into dynamic array for later.
        If ckArchaelogy = True Then
            sSQL_Archaelogy = "SELECT Email FROM tblVolInfo WHERE Archaelogy = -1"
            Set db = CurrentDb
            Set rs = db.OpenRecordset(sSQL_Archaelogy, dbOpenSnapshot)
            Dim ArchaelogyArray() As String
           
         
            'Load array with email addresses
            i = 0
            Do While Not rs.EOF
                ArchaelogyArray(i) = rs(i).Value
                i = i + 1
            Loop
           
            'close the recordset
            rs.Close
         
            'release memory
            Set rs = Nothing
            Set db = Nothing
        End If
       
       

 

   ' stAppName = "outlook /c ipm.note /m"
    'Call Shell(stAppName, 1)

Exit_CmdEmailPersonnelInterests_Click:
    Exit Sub

Err_CmdEmailPersonnelInterests_Click:
    MsgBox Err.Description
    Resume Exit_CmdEmailPersonnelInterests_Click
End Sub
0
Comment
Question by:strong-daniel
  • 4
5 Comments
 

Author Comment

by:strong-daniel
ID: 22712990
I think I might have to use the redim every time I add something to the array?  

0
 

Author Comment

by:strong-daniel
ID: 22713039
Ok...  Still not working... changed code to get record count, use the count to redim the dynamic array...  still getting the errors...




Private Sub CmdEmailPersonnelInterests_Click()
    On Error GoTo Err_CmdEmailPersonnelInterests_Click
        Dim sSQL_Archaelogy As String
        Dim db As DAO.Database
        Dim rs As DAO.Recordset
        Dim i, iRecCount As Integer
       
 
 
        'create sql query, load results into dynamic array for later.
        If ckArchaelogy = True Then
            sSQL_Archaelogy = "SELECT Email FROM tblVolInfo WHERE Archaelogy = -1"
            Set db = CurrentDb
            Set rs = db.OpenRecordset(sSQL_Archaelogy, dbOpenSnapshot)
            Dim ArchaelogyArray() As String
           
            iRecCount = rs.RecordCount
            ReDim Archaelogy(iRecCount)

            'Load array with email addresses
            i = 0
            Do While Not rs.EOF
                ArchaelogyArray(i) = rs(i).Value
                i = i + 1
            Loop
           
            'close the recordset
            rs.Close
         
            'release memory
            Set rs = Nothing
            Set db = Nothing
        End If
       
       

 

   ' stAppName = "outlook /c ipm.note /m"
    'Call Shell(stAppName, 1)

Exit_CmdEmailPersonnelInterests_Click:
    Exit Sub

Err_CmdEmailPersonnelInterests_Click:
    MsgBox Err.Description
    Resume Exit_CmdEmailPersonnelInterests_Click
End Sub
0
 

Accepted Solution

by:
strong-daniel earned 0 total points
ID: 22713143
I removed the ReDim Archaelogy(iRecCount) and added  ReDim ArchaelogyArray(0 To iRecCount) As String

and now it works...

Weird, I remember in the old Visual Basic days you never had to do that, you could grow or shrink a dynamic array without redim.

Hmm...

0
 
LVL 18

Expert Comment

by:jmoss111
ID: 22713221
Hello strong-daniel,

Chaneg i|RecCount to Long

Regards,

jmoss111
0
 

Author Comment

by:strong-daniel
ID: 22713911
Thanks...  That is a good point...




0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question