PDA

View Full Version : probably easy (visual basic)


kittyciara
12-01-2008, 09:20 PM
So I'm trying to write a program in Visual Basic. The program is supposed to read an Access database of student information and write it to a form and allow the user to maintain the information, search and select the information by student last name and display a class average. I've got the access database and most of the programming done. The only thing causing me issues is the class average button.

The click event procedure for the class average button is supposed to read from 3 parallel arrays and display the results in the name list box concatenated like this: Last Name, First Name, Student Average ( this would be a number). So far I have figured out how to make the list box display the last name, and first name but the student average and the class average are giving me problems. Here is my code and I attached a SS of my form.

Any help is appreciated! looooooove you!




Public Class mainForm

Private Sub Table1BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Table1BindingNavigatorSaveItem.Click
Me.Validate()
Me.Table1BindingSource.EndEdit()
Me.Table1TableAdapter.Update(Me.Student_Informatio nDataSet.Table1)
End Sub

Private Sub mainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
'TODO: This line of code loads data into the 'Student_InformationDataSet.Table1' table.
'You can move, or remove it, as needed.
Me.Table1TableAdapter.Fill(Me.Student_InformationD ataSet.Table1)
SplashScreenForm.ShowDialog()
End Sub

Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles exitButton.Click
Me.Close()
End Sub

Private Sub calcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles calcButton.Click
Dim Project_1 As Decimal
Dim Project_2 As Decimal
Dim Mid__Term_Grade As Decimal
Dim FInal__Exam_Grade As Decimal
Dim HW_1 As Decimal
Dim HW_2 As Decimal
Dim HW_3 As Decimal
Dim HW_4 As Decimal
Dim HW_5 As Decimal
Dim HW_7 As Decimal
Dim HW_8 As Decimal
Dim HW_9 As Decimal
Dim HW_10 As Decimal
Dim HW_Average As Decimal
Dim studentAverage As Decimal
Dim isConverted As Boolean

isConverted = Decimal.TryParse(studentAverageLabel.Text, studentAverage)

Project_1 = Project_1TextBox.Text * 0.2
Project_2 = Project_2TextBox.Text * 0.2
Mid__Term_Grade = Mid__Term_GradeTextBox.Text * 0.2
FInal__Exam_Grade = FInal__Exam_GradeTextBox.Text * 0.2
HW_1 = HW_1TextBox.Text
HW_2 = HW_2TextBox.Text
HW_3 = HW_3TextBox.Text
HW_4 = HW_4TextBox.Text
HW_5 = HW_5TextBox.Text
HW_7 = HW_7TextBox.Text
HW_8 = HW_8TextBox.Text
HW_9 = HW_9TextBox.Text
HW_10 = HW_10TextBox.Text
HW_Average = (((HW_1 + HW_2 + HW_3 + HW_4 + HW_5 + HW_7 + HW_8 + HW_9 + HW_10) / 9) * 0.2)
studentAverage = Project_1 + Project_2 + Mid__Term_Grade + FInal__Exam_Grade + HW_Average
studentAverageLabel.Text = studentAverage.ToString("N2")
End Sub

Private Sub BindingNavigatorMoveNextItem_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles BindingNavigatorMoveNextItem.Click
studentAverageLabel.Text = ""
End Sub

Private Sub BindingNavigatorMovePreviousItem_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles BindingNavigatorMovePreviousItem.Click
studentAverageLabel.Text = ""

End Sub

Private Sub BindingNavigatorMoveLastItem_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles BindingNavigatorMoveLastItem.Click
studentAverageLabel.Text = ""

End Sub

Private Sub BindingNavigatorMoveFirstItem_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles BindingNavigatorMoveFirstItem.Click
studentAverageLabel.Text = ""

End Sub

Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles BindingNavigatorAddNewItem.Click
studentAverageLabel.Text = ""

End Sub

Private Sub searchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles searchButton.Click
Try
Me.Table1TableAdapter.FillBySearch(Me.Student_Info rmationDataSet.Table1, searchTextBox.Text)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try

studentAverageLabel.Text = ""

End Sub

Private Sub OrderByLastNameToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrderByLastNameToolStripMenuItem.Click
Try
Me.Table1TableAdapter.FillByLastName(Me.Student_In formationDataSet.Table1)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try

studentAverageLabel.Text = ""


End Sub

Private Sub SearchSeniorsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchSeniorsToolStripMenuItem.Click
Try
Me.Table1TableAdapter.FillBySenior(Me.Student_Info rmationDataSet.Table1)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try

studentAverageLabel.Text = ""


End Sub

Private Sub ResetToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResetToolStripMenuItem.Click
Try
Me.Table1TableAdapter.Fill(Me.Student_InformationD ataSet.Table1)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try

studentAverageLabel.Text = ""

End Sub

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()

End Sub

Private Sub ToolTip1_Popup(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PopupEventArgs) Handles ToolTip1.Popup

End Sub

Private Sub FillBySearchToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FillBySearchToolStripButton.Click
Try
Me.Table1TableAdapter.FillBySearch(Me.Student_Info rmationDataSet.Table1, Last_NameToolStripTextBox.Text)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try

studentAverageLabel.Text = ""


End Sub

Private Sub Last_NameToolStripTextBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Last_NameToolStripTextBox.Click

End Sub

Private Sub resetButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles resetButton.Click
Try
Me.Table1TableAdapter.Fill(Me.Student_InformationD ataSet.Table1)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try

studentAverageLabel.Text = ""
nameListBox.Items.Clear()



End Sub

Private Sub Last_NameToolStripLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Last_NameToolStripLabel.Click

studentAverageLabel.Text = ""

End Sub

Private Sub showButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles showButton.Click

Dim UpperBound As Integer
Dim count As Integer


' declare parallel arrays
Dim lastName(0) As String
Dim firstName(0) As String
Dim studentAverage(0) As String

For Each record As DataRow In Student_InformationDataSet.Table1

'write student ID and class average to parallel arrays
lastName(UpperBound) = (record.Item("Last Name"))
firstName(UpperBound) = (record.Item("First Name"))
count = count + 1

'redeclare arrays, increase size of array by 1 and preserve entries
ReDim Preserve lastName(UpperBound + 1)
ReDim Preserve firstName(UpperBound + 1)


nameListBox.Items.Add(lastName(UpperBound) & ", " & firstName(UpperBound))
Next record

nameListBox.SelectedIndex = 0
End Sub

End Class

Bonzo
12-01-2008, 09:32 PM
Who in their right mind is going to sort through that pile of shit?

Bonzo
12-01-2008, 09:36 PM
Anyway, your problem is with: nameListBox.Items.Add(lastName(UpperBound) & ", " & firstName(UpperBound)) ?

You are not adding the student avg and class average to the list box.

kittyciara
12-01-2008, 09:42 PM
The list box is supposed to display the names of the students: Last name, first name, and their respective averages. So far all I got to display in the list box is the names of the students. I cant figure out how to make their averages show up, and get a class average to display.

Bronyaur
12-01-2008, 09:43 PM
in your calcButton_Click you calculate the student average, put that code in a method which is called by the calcButton_Click handler and also called when you need the student average for the listbox

kittyciara
12-01-2008, 09:51 PM
I don't want to sound like a blithering moron, but I am new to this visual basic crap and our instructor is having me do something she barely taught us to do. We did not learn about methods, is there an easier, more barney type way of doing it because I wear a helmet in my classes and spit when I talk. Thanks

Bonzo
12-01-2008, 10:16 PM
You don't even need arrays for anything, you don't need to hold on to any of the data after you display it. Greta is cooking up something for you, it'll be over soon.

Bronyaur
12-01-2008, 10:28 PM
ok, first of all

HW_1 = HW_1TextBox.Text
HW_2 = HW_2TextBox.Text
HW_3 = HW_3TextBox.Text
HW_4 = HW_4TextBox.Text
HW_5 = HW_5TextBox.Text
HW_7 = HW_7TextBox.Text
HW_8 = HW_8TextBox.Text
HW_9 = HW_9TextBox.Text
HW_10 = HW_10TextBox.Text


this shouldn't work. It might compile, but it should throw an invalid cast exception at runtime. You'll need to Convert the strings to Decimal. Here is the first one, you should get the picture:


HW_1 = Convert.ToDecimal(HW_1TextBox.Text)


Note this still will throw an exception if you don't have decimals in your textboxes, but at least it will run under normal circumstances.

Now, I could write a method for you, but I don't understand what are you doing with the arrays. Why are you declaring UpperBound but never increasing it? Do you have to use Arrays? I don't even see why you need Arrays. Here try this:


Private Sub showButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles showButton.Click

Dim totalRowCount As Integer
totalRowCount = Student_InformationDataSet.Table1.Rows.Count
Dim runningClassAvg As Decimal
runningClassAvg = 0.0

For Each record As DataRow In Student_InformationDataSet.Table1
Dim studentAvg As String
studentAvg = calcAvg(DataRow dr)
nameListBox.Items.Add(record.Item("Last Name").ToString() & ", " & record.Item("Last Name").ToString() & ", " + studentAvg)
runningClassAvg += studentAvg
Next record

' calculate class avg
runningClassAvg = runningClassAvg / Convert.ToDecimal(totalRowCount)
' TODO**: Probably display this in a text box
nameListBox.SelectedIndex = 0
End Sub

Private Function calcAvg(ByVal dr As DataRow) As String
Dim Project_1 As Decimal
Dim Project_2 As Decimal
Dim Mid__Term_Grade As Decimal
Dim FInal__Exam_Grade As Decimal
Dim HW_1 As Decimal
Dim HW_2 As Decimal
Dim HW_3 As Decimal
Dim HW_4 As Decimal
Dim HW_5 As Decimal
Dim HW_7 As Decimal
Dim HW_8 As Decimal
Dim HW_9 As Decimal
Dim HW_10 As Decimal
Dim HW_Average As Decimal
Dim studentAverage As Decimal
Dim isConverted As Boolean

Project_1 = Convert.ToDecimal(dr("Project 1")) * 0.2
Project_2 = Convert.ToDecimal(dr("Project 2")) * 0.2
Mid__Term_Grade = Convert.ToDecimal(dr("Midterm")) * 0.2
FInal__Exam_Grade = Convert.ToDecimal(dr("FinalExam")) * 0.2
HW_1 = Convert.ToDecimal(dr("HW1"))
HW_2 = Convert.ToDecimal(dr("HW2"))
HW_3 = Convert.ToDecimal(dr("HW3"))
HW_4 = Convert.ToDecimal(dr("HW4"))
HW_5 = Convert.ToDecimal(dr("HW5"))
HW_7 = Convert.ToDecimal(dr("HW7"))
HW_8 = Convert.ToDecimal(dr("HW8"))
HW_9 = Convert.ToDecimal(dr("HW9"))
HW_10 = Convert.ToDecimal(dr("HW10"))
HW_Average = (((HW_1 + HW_2 + HW_3 + HW_4 + HW_5 + HW_7 + HW_8 + HW_9 + HW_10) / 9) * 0.2)
studentAverage = Project_1 + Project_2 + Mid__Term_Grade + FInal__Exam_Grade + HW_Average
Return studentAverage.ToString("N2")
End Function


Try replacing your showButton_Click function with that. Make sure you fix the TODO comment I put in there, you'll need to set the textbox.Text property to that value to get it to display.
Also I just guessed column names at the very bottom ("Project 1" up to "HW10"), those are probably wrong, but I don't have your schema so you will have to fill in the right column names. And you are missing HW6, maybe intentionally.

Lastly, I pretty much never write in VB, so this might not work at all. :)

kittyciara
12-02-2008, 12:44 AM
Hey man, that helped a lot. The only problem is with this part of the code:
studentAvg = calcAvg(DataRow dr)
it is giving this error: Error 1 'DataRow' is a type and cannot be used as an expression. AND Error 2 Comma, ')', or a valid expression continuation expected. Ill try to figure it out, but otherwise man thanks for the help

Bronyaur
12-02-2008, 12:52 AM
yeah sorry that was stupid
should be

studentAvg = calcAvg(dr)

Sorry I was going fast and was thinking too far ahead :-/

Bronyaur
12-02-2008, 01:01 AM
I just noticed another bug

this line

runningClassAvg += studentAvg

should be:

runningClassAvg += Convert.ToDecimal(studentAvg)

kittyciara
12-02-2008, 01:05 AM
lol, its getting closer man. Now i am just getting that (dr) is not declared for some reason. and that's my only error right now.

kittyciara
12-02-2008, 01:16 AM
studentAvg = calcAvg(dr)
(dr) is not declared

Bronyaur
12-02-2008, 01:24 AM
GAH

try 'record' instead of 'dr'

hmm just saw something ELSE. Man I hate VB

this might not work
nameListBox.Items.Add(record.Item("Last Name").ToString() & ", " & record.Item("Last Name").ToString() & ", " + studentAvg)


Just change the last '+' to a '&'. Does VB really use &'s? What a dumb language. So:
nameListBox.Items.Add(record.Item("Last Name").ToString() & ", " & record.Item("Last Name").ToString() & ", " & studentAvg)

Asmo
12-02-2008, 04:52 AM
both + and & work in vb.net :D

kittyciara
12-02-2008, 10:26 AM
Hey you guys helped a lot. Putting that code in eliminated all the errors but when i ran the program and clicked the button the errors showed back up. I don't know why we have to use arrays for this project but I just looked at the requirements and we have to use them. I'm going to see what else i can do with it but here are the requirements. If anyone is bored and could hook me up id appreciate it.


Project 2: Creating an Application to Track Student Scores

Create a Visual Basic application that will read an Access database of student information and write it to a form. Allow the user to maintain the information, search and select the information by student last name and display a class average.

Functional Requirements:
• Create a database with a Student Information table that has the following fields: Student ID, Student last name, first name, middle name, status (e.g., Senior), major, and all course scores. Homework scores are separated by assignment
• Populate the Student table by keying your own personal information and grades. Include “made-up” information about nine other students.
• Include an automatic Spash Screen with Project Name, description, date completed and each programmer’s name
• Create a form to display the database information.
• Allow the user to navigate through the data: add rows, delete rows and save changes to the data via a button and menu option.
• Retrieve specific entries by allowing the user to search the data by last name and only display records from the database that match. Create two alternate ways (via button and menu option) to do this.
• Show a button to calculate correct final grade for each student (or provide an automatic calculation when the form loads). Use the weights from our syllabus to determine the student’s final grade.
• Show a button with ability to read from two parallel arrays and display the student names in a list box (sorted by last name, first name) and grades concatenated together.
• Display a class average after the list box.
• Allow the user to view tooltips on all form controls.
• Allow the user to run queries from a menu that:1) runs a roster in name alphabetical order; and 2) runs a roster query of seniors only.
• Give the user the ability to exit the application via button or menu option.

Technical Requirements:
• The assignment weights from the syllabus may be hard-coded or loaded into arrays or a database table.
• Two parallel arrays must exist in the code (student name (or id) and final grade)
• All inputs must be validated.
• Use good programming style, including comments, good naming conventions, well-designed and user-friendly forms and code.

These are the bullshit instructions I was given to create this program. Plus im a retard. Thanks
Ill fucking pay you money if you end this fucking nightmare.

Bronyaur
12-02-2008, 12:27 PM
I see. I can help you out a little later.

It sounds like what you want is two global arrays. One holding student ID's and one holding the final score (student average I think?). This way you only have to calculate them once, which makes sense.

You should define them in the class, and instantiate them in the Form OnLoad event. Set their size to be your DataSet.Table1.Rows.Count.

kittyciara
12-02-2008, 02:49 PM
Ok man ill work on it. Thanks for the help

Bonzo
12-02-2008, 03:26 PM
Can I just ask where you are doing this coding in? I'm guessing you are doing this all in access, right? Or are you using Visual Studio and then the Access DB as a datasource?

kittyciara
12-02-2008, 03:30 PM
i am coding in visual basic, and using access as a data source. You got it

Bonzo
12-02-2008, 07:31 PM
If you can, zip up your current code and the database and attach it.

kittyciara
12-03-2008, 06:49 AM
here it is.