0
votes

I am making a student profile in Visual Basic 2012 for a computing assignment.

I have a combo box populated with two courses: Computing and Business. (On the right hand side of the form, picture attached). Also, a List button which once pressed should display the students from a .mdb database according to the course selected in the combo box.

Form

How do I populate the listbox with the students from my database (which I have)?

And to be more precise below is the code I have so far:

Public Class Form1 Dim co As New ADODB.Connection Dim rs As New ADODB.Recordset Dim confirm As Integer

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    co.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Documents\ProfileDB.mdb;Persist Security Info=False")
    rs.Open("Select * from ProfileTBL", co, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockPessimistic)
    Combo1.Items.Add("Computer Technology")
    Combo1.Items.Add("Business Management")
    display()
End Sub

Sub display()
    TextBox1.Text = rs.Fields.Item("FirstName").Value
    TextBox2.Text = rs.Fields.Item("Surname").Value
    Combo1.Text = rs.Fields.Item("Course").Value
    TextBox4.Text = rs.Fields.Item("Phone").Value
End Sub


Private Sub addnew_Click(sender As Object, e As EventArgs) Handles addnew.Click
    rs.AddNew()
    clear()
End Sub

Sub clear()
    TextBox1.Text = ""
    TextBox2.Text = ""
    Combo1.Text = "Select Course"
    TextBox4.Text = ""
End Sub

Private Sub Savebtn_Click(sender As Object, e As EventArgs) Handles Savebtn.Click
    rs.Fields("FirstName").Value = TextBox1.Text
    rs.Fields("Surname").Value = TextBox2.Text
    rs.Fields("Course").Value = Combo1.Text
    rs.Fields("Phone").Value = TextBox4.Text
    MsgBox("Data is saved successfully.", vbInformation)
    rs.Update()
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    rs.MovePrevious()
    If rs.BOF Then
        rs.MoveLast()
        display()
    Else
        display()
    End If
End Sub


Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    rs.MoveNext()
    If Not rs.EOF Then
        display()
    Else
        rs.MoveFirst()
        display()
    End If
End Sub

Private Sub deletebtn_Click(sender As Object, e As EventArgs) Handles deletebtn.Click
    confirm = MsgBox("Do you want to delete the Student Profile", vbYesNo + vbCritical, "Deletion Confirmation")
    If confirm = vbYes Then
        rs.Delete()
        MsgBox("Record has been Deleted successfully", vbInformation, "Message")
        rs.Update()
        Refresh()
        clear()

    Else
        MsgBox("Profile Not Deleted!", vbInformation, "Message")
    End If
End Sub



Private Sub findbtn_Click(sender As Object, e As EventArgs) Handles findbtn.Click
    rs.Close()
    rs.Open("Select * from ProfileTBL where Phone='" + TextBox4.Text + "'", co, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockPessimistic)
    If Not rs.EOF Then
        display()
        reload()
    Else
        MsgBox("Record Profile not found!", vbInformation)
    End If

End Sub

Sub reload()
    rs.Close()
    rs.Open("Select * from ProfileTBL", co, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockPessimistic)
End Sub


Private Sub listbtn_Click(sender As Object, e As EventArgs) Handles listbtn.Click


End Sub

End Class

1
Thx a lot! But my database is .mdb Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load co.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Documents\ProfileDB.mdb;Persist Security Info=False") rs.Open("Select * from ProfileTBL", co, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockPessimistic) Combo1.Items.Add("Computer Technology") Combo1.Items.Add("Business Management") display() End Sub - ciprian
please add all Information to your question. Edit it. Do not post them in comments. Especially if you use code - no code blocks in comments - only inline ones. - Patrick Artner

1 Answers

0
votes

Could be like below:

Private Sub btnListStudents_Click(sender As Object, e As EventArgs) Handles btnListStudents.Click
    Dim dt As New DataTable()
    dt.Columns.AddRange(New DataColumn() {New DataColumn("StudentId", GetType(Integer)),
                                           New DataColumn("StudentName", GetType(String)),
                                           New DataColumn("StudentCourse", GetType(Integer))})
    dt.Rows.Add(1, "John Hammond", 555)
    dt.Rows.Add(2, "Mudassar Khan", 666)
    dt.Rows.Add(3, "Suzanne Mathews", 777)
    dt.Rows.Add(4, "Robert Schidner", 888)
    dt.Rows.Add(5, "John Doo", 999)

    'Clear first
    lstStudents.Items.Clear()

    'Fill names into the listbox
    For Each row As DataRow In dt.Rows
        lstStudents.Items.Add(row("StudentName"))
    Next row
End Sub