|
-
Apr 22nd, 2001, 10:38 PM
#1
Thread Starter
New Member
Can anyone tell me what I am doing wrong? And Tell me what I should have as the right code
Read the following instructions, my code is below it.
The third button will bring the user to a form that allows entry of Student marks into a file. Upon indicating that the user has completed entering marks for one student have the program calculate the average mark, display that mark in a message box and then clear the screen to allow for a new student.
The must also be a button to allow return to main menu.
My code is:
Private Sub cmdAddToFile_Click()
Dim strName As String
Dim intMarks As Integer
strName = txtStudentName.Text
intMarks = Val(txtMark.Text)
'Open file to add student marks to it (append)
If IsNumeric(txtStudentName.Text) Then
MsgBox "Please Enter Text, not a number", vbOKOnly, "Error"
Else
If txtStudentName.Text <> "" Then
If IsNumeric(txtMark.Text) And Val(txtMark.Text) >= 0 And Val(txtMark.Text) <= 100 Then
Open "C:\My Documents\StudentMarks.txt" For Append As #1
Write #1, strName, intMarks
Close #1
Else
MsgBox "Please enter a number from 1 to 100"
End If
txtMark.Text = ""
txtMark.SetFocus
Else
MsgBox "Please Enter Student Name", vbOKOnly, "Error"
txtStudentName.SetFocus
End If
End If
End Sub
Private Sub cmdAverage_Click()
'Displays student average mark in a Message box and student name in a label
'Totals Student marks
'Give an average in a message box
Dim msngTotal As Long
Dim mintCount As Integer
Open "C:\My Documents\StudentMarks.txt" For Input As #1
Do Until strName = txtStudentName.Text
Input #1, strName, intMarks
lblTitle.Caption = strName
Loop
If strName = txtStudentName.Text Then
msngTotal = msngTotal + intMarks
mintCount = mintCount + 1
MsgBox ("Your Mark is:" & " " & msngTotal / mintCount)
End If
Close #1
txtMark.Text = ""
txtMark.SetFocus
End Sub
Private Sub cmdDone_Click()
Dim msngTotal As Long
Dim mintCount As Integer
Dim strYesNo As String
strYesNo = MsgBox("Are you done entering Student Marks?", vbYesNo + vbQuestion, "Confirm Done Entering Marks?")
If strYesNo = vbYes Then
Open "C:\My Documents\StudentMarks.txt" For Input As #1
Do Until EOF(1)
Input #1, strName, intMarks
lblTitle.Caption = strName
Loop
Close #1
Call cmdAverage_Click
End If
End Sub
-
Apr 23rd, 2001, 01:40 AM
#2
Before looking through the code I would like to know what kind of error i'm looking for.
-
Apr 23rd, 2001, 05:50 AM
#3
Thread Starter
New Member
Can anyone tell me what I am doing wrong?
Can anyone tell me what I am doing wrong? And Tell me what I should have as the right code. When I try my code, I am not getting the average make for one student it is averaging all the marks in the file. Please help me. I think my main mistake is in the Private Sub cmdAverage_Click() command.
Read the following instructions, my code is below it.
Create a form that allows entry of Student marks into a file. Upon indicating that the user has completed entering marks for one student have the program calculate the average mark, display that mark in a message box and then clear the screen to allow for a new student. It should also display the students name, need to validate numbers, and text entered, and only enter numbers greater than 0 and less than 100.
My code is:
Private Sub cmdAddToFile_Click()
Dim strName As String
Dim intMarks As Integer
strName = txtStudentName.Text
intMarks = Val(txtMark.Text)
'Open file to add student marks to it (append)
If IsNumeric(txtStudentName.Text) Then
MsgBox "Please Enter Text, not a number", vbOKOnly, "Error"
Else
If txtStudentName.Text <> "" Then
If IsNumeric(txtMark.Text) And Val(txtMark.Text) >= 0 And Val(txtMark.Text) <= 100 Then
Open "C:\My Documents\StudentMarks.txt" For Append As #1
Write #1, strName, intMarks
Close #1
Else
MsgBox "Please enter a number from 1 to 100"
End If
txtMark.Text = ""
txtMark.SetFocus
Else
MsgBox "Please Enter Student Name", vbOKOnly, "Error"
txtStudentName.SetFocus
End If
End If
End Sub
Private Sub cmdAverage_Click()
'Displays student average mark in a Message box and student name in a label
'Totals Student marks
'Give an average in a message box
Dim msngTotal As Long
Dim mintCount As Integer
Open "C:\My Documents\StudentMarks.txt" For Input As #1
Do Until strName = txtStudentName.Text
Input #1, strName, intMarks
lblTitle.Caption = strName
Loop
If strName = txtStudentName.Text Then
msngTotal = msngTotal + intMarks
mintCount = mintCount + 1
MsgBox ("Your Mark is:" & " " & msngTotal / mintCount)
End If
Close #1
txtMark.Text = ""
txtMark.SetFocus
End Sub
Private Sub cmdDone_Click()
Dim msngTotal As Long
Dim mintCount As Integer
Dim strYesNo As String
strYesNo = MsgBox("Are you done entering Student Marks?", vbYesNo + vbQuestion, "Confirm Done Entering Marks?")
If strYesNo = vbYes Then
Open "C:\My Documents\StudentMarks.txt" For Input As #1
Do Until EOF(1)
Input #1, strName, intMarks
lblTitle.Caption = strName
Loop
Close #1
Call cmdAverage_Click
End If
End Sub
-
Apr 23rd, 2001, 06:03 AM
#4
PowerPoster
The mistake is in the cmdAverage_Click.
You need to look more closely at the loop, where it's loading from the file to produce the average. It doesn't quite encompass everything you need.
Gentile or Jew,
O you who turn the wheel and look to windward,
Consider Phlebas, who was once handsome and tall as you...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|