3

I'm studying computing at AS Level in England, and the language we are using is VB6.

I am working on out assignment which has to be completed for 18/12/2014.

The project is for a hypothetical situation where a running club needs software to do the following: - Add Members - View Members - Edit Member Information - Search for Members - Delete Members - Add Training Information for Members - View Training Information - Compare Training Information - Automatically select a team of runners based upon the number of hours trained for

Here's my code for the problem form:

Option Explicit

Private Sub CmdExitFromSelectTeam_Click()
Unload Me
End Sub

Private Sub SelectTeam()
Dim TrainingChannel As Integer
Dim Training As TrainingRecord
Dim MemberChannel As Integer
Dim Member As MemberRecord
Dim MemberRecordPointer As Integer
Dim TotalHoursTrained As Single
Dim TrainingRecordPointer As Integer
Dim FoundAtLeastOneRecord
FoundAtLeastOneRecord = False
MemberChannel = FreeFile
Open MemberFile For Random As MemberChannel Len = MemberLength
    MemberRecordPointer = 1
    Get MemberChannel, MemberRecordPointer, Member
    Do While Not EOF(MemberChannel)
        TotalHoursTrained = 0
        TrainingRecordPointer = 1
        TrainingChannel = FreeFile
        Open TrainingFile For Random As TrainingChannel Len = TrainingLength
            Get TrainingChannel, MemberRecordPointer, Training
            Do While Not EOF(TrainingChannel)
                If Member.ID = Training.MemberID Then
                    TotalHoursTrained = Round(TotalHoursTrained, 1) + Round(Training.TimeTaken, 1)
                End If
                TrainingRecordPointer = TrainingRecordPointer + 1 (*)
                Get TrainingChannel, MemberRecordPointer, Training
            Loop
        Close TrainingChannel
    LstTeamSelectDisplayTeam.AddItem TotalHoursTrained, 1
    LstTeamSelectDisplayTeam.AddItem Member.ID, 2
    LstTeamSelectDisplayTeam.AddItem Member.Forename, 3
    LstTeamSelectDisplayTeam.AddItem Member.Surname, 4
    MemberRecordPointer = MemberRecordPointer + 1
    Get MemberChannel, MemberRecordPointer, Member
    Loop
Close MemberChannel

End Sub

Private Sub Form_Load()
SelectTeam
End Sub

When this form (FrmSelectTeam.frm) is loaded at run time, the line: marked with (*) is highlighted as the debug line.

I have no idea what the problem is, and I'd appreciate all the help I can get, so thanks in advance!!!

James

5
  • 1
    How many records do you have? Try using a long instead of an integer. Commented Dec 11, 2014 at 17:05
  • Are there more than 32768 records in the file? If so, you've hit the limit of an integer type. Change Dim TrainingRecordPointer As Integer to Dim TrainingRecordPointer As Long to change the cap to ~2.1 billion. Commented Dec 11, 2014 at 17:07
  • 2
    Good grief...tell your instructor to stop teaching with VB6! shakes head in disbelief Commented Dec 11, 2014 at 18:10
  • 1
    Plus one for asking the question so clearly & providing all the important information Commented Dec 12, 2014 at 12:07
  • 1
    Excellent question, and a common problem for beginning programmers. Commented Dec 12, 2014 at 23:48

3 Answers 3

4

In VB6, the maximum value for the Integer data type is 32767. You are apparently exceeding that limit in the (*) statement. You can change it to a 32-bit integer by declaring it long:

Dim TrainingRecordPointer As Long
Sign up to request clarification or add additional context in comments.

Comments

0

@xpda's answer is almost certainly correct.

One handy debugging trick for an error like this, is to modify your code slightly, as below:

Open MemberFile For Random As MemberChannel Len = MemberLength
MemberRecordPointer = 1
Get MemberChannel, MemberRecordPointer, Member
Do While Not EOF(MemberChannel)
    TotalHoursTrained = 0
    TrainingRecordPointer = 1
    TrainingChannel = FreeFile
    Open TrainingFile For Random As TrainingChannel Len = TrainingLength
        Get TrainingChannel, MemberRecordPointer, Training
        Do While Not EOF(TrainingChannel)
            If Member.ID = Training.MemberID Then
                TotalHoursTrained = Round(TotalHoursTrained, 1) + Round(Training.TimeTaken, 1)
            End If
            If TrainingRecordPointer > 32750 Then
                Debug.Print TrainingRecordPointer
            End If
            TrainingRecordPointer = TrainingRecordPointer + 1
            Get TrainingChannel, MemberRecordPointer, Training
        Loop
    Close TrainingChannel
LstTeamSelectDisplayTeam.AddItem TotalHoursTrained, 1
LstTeamSelectDisplayTeam.AddItem Member.ID, 2
LstTeamSelectDisplayTeam.AddItem Member.Forename, 3
LstTeamSelectDisplayTeam.AddItem Member.Surname, 4
MemberRecordPointer = MemberRecordPointer + 1
Get MemberChannel, MemberRecordPointer, Member
Loop
Close MemberChannel

Alternatively, you can put a breakpoint in the added If-Then, and step through using the debugger.

Comments

0

Well thankyou for your feedback, but what was actually the cause (Believe it or not) was simple human error; I put "Get TrainingChannel, MemberRecordPointer, Training" instead of "Get TrainingChannel, TrainingRecordPointer, Training"

It's so annoying that something as simple as that could cause such a big problem.

But once again, thanks!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.