Solved

redim array problem

Posted on 2000-04-10
7
306 Views
Last Modified: 2011-09-20
Having problem with reading a sequential access file into an array and calculating average and high score.

i think its the way i declare the redim?

Any suggestions??

Slammer14
Option Explicit

'Dim intScoreArray(1 To 20) As Integer
Dim intNumData As Integer
Dim intRecCount As Integer

Private Sub cmdAvg_Click()
    Dim intNum As Integer, intTotal As Integer, sngAvg As Single
    For intNum = 1 To intRecCount
        intTotal = intTotal + intScoreArray(intNum)   'accumulate array scores
    Next intNum
    sngAvg = intTotal / intRecCount                        'calculate average
    lblMsg.Caption = "Average"                        'display results
    lblAnswer.Caption = Format(sngAvg, "standard")
End Sub

Private Sub cmdExit_Click()
    Unload frmTest
End Sub

Private Sub cmdHigh_Click()
    Dim intNum As Integer, intHigh As Integer
    intHigh = intScoreArray(1)         'initialize intHigh to first array element
    For intNum = 2 To intNumData               'compare intHigh to remaining array elements
        If intScoreArray(intNum) > intHigh Then  'element is greater than intHigh
            intHigh = intScoreArray(intNum)
        End If
    Next intNum
    lblMsg.Caption = "Highest"         'display results
    lblAnswer.Caption = intHigh
End Sub

Private Sub cmdUpdate_Click()
    Dim intNum As Integer, intIncrease As Integer
    intIncrease = Val(InputBox("Enter the number of additional points:"))
    For intNum = 1 To intNumData        'add additional points to each score in the array
        intScoreArray(intNum) = intScoreArray(intNum) + intIncrease
    Next intNum
    lblMsg.Caption = "Scores updated by"        'display message
    lblAnswer.Caption = intIncrease
   
    Open "a:\Tut10\Test11.dat" For Output As #1   'open file
    For intNum = 1 To intNumData                      'write array contents to file
        Write #1, intScoreArray(intNum)
    Next intNum
    Close #1                                    'close file
End Sub
Private Sub Form_Load()

    frmTest.Left = (Screen.Width - frmTest.Width) / 2
    frmTest.Top = (Screen.Height - frmTest.Height) / 2
   
    Dim intNum As Integer, strNext As String, intRecord As Integer
    Dim i As Integer
    Open "a:\Tut10\test11.dat" For Input As #1            'open file
   
    intRecord = 0
   
    Do While Not EOF(1)
        ReDim intScoreArray(0) As Integer
        Input #1, intNum
        ReDim Preserve intScoreArray(1 To intNum) As Integer
             For i = 1 To intNumData
       
            Input #1, intScoreArray(i)
            intRecCount = intRecCount + 1                    ' establish amount of records for upperbound
            Next
    Loop
  '
    Close #1
   
End Sub




                       


0
Comment
Question by:slammer14
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 6

Expert Comment

by:Marine
ID: 2702666
I am not sure but i don't think you can REDIM Array that has been dimentioned.
i tried it like this and it gave error.
if array is dimintioned ex.
Dim r(4) as integer and then you redim you get an error array dimentioned
but if array was declared without given dimention like
Dim r() as integer and then you redim it
Redim r(1 to 10) as integer
no error will occur.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2702725
Do While Not EOF(1)
        ReDim intScoreArray(0) As Integer
        Input #1, intNum
        ReDim Preserve intScoreArray(1 To intNum) As Integer
             For i = 1 To intNumData
         
            Input #1, intScoreArray(i)
            intRecCount = intRecCount + 1                    ' establish amount of records for upperbound
            Next
    Loop
 

I dont see why you are using two Redim statements here.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2702789
also you state For i = 1 to intNumData in your form load event.  I see where you declare intNumData but I dont see anywhere where you assign a value to it.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:wsh2
ID: 2702836
Too much to comment on.. take a look at this.. <huge smile>.. Great to SIEZE ya again !!!

<----- Code Begin ----->

Option Explicit

Private m_intCount As Integer
Private m_intScores(1 To 20) As Integer
Private m_strFilePath As String

Private Sub cmdAvg_Click()
   
   Dim intTotal As Integer
   Dim intIndex As Integer
   For intIndex = 1 To m_intCount
       intTotal = intTotal _
           + m_intScores(intIndex) ' accumulate array scores
   Next intIndex
   
   lblMsg.Caption = "Average" ' display results
   Dim sngAverage As Single
   sngAverage = intTotal / m_intCount ' calculate average
   lblAnswer.Caption = Format(sngAverage, "standard")

End Sub

Private Sub cmdExit_Click()
   
    Unload frmTest

End Sub

Private Sub cmdHigh_Click()
   
   Dim intMaximum As Integer
   intMaximum = m_intScores(1) ' initialize intMaximum to first array element
   
   Dim intIndex As Integer
   For intIndex = 2 To m_intCount ' compare intMaximum to remaining array elements
      If m_intScores(intIndex) > intMaximum _
      Then  'element is greater than intMaximum
         intMaximum = m_intScores(intIndex)
      End If
   Next intIndex
   
   lblMsg.Caption = "Highest" ' display results
   lblAnswer.Caption = intMaximum

End Sub
Private Sub cmdUpdate_Click()
   
   Dim intTotal As Integer
   Dim intIndex As Integer
   For intIndex = 1 To m_intCount ' add additional points to each score in the array
      m_intScores(intIndex) = Val(InputBox( _
         "Enter the number of points:", _
         "Entry# " & intIndex & " Of " & m_intCount, _
         m_intScores(intIndex) _
         ))
      intTotal = intTotal + m_intScores(intIndex)
   Next intIndex
   
   lblMsg.Caption = "Scores Total " ' display message
   lblAnswer.Caption = intTotal
   
   On Error Resume Next
   Kill m_strFilePath
   On Error GoTo 0
   Open m_strFilePath For Output As #1 ' open file
   For intIndex = 1 To m_intCount ' write array contents to file
       Write #1, m_intScores(intIndex)
   Next intIndex
   Close #1 ' close file

End Sub

Private Sub Form_Load()

   frmTest.Left = (Screen.Width - frmTest.Width) / 2
   frmTest.Top = (Screen.Height - frmTest.Height) / 2
   
   m_strFilePath = "a:\Tut10\test11.dat"

   Dim intTotal As Integer
   On Error Resume Next
   Open m_strFilePath For Input As #1 ' open file
   If Err.Number > 0 _
   Then
      Err.Clear
   Else
      Dim intIndex As Integer
      Do Until EOF(1) _
      Or intIndex = UBound(m_intScores)
        intIndex = intIndex + 1
        Input #1, m_intScores(intIndex)
      Loop
   End If
   Close #1
   
   lblMsg.Caption = "Scores Loaded " ' display message
   lblAnswer.Caption = intIndex
   m_intCount = UBound(m_intScores)
     
End Sub

<----- Code End ----->
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 2703488
Try this


Option Base 1


Dim intScoreArray() As Integer

to redim the array:

ReDim Preserve intScoreArray(intNum)
             
To erase the array

erase intScoreArray


0
 

Expert Comment

by:sreekrish
ID: 2703528
Redim will give you error in the following case

Dim iaScore(4) as integer
....
....
....
....
Redim iaScore(20) as integer

-- error because iaScore should not be dimensioned when declare the dimension statement should be dim a() as integeriin that case it will work.
0
 
LVL 11

Accepted Solution

by:
bmatumbura earned 50 total points
ID: 2712777
If you cut and pasted your code above then the line after Option Explicit should read:

  Dim intScoreArray() As Integer

to declare a dynamic array.

You can use the Dim statement with empty parentheses to declare a dynamic array. After declaring a dynamic array, use the ReDim statement within a procedure to define the number of dimensions and elements in the array. If you try to redeclare a dimension for an array variable whose size was explicitly specified in a Private, Public, or Dim statement, an error occurs.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

751 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