Click here to Skip to main content
15,885,823 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hey there, im trying to insert, update, delete, and search data from vb into microsoft access. the problem is i want to use combobox from vb and store the data in access. how do i do it? below are my code. where do i need to change since i used tutorial used to insert, update, delete, and search text data and not combobox. thanks :D

VB
Public Class Form1

    Public con As New OleDb.OleDbConnection("Provider = Microsoft.JET.OLEDB.4.0;Data Source=example.mdb")
    Public Sub Display_Data()
        con.Open()
        Dim dt As New DataTable("table1")
        Dim rs As New OleDb.OleDbDataAdapter("SELECT * FROM table1", con)
        rs.Fill(dt)
        DataGridView1.DataSource = dt
        DataGridView1.Refresh()
        Label7.Text = dt.Rows.Count

        rs.Dispose()
        con.Close()

    End Sub
    Public Sub Add_Data()
        con.Open()

        Dim rs As New OleDb.OleDbCommand("Insert into table1 (id, control1, control2, control2, control3, control4, control5) values('" & TextBox1.Text & "', '" & ComboBox1.Text & "', '" & ComboBox2.Text & "', '" & ComboBox3.Text & "', '" & ComboBox4.Text & "', '" & ComboBox5.Text & "')", con)

        con.Close()
        Display_Data()


    End Sub
    Public Sub Get_Data()
        con.Open()

        Dim dt As New DataTable("table1")
        Dim rs As New OleDb.OleDbDataAdapter("select * from table1 where id= '" & TextBox1.Text & "' ", con)
        rs.Fill(dt)

        DataGridView1.DataSource = dt
        DataGridView1.Refresh()

        rs.Dispose()
        con.Close()

        If Val(Label7.Text) = 1 Then
            Dim i As Integer
            i = DataGridView1.CurrentRow.Index

            ComboBox1.Text = DataGridView1.Item(1, i).Value
            ComboBox2.Text = DataGridView1.Item(2, i).Value
            ComboBox3.Text = DataGridView1.Item(3, i).Value
            ComboBox4.Text = DataGridView1.Item(4, i).Value
            ComboBox5.Text = DataGridView1.Item(5, i).Value

        End If
        'Display_Data()

    End Sub
    Public Sub Update_Data()

        con.Open()
        Dim rs As New OleDb.OleDbCommand("Update table1 set control1='" & ComboBox1.Text & "', control2='" & ComboBox2.Text & "', control3='" & ComboBox3.Text & "', control4='" & ComboBox4.Text & "', control5='" & ComboBox5.Text & "' where id='" & TextBox1.Text & "' ", con)
        rs.ExecuteNonQuery()

        con.Close()
        Display_Data()

    End Sub
    Public Sub Delete_Data()

        con.Open()
        Dim rs As New OleDb.OleDbCommand("Delete * from table1 where id='" & TextBox1.Text & "' ", con)
        rs.ExecuteNonQuery()

        con.Close()
        Display_Data()

    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'jumlah control ntok setiap domain
        Dim d1 As Integer = 5
        'markah ntok setiap options 0-6
        Dim value As Integer
        'total mark ntok domain b4 avg
        Dim mark As Integer = 0
        'calculate avg
        Dim final As Double = 0

        'combobox1

        If ComboBox1.SelectedIndex = 0 Then
            value = 0
        ElseIf ComboBox1.SelectedIndex = 1 Then
            value = 1
        ElseIf ComboBox1.SelectedIndex = 2 Then
            value = 2
        ElseIf ComboBox1.SelectedIndex = 3 Then
            value = 3
        ElseIf ComboBox1.SelectedIndex = 4 Then
            value = 4
        Else
            d1 = d1 - 1
            value = 0
        End If
        mark = mark + value 'combobox1

        'combobox2

        If ComboBox2.SelectedIndex = 0 Then
            value = 0
        ElseIf ComboBox2.SelectedIndex = 1 Then
            value = 1
        ElseIf ComboBox2.SelectedIndex = 2 Then
            value = 2
        ElseIf ComboBox2.SelectedIndex = 3 Then
            value = 3
        ElseIf ComboBox2.SelectedIndex = 4 Then
            value = 4
        Else
            d1 = d1 - 1
            value = 0
        End If
        mark = mark + value 'combobox1+combobox2

        'combobox3

        If ComboBox3.SelectedIndex = 0 Then
            value = 0
        ElseIf ComboBox3.SelectedIndex = 1 Then
            value = 1
        ElseIf ComboBox3.SelectedIndex = 2 Then
            value = 2
        ElseIf ComboBox3.SelectedIndex = 3 Then
            value = 3
        ElseIf ComboBox3.SelectedIndex = 4 Then
            value = 4
        Else
            d1 = d1 - 1
            value = 0
        End If
        mark = mark + value 'combobox1+combobox2+combobox3

        'combobox4

        If ComboBox4.SelectedIndex = 0 Then
            value = 0
        ElseIf ComboBox4.SelectedIndex = 1 Then
            value = 1
        ElseIf ComboBox4.SelectedIndex = 2 Then
            value = 2
        ElseIf ComboBox4.SelectedIndex = 3 Then
            value = 3
        ElseIf ComboBox4.SelectedIndex = 4 Then
            value = 4
        Else
            d1 = d1 - 1
            value = 0
        End If
        mark = mark + value 'combobox1+combobox2+combobox3+combobox4

        'combobox5

        If ComboBox5.SelectedIndex = 0 Then
            value = 0
        ElseIf ComboBox5.SelectedIndex = 1 Then
            value = 1
        ElseIf ComboBox5.SelectedIndex = 2 Then
            value = 2
        ElseIf ComboBox5.SelectedIndex = 3 Then
            value = 3
        ElseIf ComboBox5.SelectedIndex = 4 Then
            value = 4
        Else
            d1 = d1 - 1
            value = 0
        End If
        mark = mark + value 'combobox1+combobox2+combobox3+combobox4+combobox5

        final = mark / d1
        MsgBox(final)


    End Sub

    Public Sub Search_Data()
        con.Open()

        Dim dt As New DataTable("table1")
        Dim rs As New OleDb.OleDbDataAdapter("select * from table1 where id= '" & TextBox1.Text & "' ", con)
        rs.Fill(dt)

        DataGridView1.DataSource = dt
        DataGridView1.Refresh()
        Label7.Text = dt.rows.Count

        rs.Dispose()
        con.Close()

        'Display_Data()

    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Display_Data()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Add_Data()

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Get_Data()

    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Search_Data()

    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Update_Data()

    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        Delete_Data()

    End Sub
End Class
Posted
Comments
Michael_Davies 26-Jun-15 1:45am    
First of all validate that the comboboxes have an item selected then you need to store the selected value.

Check combobox_object.selectedindex <> -1 to make sure it has a valid selection.

To save the value of the selected combobox item use combobox_object.selecteditem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900