Using DataGridView With Access - VB - Net Tutorials
Using DataGridView With Access - VB - Net Tutorials
NET Tutorials
about:reader?url=http://www.dreamincode.net/forums/topic/148532-us...
dreamincode.net
For me, I dont like using the tools VS provides that write your
databse code for you. I prefer to write all my code out that does my
Inserts, Updates, Adding and Deleting values to the database. Also,
anytime youre using OleDB, or SQL or any type of connection be
sure you import the namespace you will be needing. For OleDB we
will be using Imports System.Data.OleDb
Form 1(frmMain) - The Main Form
btnNew - New Record Button btnUpdate - Update Record Button
btnDelete - Delete Record Button dgv1 - DataGridView for Records
txtSearch - Search Records
Posted Image
First to connect to any Database source you have to setup your
connection string. I usually go with Access or SQL Server. If you are
looking into using another datasource check out
www.connectionstrings.com for other data sources like MySQL,
Oracle and lots more.
Imports System.Data.OleDb
Public Class frmMain
' Our Connection String - Be sure you
change the Data Source
1 de 14
05/10/2016 22:00
about:reader?url=http://www.dreamincode.net/forums/topic/148532-us...
Now we go into selecting our records from the Data Source and
populating out DataGridView with them.
' This displays our records in the
DataGridView so the User can select them to
update, delete, or search
Private Sub frmMain_Load(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
' Our SQL Statement
Dim sql As String
sql = "SELECT * FROM users"
' This is our DataAdapter. This
executes our SQL Statement above against the
Database
' we defined in the Connection
String
Dim adapter As New
OleDbDataAdapter(sql, con1)
' Gets the records from the
table and fills our adapter with those.
Dim dt As New DataTable("users")
adapter.Fill(dt)
' Assigns our DataSource on the
DataGridView
dgv1.DataSource = dt
'
2 de 14
05/10/2016 22:00
about:reader?url=http://www.dreamincode.net/forums/topic/148532-us...
3 de 14
05/10/2016 22:00
about:reader?url=http://www.dreamincode.net/forums/topic/148532-us...
4 de 14
05/10/2016 22:00
about:reader?url=http://www.dreamincode.net/forums/topic/148532-us...
End Sub
Private Sub dgv1_CellDoubleClick(ByVal
sender As Object, ByVal e As
System.Windows.Forms.DataGridViewCellEventArgs)
Handles dgv1.CellDoubleClick
frmUpdate.Show()
' Same thing as btnUpdate_Click
except the user can actually DoubleClick
' a record. You want to be sure
you get the field names and cell numbers
correct or the information
will be misconstrued.
frmUpdate.txtFirst.Text =
dgv1.Rows(e.RowIndex).Cells(0).Value.ToString
frmUpdate.txtLast.Text =
dgv1.Rows(e.RowIndex).Cells(1).Value.ToString
frmUpdate.txtAddress.Text =
dgv1.Rows(e.RowIndex).Cells(2).Value.ToString
frmUpdate.txtCity.Text =
dgv1.Rows(e.RowIndex).Cells(3).Value.ToString
frmUpdate.txtZip.Text =
dgv1.Rows(e.RowIndex).Cells(4).Value.ToString
frmUpdate.txtPhone.Text =
dgv1.Rows(e.RowIndex).Cells(5).Value.ToString
frmUpdate.txtEmail.Text =
dgv1.Rows(e.RowIndex).Cells(6).Value.ToString
frmUpdate.txtID.Text =
dgv1.Rows(e.RowIndex).Cells(7).Value.ToString
End Sub
5 de 14
05/10/2016 22:00
about:reader?url=http://www.dreamincode.net/forums/topic/148532-us...
Below is the code for our Delete Record function. You are able to
select a record and hit the Delete button and the record will be
deleted from both the DataGridView and the data source file. Be
sure you have a Unique ID or Field in your Data file so you can use
the WHERE statement to be sure that the correct record is deleted.
Private Sub btnDelete_Click(ByVal sender
As System.Object, ByVal e As System.EventArgs)
Handles btnDelete.Click
' This is our DELETE Statement. To be sure we
delete the correct record and not all of
them.
' We use the WHERE to be sure
only that record that the user has selected is
deleted.
Dim sqldelete As String
sqldelete = "DELETE * FROM users
WHERE IDNum='" &
dgv1.CurrentRow.Cells(7).Value.ToString & "'"
' This is our DataAdapter. This
executes our SQL Statement above against the
Database
' we defined in the Connection
String
Dim adapter As New
OleDbDataAdapter(sqldelete, con1)
' Gets the records from the
table and fills our adapter with those.
Dim dt As New DataTable("users")
adapter.Fill(dt)
' Assigns the edited DataSource
on the DataGridView and the refreshes the
6 de 14
05/10/2016 22:00
about:reader?url=http://www.dreamincode.net/forums/topic/148532-us...
updated, or deleted.
RefreshDGV()
End Sub
7 de 14
05/10/2016 22:00
about:reader?url=http://www.dreamincode.net/forums/topic/148532-us...
adapter.Fill(dt)
dgv1.DataSource = dt
'
End Sub
End Class
Now the code for the New Record Form to create new records.
Form 2(frmNew) - New Record Form
txtFirst txtLast txtAddress txtCity txtZip txtPhone txtEmail txtID
btnSave
Posted Image
Imports System.Data.Oledb
Public Class frmNew
' Our Connection String
Dim con1 As New
OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data
Source=C:\Users\Sethro\Desktop\Test.mdb")
The Save button is what saves the new information to our Data
source.
Private Sub btnSave_Click(ByVal sender
As System.Object, ByVal e As System.EventArgs)
Handles btnSave.Click
Dim sqlinsert As String
We use the INSERT statement
which tells our program to add the information
8 de 14
05/10/2016 22:00
about:reader?url=http://www.dreamincode.net/forums/topic/148532-us...
9 de 14
05/10/2016 22:00
about:reader?url=http://www.dreamincode.net/forums/topic/148532-us...
Now our code for the Update form to update any records.
Form 3(frmUpdate) - Update Record Form
txtFirst txtLast txtAddress txtCity txtZip txtPhone txtEmail txtID
btnUpdate
Posted Image
Imports System.Data.Oledb
Public Class frmUpdate
' Our Connection String
Dim con1 As New
OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data
Source=C:\Users\Sethro\Desktop\Test.mdb")
10 de 14
05/10/2016 22:00
about:reader?url=http://www.dreamincode.net/forums/topic/148532-us...
11 de 14
05/10/2016 22:00
about:reader?url=http://www.dreamincode.net/forums/topic/148532-us...
OleDbParameter("@LastName", txtLast.Text))
cmd.Parameters.Add(New
OleDbParameter("@Address", txtAddress.Text))
cmd.Parameters.Add(New
OleDbParameter("@City", txtCity.Text))
cmd.Parameters.Add(New
OleDbParameter("@Zip", txtZip.Text))
cmd.Parameters.Add(New
OleDbParameter("@Phone", txtPhone.Text))
cmd.Parameters.Add(New
OleDbParameter("@Email", txtEmail.Text))
' This is what actually writes
our changes to the DataBase.
' You have to open the
connection, execute the commands and
' then close connection.
con1.Open()
cmd.ExecuteNonQuery()
con1.Close()
' This are subs in Module1, to
clear all the TextBoxes on the form
' and refresh the DataGridView
on the MainForm to show our new records.
ClearTextBox(Me)
Me.Close()
RefreshDGV()
End Sub
End Class
12 de 14
05/10/2016 22:00
about:reader?url=http://www.dreamincode.net/forums/topic/148532-us...
Subs.
Imports System.Data.OleDb
Module Module1
Dim con1 As New
OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data
Source=C:\Users\Sethro\Desktop\Test.mdb")
Sub RefreshDGV()
Dim sql As String
sql = "SELECT * FROM users"
Dim adapter As New
OleDbDataAdapter(sql, con1)
Dim dt As New DataTable("users")
adapter.Fill(dt)
frmMain.dgv1.DataSource = dt
End Sub
Sub ClearTextBox(ByVal FormName As Form)
For Each txt As Control In
FormName.Controls
If TypeOf txt Is TextBox
Then
CType(txt,
TextBox).Text = ""
End If
Next
End Sub
End Module
13 de 14
05/10/2016 22:00
about:reader?url=http://www.dreamincode.net/forums/topic/148532-us...
Ive attached the project file so you can open it up and see all the
action for yourself.
[attachment=15608:attachment]
14 de 14
05/10/2016 22:00