Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Error 54: Bad File Mode
Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
peterggmss




PostPosted: Mon Jan 15, 2007 3:24 pm   Post subject: Error 54: Bad File Mode

I get a bad file mode error when i click on cmdLoadFile. Can someone please help me, i've spent almost 2 hours on this problem. This is my code:

code:
    Private Type Student
        studentName As String
        studentNumber As String
        studentGrade As Double
        studentMark As Double
    End Type
   
    Dim Students(1 To 35) As Student
   
    Dim NumberStudents, NumberStudents_Add, StudentEdit, StudentGrade_Temp, StudentMark_Temp, StudentNumber_Length As Double
    Dim StudentNumber_Temp, StudentName_Temp As String
    Dim CurrentNumber, FileNumber As Integer
    'Dim Temp As Double
    Dim pushable As Boolean
   
Private Sub Form_Load()
   
    'lbl_name.Enabled = False
    'lbl_stunum.Enabled = False
    'lbl_grade.Enabled = False
    'lbl_avg.Enabled = False
   
    'lblListStudentNumbers.Visible = False
    'lblListNames.Visible = False
    'lblListMarks.Visible = False
    'lblListGrades.Visible = False
   
    'lbl_status.Caption = "To Begin Please Click Input Records"

End Sub
   
Private Sub cmdAddStudents_Click()

    If NumberStudents = 35 Then
        MsgBox ("You can only have 35 students in a class.")
    Else
        NumberStudents_Add = Int(Val(InputBox("How many more students are there?")))
            NumberStudentsCheck_Add
    End If

End Sub

Private Sub cmdEditStudents_Click()

    StudentEdit = Abs(Int(Val(InputBox("Which student do you want to edit?" & vbNewLine & "The first student shown is 1, the next is 2, etc."))))
        StudentEditorCheck

End Sub

Private Sub cmdDone_Click()

    Unload frmStudents

End Sub

Private Sub cmdStart_Click()

    NumberStudents = Val(InputBox("How many students are there?"))
    NumberStudentsCheck
   
    For x = 1 To NumberStudents
        CurrentNumber = x
        Students(x).studentName = StrConv(InputBox("What Student " & x & "'s Name?" & x), vbProperCase)
            NameCheck
        Students(x).studentNumber = Abs(Val(InputBox("What is Student " & Students(x).studentName & "'s Student Number?", "Student Number")))
            NumberCheck
        Students(x).studentGrade = Int(Val(InputBox("What is Student " & Students(x).studentName & "'s Grade?", "Student Grade")))
            GradeCheck
        Students(x).studentMark = Int(Val(InputBox("What is Student " & Students(x).studentName & "'s mark?", "Student Mark")))
            stuavg_check
    Next x
   
    AddStudents
   
    lblListStudentNumbers.Visible = True
    lblListNames.Visible = True
    lblListGrades.Visible = True
    lblListMarks.Visible = True
   
    cmdSaveFile.Enabled = True
    cmdEditStudents.Enabled = True
    cmdAddStudents.Enabled = True

End Sub

Private Sub cmdLoadFile_Click()

    lblListStudentNumbers.Caption = ""
    lblListNames.Caption = ""
    lblListGrades.Caption = ""
    lblListMarks.Caption = ""
   
    FileNumber = FreeFile
    Open App.Path & "\StudentNumbers.dat" For Random As FileNumber
        lblListStudentNumbers.Caption = Input(LOF(FileNumber), FileNumber)
    Close FileNumber
   
    FileNumber = FreeFile
    Open App.Path & "\StudentNames.dat" For Random As FileNumber
        lblListNames.Caption = App.Path & "\StudentNames.dat" 'Input(LOF(FileNumber), FileNumber)
    Close FileNumber
   
    FileNumber = FreeFile
    Open App.Path & "\StudentGrades.dat" For Random As FileNumber
        lblListGrades.Caption = Input(LOF(FileNumber), FileNumber)
    Close FileNumber
   
    FileNumber = FreeFile
    Open App.Path & "\StudentMarks.dat" For Random As FileNumber
        lblListMarks.Caption = Input(LOF(FileNumber), FileNumber)
    Close FileNumber
   
    MsgBox ("Loaded Sucessfully")

End Sub

Function SaveStudentFile()

    FileNumber = FreeFile
    Open App.Path & "\StudentNumbers.dat" For Output As FileNumber
        Print #FileNumber, lblListStudentNumbers.Caption
    Close FileNumber
   
    FileNumber = FreeFile
    Open App.Path & "\StudentNames.dat" For Output As FileNumber
        Print #FileNumber, lblListNames.Caption
    Close FileNumber
   
    FileNumber = FreeFile
    Open App.Path & "\StudentGrades.dat" For Output As FileNumber
        Print #FileNumber, lblListGrades.Caption
    Close FileNumber
   
    FileNumber = FreeFile
    Open App.Path & "\StudentMarks.dat" For Output As FileNumber
        Print #FileNumber, lblListMarks.Caption
    Close FileNumber

End Function

Private Sub cmdSaveFile_Click()

    SaveStudentFile
    MsgBox ("Saved Sucessfully")

End Sub



Function SortNames()

'Sort by Student Name
    For x = 1 To NumberStudents
        For y = x To NumberStudents
           If Students(y).studentName < Students(x).studentName Then
                StudentNumber_Temp = Students(y).studentNumber
                StudentName_Temp = Students(y).studentName
                StudentGrade_Temp = Students(y).studentGrade
                StudentMark_Temp = Students(y).studentMark
                       
                Students(y).studentNumber = Students(x).studentNumber
                Students(y).studentName = Students(x).studentName
                Students(y).studentGrade = Students(x).studentGrade
                Students(y).studentMark = Students(x).studentMark
                       
                Students(x).studentNumber = StudentNumber_Temp
                Students(x).studentName = StudentName_Temp
                Students(x).studentGrade = StudentGrade_Temp
                Students(x).studentMark = StudentMark_Temp
            End If
        Next y
    Next x
   
    AddStudents
   
    lbl_status.Caption = "The Marks Are Sorted According the Student Name (Lowest to Highest)"

End Function

Function SortNumber()

'Sort by Student Number
    For x = 1 To NumberStudents
        For y = x To NumberStudents
           If Students(y).studentNumber < Students(x).studentNumber Then
                StudentNumber_Temp = Students(y).studentNumber
                StudentName_Temp = Students(y).studentName
                StudentGrade_Temp = Students(y).studentGrade
                StudentMark_Temp = Students(y).studentMark
                       
                Students(y).studentNumber = Students(x).studentNumber
                Students(y).studentName = Students(x).studentName
                Students(y).studentGrade = Students(x).studentGrade
                Students(y).studentMark = Students(x).studentMark
                       
                Students(x).studentNumber = StudentNumber_Temp
                Students(x).studentName = StudentName_Temp
                Students(x).studentGrade = StudentGrade_Temp
                Students(x).studentMark = StudentMark_Temp
            End If
        Next y
    Next x
   
AddStudents

lbl_status.Caption = "The Marks Are Sorted According the Student Number (Lowest to Highest)"
   
End Function

Function SortGrade()

'Sort by Student Grade
    For x = 1 To NumberStudents
        For y = x To NumberStudents
           If Students(y).studentGrade < Students(x).studentGrade Then
                StudentNumber_Temp = Students(y).studentNumber
                StudentName_Temp = Students(y).studentName
                StudentGrade_Temp = Students(y).studentGrade
                StudentMark_Temp = Students(y).studentMark
                       
                Students(y).studentNumber = Students(x).studentNumber
                Students(y).studentName = Students(x).studentName
                Students(y).studentGrade = Students(x).studentGrade
                Students(y).studentMark = Students(x).studentMark
                       
                Students(x).studentNumber = StudentNumber_Temp
                Students(x).studentName = StudentName_Temp
                Students(x).studentGrade = StudentGrade_Temp
                Students(x).studentMark = StudentMark_Temp
            End If
        Next y
    Next x
   
AddStudents

lbl_status.Caption = "The Marks Are Sorted According the Grade (Lowest to Highest)"
   
End Function

Function SortMarks()

'Sort by Student Number
    For x = 1 To NumberStudents
        For y = x To NumberStudents
           If Students(y).studentMark < Students(x).studentMark Then
                StudentNumber_Temp = Students(y).studentNumber
                StudentName_Temp = Students(y).studentName
                StudentGrade_Temp = Students(y).studentGrade
                StudentMark_Temp = Students(y).studentMark
                       
                Students(y).studentNumber = Students(x).studentNumber
                Students(y).studentName = Students(x).studentName
                Students(y).studentGrade = Students(x).studentGrade
                Students(y).studentMark = Students(x).studentMark
                       
                Students(x).studentNumber = StudentNumber_Temp
                Students(x).studentName = StudentName_Temp
                Students(x).studentGrade = StudentGrade_Temp
                Students(x).studentMark = StudentMark_Temp
            End If
        Next y
    Next x
   
AddStudents

lbl_status.Caption = "The Marks Are Sorted According the Average (Lowest to Highest)"
   
End Function

Private Sub lblMark_Click()

    SortMarks

End Sub

Private Sub lblGrade_Click()

    SortGrade

End Sub

Private Sub lblName_Click()

    SortNames

End Sub

Private Sub lblStudentNumber_Click()

    SortNumber

End Sub

Function AddStudents()

    lblListNames.Caption = ""
    lblListStudentNumbers.Caption = ""
    lblListGrades.Caption = ""
    lblListMarks.Caption = ""

    For x = 1 To NumberStudents
        If x = 1 Then
            lblListNames.Caption = lblListNames.Caption & Students(x).studentName
            lblListStudentNumbers.Caption = lblListStudentNumbers.Caption & Students(x).studentNumber
            lblListGrades.Caption = lblListGrades.Caption & Students(x).studentGrade
            lblListMarks.Caption = lblListMarks.Caption & Students(x).studentMark
        Else
            lblListNames.Caption = lblListNames.Caption & vbNewLine & Students(x).studentName
            lblListStudentNumbers.Caption = lblListStudentNumbers.Caption & vbNewLine & Students(x).studentNumber
            lblListGrades.Caption = lblListGrades.Caption & vbNewLine & Students(x).studentGrade
            lblListMarks.Caption = lblListMarks.Caption & vbNewLine & Students(x).studentMark
        End If
    Next x
   
    End Function
   
Function NumberCheck()
   
    If Students(CurrentNumber).studentNumber = 0 Then
        MsgBox ("The Student Number You Have Entered is Incorrect. A Proper Student Number Only Contains 6 Numbers")
        Students(CurrentNumber).studentNumber = Abs(Val(InputBox("What is " & Students(CurrentNumber).studentName & "'s Student Number?", "Student Number")))
        NumberCheck
    Else
   
        StudentNumber_Length = Len(Students(CurrentNumber).studentNumber)
   
        If StudentNumber_Length > 6 Then
            MsgBox ("The Student Number You Have Entered is Incorrect. A Proper Student Number Only Contains 6 Numbers")
            Students(CurrentNumber).studentNumber = Abs(Val(InputBox("What is " & Students(CurrentNumber).studentName & "'s Student Number?", "Student Number")))
            NumberCheck
        ElseIf StudentNumber_Length < 6 Then
            MsgBox ("The Student Number You Have Entered is Incorrect. A Proper Student Number Only Contains 6 Numbers")
            Students(CurrentNumber).studentNumber = Abs(Val(InputBox("What is " & Students(CurrentNumber).studentName & "'s Student Number?", "Student Number")))
            NumberCheck
        End If
   
    End If

End Function

Function NameCheck()

    If Students(CurrentNumber).studentName = "" Then
        MsgBox ("You Must Enter a Name for Student " & CurrentNumber)
        Students(CurrentNumber).studentName = StrConv(InputBox("What is the Name of Student " & x & "?", "Name of Student " & CurrentNumber), vbProperCase)
        NameCheck
    End If

End Function

Function GradeCheck()

    If Students(CurrentNumber).studentGrade > 12 Or Students(CurrentNumber).studentGrade < 1 Then
        MsgBox ("You Must Enter a Valid Grade for " & Students(CurrentNumber).studentName & ". The Only Grades Available Range from 1 to 12")
        Students(CurrentNumber).studentGrade = Int(Val(InputBox("What grade is " & Students(CurrentNumber).studentName & " in?", "Student Grade")))
        GradeCheck
    End If

End Function

Function stuavg_check()

    If Students(CurrentNumber).studentMark > 100 Or Students(CurrentNumber).studentMark < 0 Then
        MsgBox ("You Have Entered an Invalid Mark for " & Students(CurrentNumber).studentName & ". A Student's Average can Only Range from 0 to 100%")
        Students(CurrentNumber).studentMark = Int(Val(InputBox("What is " & Students(CurrentNumber).studentName & "'s mark in this class? Please Note that All Marks will be Rounded to a Whole Number.", "Student Mark")))
        stuavg_check
    End If

End Function

Function NumberStudentsCheck()

    If NumberStudents > 34 Then
        MsgBox ("You Have Too Many Students in Your Class! Please Reduce the Amount of Students In This Class! The Maxium Class Size is 34.")
        NumberStudents = Int(Val(InputBox("For How Many Students Do You Want to Input Records For?", "Number of Students")))
        NumberStudentsCheck
    ElseIf NumberStudents < 1 Then
        MsgBox ("You Have Too Few Students in Your Class! Please Increase the Amount of Students In This Class! The Minimum Class Size is 1 Student.")
        NumberStudents = Int(Val(InputBox("For How Many Students Do You Want to Input Records For?", "Number of Students")))
        NumberStudentsCheck
    End If

End Function

Function NumberStudentsCheck_Add()

    If NumberStudents_Add > 34 Then
        MsgBox ("You Are Trying to Add More Students than there can be in One Class. Please Enter a Number that is Less than 34, and when added to the current student amount, will be equal to or less than 34")
        NumberStudents_Add = InputBox("How Many More Records Would You Like To Add?", "Adding More Students")
        NumberStudentsCheck_Add
    ElseIf NumberStudents_Add < 0 Then
        MsgBox ("Please Enter a Number Greater Than 0. If You Do Not Wish to Add Any Records, Please Type in 0")
        NumberStudents_Add = InputBox("How Many More Records Would You Like To Add?", "Adding More Students")
        NumberStudentsCheck_Add
    ElseIf (NumberStudents + NumberStudents_Add) > 34 Then
        MsgBox ("The Amount of Records that you are trying to Add On will Cause You To Have More Records than You Are Allowed to Have. Please Enter a Number that will not Cause this Error to Occur (.:. a number less than the one you just typed in)")
        NumberStudents_Add = InputBox("How Many More Records Would You Like To Add?", "Adding More Students")
        NumberStudentsCheck_Add
    Else
        For x = NumberStudents + 1 To (NumberStudents + NumberStudents_Add)
            CurrentNumber = x
            Students(x).studentName = StrConv(InputBox("What is the Name of Student " & x & "?", "Name of Student " & x), vbProperCase)
                NameCheck
            Students(x).studentNumber = Abs(Val(InputBox("What is " & Students(x).studentName & "'s Student Number?", "Student Number")))
                NumberCheck
            Students(x).studentGrade = Int(Val(InputBox("What grade is " & Students(x).studentName & " in?", "Student Grade")))
                GradeCheck
            Students(x).studentMark = Int(Val(InputBox("What is " & Students(x).studentName & "'s mark in this class? Please Note that All Marks will be Rounded to a Whole Number.", "Student Mark")))
                stuavg_check
        Next x
             
        For x = NumberStudents + 1 To (NumberStudents + NumberStudents_Add)
            lblListNames.Caption = lblListNames.Caption & vbNewLine & Students(x).studentName
            lblListStudentNumbers.Caption = lblListStudentNumbers.Caption & vbNewLine & Students(x).studentNumber
            lblListGrades.Caption = lblListGrades.Caption & vbNewLine & Students(x).studentGrade
            lblListMarks.Caption = lblListMarks.Caption & vbNewLine & Students(x).studentMark
        Next x
       
        NumberStudents = NumberStudents + NumberStudents_Add
       
    End If

End Function

Function StudentEditorCheck()

    If StudentEdit > NumberStudents Then
        MsgBox ("You Cannot Edit a Record that Does Not Exist.")
        StudentEdit = InputBox("How Many More Records Would You Like To Add?", "Adding More Students")
        NumberStudentsCheck_Add
    ElseIf StudentEdit < 0 Then
        MsgBox ("Please Enter a Number Greater Than 0. If You Do Not Wish to Edit Any Records, Please Type in 0")
        StudentEdit = Abs(Int(Val(InputBox("To Edit a Record, Please Type in the Record Number. For Example, the first record printed in the table, would be 1.", "Editing Records"))))
        StudentEditorCheck
    ElseIf StudentEdit > 34 Then
        MsgBox ("You Cannot Edit Records that Do Not Exist.")
        StudentEdit = Abs(Int(Val(InputBox("To Edit a Record, Please Type in the Record Number. For Example, the first record printed in the table, would be 1.", "Editing Records"))))
        StudentEditorCheck
    Else
        CurrentNumber = StudentEdit
        Students(StudentEdit).studentName = StrConv(InputBox("What is the Name of Student " & x & "?", "Name of Student " & StudentEdit, Students(StudentEdit).studentName), vbProperCase)
            NameCheck
        Students(StudentEdit).studentNumber = Abs(Val(InputBox("What is " & Students(StudentEdit).studentName & "'s Student Number?", "Student Number", Students(StudentEdit).studentNumber)))
            NumberCheck
        Students(StudentEdit).studentGrade = Int(Val(InputBox("What grade is " & Students(StudentEdit).studentName & " in?", "Student Grade", Students(StudentEdit).studentGrade)))
            GradeCheck
        Students(StudentEdit).studentMark = Int(Val(InputBox("What is " & Students(StudentEdit).studentName & "'s mark in this class? Please Note that All Marks will be Rounded to a Whole Number.", "Student Mark", Students(StudentEdit).studentMark)))
            stuavg_check
         
        lblListNames.Caption = ""
        lblListStudentNumbers.Caption = ""
        lblListGrades.Caption = ""
        lblListMarks.Caption = ""
         
        AddStudents
       
    End If

End Function
Sponsor
Sponsor
Sponsor
sponsor
cool dude




PostPosted: Mon Jan 15, 2007 3:52 pm   Post subject: Re: Error 54: Bad File Mode

mind providing the form and the files possibly. i quickly glanced at your open statements and i'm not sure if thats right. From what i remember there should be # filenumber

Also you put the & sign and i'm not sure if you can do that Confused If your trying to open 2 files just make 2 open statements.
Monstrosity_




PostPosted: Mon Jan 15, 2007 6:32 pm   Post subject: Re: Error 54: Bad File Mode

peterggmss @ Mon Jan 15, 2007 4:24 pm wrote:
I get a bad file mode error when i click on cmdLoadFile. Can someone please help me, i've spent almost 2 hours on this problem. This is my code:

<code snipped>

Next time try to provide the line the error occurred on (since I'm not opening VB to find that out myself). The problem is most likely the fact that your opening your file for random access (even though you are not accessing it randomly in your code), but the Input() function expects a file opened for sequential reads. To fix this either start using Get statement or open you files up with Input as your mode.

cool dude wrote:

mind providing the form and the files possibly. i quickly glanced at your open statements and i'm not sure if thats right. From what i remember there should be # filenumber

Also you put the & sign and i'm not sure if you can do that Confused If your trying to open 2 files just make 2 open statements.

The # is optional, and & is valid (and prefered) for concatenating stings.
cool dude




PostPosted: Mon Jan 15, 2007 9:43 pm   Post subject: Re: Error 54: Bad File Mode

Like i said i wasn't sure because i've always been taught to put the #. Anyhow if i was to debug that many lines of code the form and files would be nice so that i could know exactly where the problem is happening.
Display posts from previous:   
   Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: