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
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
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
Dim d1 As Integer = 5
Dim value As Integer
Dim mark As Integer = 0
Dim final As Double = 0
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
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
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
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
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
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()
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