?
Solved

VB 6.0 Sequencial Access File and a One-Dimenaional array

Posted on 2003-03-10
15
Medium Priority
?
278 Views
Last Modified: 2012-08-13
OK, here it goes:  this VB code should include a form that has four label controls and three command buttons (a find, print and exit button)

Within the code:

- in the form's General declaration, declare a 10-element one-dimensional Integer array named intNumbers
- in the form's Load even, enter 10 numbers in the array.  (The numbers are to be stored in a .dat file)
- code the "Find" command button so that it displays lowest number of points stored in the array and displays the # of students earning that score, (i.e. the number of times that number appears in the array)
- the click even of the "Find" button should also display the highest class score and also the average class score and total number of student records that were used to process the info.


ANy addditional info needed, please let me know.
0
Comment
Question by:GlobalFax
[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
  • 8
  • 7
15 Comments
 
LVL 11

Expert Comment

by:supunr
ID: 8108080
Option Explicit
Option Base 1

Private intNumbers() As Integer

Private Sub cmdFind_Click()
    Dim i As Long
    Dim ArrayCount As Long
    Dim MinScore As Integer
    Dim MinScoreCount As Long
    Dim MaxScore As Integer
    Dim totalScore As Long
   
    ArrayCount = UBound(intNumbers)
    MinScore = 32000
    MinScoreCount = 0
    MaxScore = -32000
    totalScore = 0
    For i = 1 To ArrayCount
        If (intNumbers(i) < MinScore) Then
            MinScore = intNumbers(i)
            MinScoreCount = 1
        ElseIf (intNumbers(i) < MinScore) Then
            MinScoreCount = MinScoreCount + 1
        End If
        If (intNumbers(i) > MaxScore) Then
            MaxScore = intNumbers(i)
        End If
        totalScore = totalScore + intNumbers(i)
    Next i
   
    MsgBox "Minimum Score = " & MinScore & vbCrLf & "No of occurances = " & MinScoreCount & vbCrLf & _
            "Average Class Score  = " & (totalScore / ArrayCount) & vbCrLf & _
            "Number of students = " & ArrayCount
End Sub

Private Sub Form_Load()
    Dim i As Long
   
    ReDim intNumbers(10) As Integer
   
     ' just put some random numbers to the array
    Randomize
    For i = 1 To 10
        intNumbers(i) = Int(Rnd * 100)
    Next i
End Sub
0
 
LVL 1

Author Comment

by:GlobalFax
ID: 8108379
ArrayCount = UBound, would this be my dat file?


0
 
LVL 1

Author Comment

by:GlobalFax
ID: 8108382
ArrayCount = UBound, would this be my dat file?


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Expert Comment

by:supunr
ID: 8108436
No...here is an update code to get data from the file

Option Explicit
Option Base 1 ' make arrays start from 1, not 0

Private intNumbers() As Integer

Private Sub cmdFind_Click()
   Dim i As Long
   Dim ArrayCount As Long
   Dim MinScore As Integer
   Dim MinScoreCount As Long
   Dim MaxScore As Integer
   Dim totalScore As Long
   
   ' UBount means the upper bound of the array
   ArrayCount = UBound(intNumbers)
   MinScore = 32000
   MinScoreCount = 0
   MaxScore = -32000
   totalScore = 0
   For i = 1 To ArrayCount
       If (intNumbers(i) < MinScore) Then
           MinScore = intNumbers(i)
           MinScoreCount = 1
       ElseIf (intNumbers(i) < MinScore) Then
           MinScoreCount = MinScoreCount + 1
       End If
       If (intNumbers(i) > MaxScore) Then
           MaxScore = intNumbers(i)
       End If
       totalScore = totalScore + intNumbers(i)
   Next i
   
   MsgBox "Minimum Score = " & MinScore & vbCrLf & "No of occurances = " & MinScoreCount & vbCrLf & _
           "Average Class Score  = " & (totalScore / ArrayCount) & vbCrLf & _
           "Number of students = " & ArrayCount
End Sub

Private Sub LoadDataFromDatFile()
   On error goto OpenErr
   Dim i As Long
   Dim FileName as String
   Dim NumbersCount as long
   Dim strLine as String

   FileName = "C:\MyData.Dat"

   ' open the file
   Open FileName For Input As #1
   
   NumbersCount = 0
   Do While (Not EOF(1))
      ' read line by line and add to the array list
      ' assuming that your dat file is just numbers in each line.
      Line Input #1, strLine
     
      if (Trim(strLine) <> "") then ' not an empty line
          NumbersCount = NumbersCount + 1
          ReDim Preserve intNumbers(NumbersCount) As Integer
          intNumbers(NumbersCount) = Val(strLine)
      end if
   Loop

   ' close the file
   Close #1  
   Exit Sub

OpenErr:
   MsgBox "Error Opening the data file." & vbcrlf & err.number & ": " & err.description
   On error goto 0  
End SUb

Private Sub Form_Load()
   Call LoadDataFromDatFile
End Sub
0
 
LVL 11

Expert Comment

by:supunr
ID: 8108446
If you want to save data later on, then you can use function similar to below...


Private Sub SaveDataFromDatFile()
   On error goto OpenErr
   Dim i As Long
   Dim FileName as String

   FileName = "C:\MyData.Dat"

   ' open the file
   Open FileName For Output As #1
   
   for i = 1 to UBound(intNumbers)
      ' write numbers to the dat file
      Write #1, strLine
   next i

   ' close the file
   Close #1  
   Exit Sub

OpenErr:
   MsgBox "Error saving the data file." & vbcrlf & err.number & ": " & err.description
   On error goto 0  
End SUb
0
 
LVL 1

Author Comment

by:GlobalFax
ID: 8108488
I am receiving a debug error 9, Subsccript out of range on the line:

ArrayCount = UBound(intNumbers)
0
 
LVL 1

Author Comment

by:GlobalFax
ID: 8108494
Also,

Where did you obtain the 32000/Max -32000/min figures?
0
 
LVL 11

Expert Comment

by:supunr
ID: 8108507
OK forget about UBound, use this other method....I have check this code using a sample file...

Option Explicit
Option Base 1 ' make arrays start from 1, not 0

Private intNumbers() As Integer
Private intNumbersCount As Long

Private Sub cmdFind_Click()
   Dim i As Long
   Dim ArrayCount As Long
   Dim MinScore As Integer
   Dim MinScoreCount As Long
   Dim MaxScore As Integer
   Dim totalScore As Long
   
   ' UBount means the upper bound of the array
   ArrayCount = intNumbersCount
   MinScore = 32000
   MinScoreCount = 0
   MaxScore = -32000
   totalScore = 0
   For i = 1 To ArrayCount
       If (intNumbers(i) < MinScore) Then
           MinScore = intNumbers(i)
           MinScoreCount = 1
       ElseIf (intNumbers(i) < MinScore) Then
           MinScoreCount = MinScoreCount + 1
       End If
       If (intNumbers(i) > MaxScore) Then
           MaxScore = intNumbers(i)
       End If
       totalScore = totalScore + intNumbers(i)
   Next i
   
   MsgBox "Minimum Score = " & MinScore & vbCrLf & "No of occurances = " & MinScoreCount & vbCrLf & _
           "Average Class Score  = " & (totalScore / ArrayCount) & vbCrLf & _
           "Number of students = " & ArrayCount
End Sub

Private Sub LoadDataFromDatFile()
   On Error GoTo OpenErr
   Dim i As Long
   Dim FileName As String
   Dim strLine As String

   FileName = "C:\MyData.Dat"

   ' open the file
   Open FileName For Input As #1
   
   intNumbersCount = 0
   Do While (Not EOF(1))
      ' read line by line and add to the array list
      ' assuming that your dat file is just numbers in each line.
      Line Input #1, strLine
     
      If (Trim(strLine) <> "") Then ' not an empty line
          intNumbersCount = intNumbersCount + 1
          ReDim Preserve intNumbers(intNumbersCount) As Integer
          intNumbers(intNumbersCount) = Val(strLine)
      End If
   Loop

   ' close the file
   Close #1
   Exit Sub

OpenErr:
   MsgBox "Error Opening the data file." & vbCrLf & Err.Number & ": " & Err.Description
   On Error GoTo 0
End Sub

Private Sub SaveDataFromDatFile()
  On Error GoTo OpenErr
  Dim i As Long
  Dim FileName As String

  FileName = "C:\MyData.Dat"

  ' open the file
  Open FileName For Output As #1
 
  For i = 1 To intNumbersCount
     ' write numbers to the dat file
     Write #1, Str(intNumbers(i))
  Next i

  ' close the file
  Close #1
  Exit Sub

OpenErr:
  MsgBox "Error saving the data file." & vbCrLf & Err.Number & ": " & Err.Description
  On Error GoTo 0
End Sub

Private Sub Form_Load()
   Call LoadDataFromDatFile
End Sub


0
 
LVL 11

Expert Comment

by:supunr
ID: 8108550
those two numbers are very large and very small integer values needed to initialize min and max numbers.  You need to initialize min with the largest possible number and max with smallest possible number in order to find the max and min values from set of data.

Max integer value possible is 32767 and the smallest is -32,768.  So rather than using exact of these values I approximated the values because I was not able to remember them form top of my head earlier.  If you want you could replace 32000 with MAX_INT and -32000 with MIN_INT and delcare them as constants.

Private Const MAX_INT=32767
Private Const MIN_INT=-32768
0
 
LVL 1

Author Comment

by:GlobalFax
ID: 8108560
OK, how about if we take it from a different approach.

Let say that the buttons will be, "Average Score", "Highest Score" & "Update Score" respectively, and the final of four buttons being "Exit".

I would assume this would reduce the lines of code needed!

Thanks for you help!
0
 
LVL 11

Accepted Solution

by:
supunr earned 2000 total points
ID: 8108600

Private Sub cmdHighestScore_Click()
   Dim i As Long
   Dim ArrayCount As Long
   Dim MaxScore As Integer
   
   ' UBount means the upper bound of the array
   ArrayCount = intNumbersCount
   MaxScore = -32000
   For i = 1 To ArrayCount
       If (intNumbers(i) > MaxScore) Then
           MaxScore = intNumbers(i)
       End If
   Next i
   
   MsgBox "Highest Score = " & MaxScore
End Sub


Private Sub cmdLowestScore_Click()
   Dim i As Long
   Dim ArrayCount As Long
   Dim MinScore As Integer
   Dim MinScoreCount As Long
   
   ' UBount means the upper bound of the array
   ArrayCount = intNumbersCount
   MinScore = 32000
   MinScoreCount = 0
   For i = 1 To ArrayCount
       If (intNumbers(i) < MinScore) Then
           MinScore = intNumbers(i)
           MinScoreCount = 1
       ElseIf (intNumbers(i) < MinScore) Then
           MinScoreCount = MinScoreCount + 1
       End If
   Next i
   
   MsgBox "Minimum Score = " & MinScore & vbCrLf & _
            "No of occurances = " & MinScoreCount
End Sub

Private Sub cmdAverageScore_Click()
   Dim i As Long
   Dim ArrayCount As Long
   Dim totalScore As Long
   
   ' UBount means the upper bound of the array
   ArrayCount = intNumbersCount
   totalScore = 0
   For i = 1 To ArrayCount
       totalScore = totalScore + intNumbers(i)
   Next i
   
   MsgBox "Average Class Score  = " & (totalScore / ArrayCount) & vbCrLf & _
           "Number of students = " & ArrayCount
End Sub
0
 
LVL 11

Expert Comment

by:supunr
ID: 8108613
private sub cmdExit_Click()
    Unload Me
    End ' end the program
End Sub

I am not sure what u mean by "Update Score"????
0
 
LVL 1

Author Comment

by:GlobalFax
ID: 8108828
Here is what I have come up with:

Option Explicit
Dim intNum(1 To 20) As Integer

Private Sub cmdAvgScore_Click()
    Dim intNum As Integer, intTotal As Integer, sngAvg As Single
    For intNum = 1 To 20
        intTotal = intTotal + intNum
    Next intNum
    sngAvg = intTotal / 20
    lblLowNum.Caption = "Average"
    lblNumStud.Caption = Format(sngAvg, "standard")
End Sub

Private Sub cmdHighScore_Click()
    Dim intNum As Integer, intHigh As Integer
    intHigh = intNum
    For intNum = 2 To 20
        If intNum > intHigh Then
            intHigh = intNum
        End If
    Next intNum
    lblLowNum.Caption = "Highest"
    lblNumStud.Caption = intHigh
End Sub

Private Sub cmdUpdateScore_Click()
    Dim intNum As Integer, intIncrease As Integer
    intIncrease = Val(InputBox("Enter the number of additional points:"))
    For intNum = 1 To 20
        intNum = intNum + intIncrease
    Next intNum
    lblLowNum.Caption = "Scores Updated"
    lblNumStud.Caption = intIncrease
End Sub

Private Sub Form_Load()
    frmPoints.Top = (Screen.Height - frmPoints.Height) / 2
    frmPoints.Left = (Screen.Width - frmPoints.Width) / 2
    Dim intX As Integer
    Open "D:\AIU\ITP410-files\student files\tut10\points.dat" For Input As #1
    For intX = 1 To 20
        Input #1, intNum(intX)
    Next intX
    Close #1
End Sub

Private Sub cmdExit_Click()
    Unload frmPoints
End Sub
0
 
LVL 1

Author Comment

by:GlobalFax
ID: 8108895
I think I have confused myself as to what I wanted, but as you have provided a solution you deserver the points!
0
 
LVL 11

Expert Comment

by:supunr
ID: 8108897
frmPoints.Top = (Screen.Height - frmPoints.Height) / 2
frmPoints.Left = (Screen.Width - frmPoints.Width) / 2

these two lines can be deleted and replace with
frmPoints.StartupPosition = 2 ' center screen

other than that your code is fine and simple.  But it assume that there are always 20 records, which I guess is what you wanted.
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month9 days, 6 hours left to enroll

764 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