Вы находитесь на странице: 1из 4

Imports System.Data.

OleDb
Imports System.IO

Public Class Form1

Dim strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data


Source=C:\Database\Contacts.accdb;Persist Security Info=False;"
Dim con As New OleDbConnection
Dim cmd As New OleDbCommand
Dim da As New OleDbDataAdapter
Dim dt As New DataTable
Dim save_tag As String

Sub Fill_Grid(grid As Windows.Forms.DataGridView)


Dim x As Integer

'# Refreshes the grid.


'# this method is useful when you want to reload the grid with new values
after saving entries
If grid.Rows.Count > 0 Then
While x < grid.Rows.Count
grid.Rows.RemoveAt(x)
End While
End If

'# Setting how the grid behaves or looks like when filled/loaded
'# you may add more properties here if necessary e.g alternating row
background colors
With grid
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
.RowHeadersVisible = False
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End With

'# Setting and opening a connection to the database via connection string
con.ConnectionString = strConn
con.Open()

'# Represents an SQL statement or stored procedure to execute against a


data source.
With cmd
.Connection = con
.CommandType = CommandType.Text
.CommandText = "Select * from Contacts"
End With
da.SelectCommand = cmd
grid.DataSource = dt
da.Fill(dt)

'# hide some of the unwanted columns


For i As Integer = 6 To 17
grid.Columns(i).Visible = False
Next
grid.Columns(0).Visible = False

'#closing the connection


con.Dispose()
con.Close()

End Sub

Sub save(ByVal tag As String)

Dim cmdtxt As String = Nothing


If tag = "new" Then
cmdtxt = "Insert into Contacts([Company], [First Name], [Last Name],
[E-mail Address], [Job Title]) " & _
"Values('" & TextBox4.Text & "','" & TextBox1.Text & "','" &
TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox5.Text & "' ) "

ElseIf tag = "edit" Then


cmdtxt = "Update Contacts set " & _
"[Company] = '" & TextBox4.Text.ToString & "',[First Name] =
'" & TextBox1.Text & "', [Last Name] = '" & TextBox2.Text & "' , [E-mail Address]
='" & TextBox3.Text & "' ,[Job Title]='" & TextBox5.Text & "' " & _
"Where ID = " & TextBox6.Text & ""
End If

Try
'The connection
con.ConnectionString = strConn
con.Open()

'The OLEDB Command


With cmd
.Connection = con
.CommandType = CommandType.Text
.CommandText = cmdtxt
End With

cmd.ExecuteNonQuery()

Catch ex As Exception
MsgBox(ex.Message, vbCritical)
Finally
con.Dispose()
con.Close()
End Try

End Sub

Sub delete(ByVal id As Integer)


Dim cmdtxt As String = Nothing

cmdtxt = "Delete From Contacts Where ID = " & id & ""

Try
'The connection
con.ConnectionString = strConn
con.Open()

'The OLEDB Command


With cmd
.Connection = con
.CommandType = CommandType.Text
.CommandText = cmdtxt
End With

cmd.ExecuteNonQuery()

Catch ex As Exception
MsgBox(ex.Message, vbCritical)
Finally
con.Dispose()
con.Close()
End Try
End Sub
Private Sub DataGridView1_CellMouseClick(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles
DataGridView1.CellMouseClick

'# Assign values to to textboxes from selected/higlighted row


'# e.index is the index value of current selected row
'# cell(1) for instance gets the value of the cell(row,column) of the
current row selected
TextBox1.Text = DataGridView1.Rows(e.RowIndex).Cells(3).Value.ToString
TextBox2.Text = DataGridView1.Rows(e.RowIndex).Cells(2).Value.ToString
TextBox3.Text = DataGridView1.Rows(e.RowIndex).Cells(4).Value.ToString
TextBox4.Text = DataGridView1.Rows(e.RowIndex).Cells(1).Value.ToString
TextBox5.Text = DataGridView1.Rows(e.RowIndex).Cells(5).Value.ToString
TextBox6.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString

save_tag = "edit"

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click

'# sets all textboxes empty


TextBox1.Text = String.Empty
TextBox2.Text = String.Empty
TextBox3.Text = String.Empty
TextBox4.Text = String.Empty
TextBox5.Text = String.Empty
TextBox1.Focus()

save_tag = "new"

End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button4.Click

'# calling the save() function


save(save_tag)
Button1.PerformClick()
Fill_Grid(DataGridView1)

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load

save_tag = "new"
Fill_Grid(DataGridView1)

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
Button1.PerformClick()
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
Me.Close()
End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button5.Click
Dim id As Integer

id = TextBox6.Text
delete(id)
Button1.PerformClick()
Fill_Grid(DataGridView1)
End Sub
End Class

Вам также может понравиться