Tải bản đầy đủ (.docx) (10 trang)

Giáo trình Visual Basic Tạo ứng dụng cụ thể

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (63.17 KB, 10 trang )


BÀI 15 - THIẾT KẾ MỘT ỨNG DỤNG CỤ THỂ
Imports System.Windows
Imports System.Data.OleDb
Public Class Form1
'======các biến mo tả chung
Dim dataSet1 As DataSet = New DataSet
Dim adapter As New OleDbDataAdapter()
Dim str1 As String = "provider=Microsoft.Jet.OLEDB.4.0; data source=" &
Application.StartupPath & "\ho_so.mdb"
Dim connect1 As OleDbConnection = New OleDbConnection(str1)
Dim bien_chon As Integer = 0 '1- nhap; 2- sửa; 3-tra cứu; 4-report
Dim hang, cot As Integer
'======================================
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
MonthCalendar1.Visible = False
GroupBox1.Visible = False
GroupBox2.Visible = False
GroupBox3.Visible = False
'connect1.Open()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'nhâp Data
Dim bang As New DataTable
bien_chon = 1 'nhap
GroupBox1.Visible = True
Dim queryString As String = "select * from table1 where ho_ten='$' "
adapter.SelectCommand = New OleDbCommand(queryString, connect1)
adapter.Fill(dataSet1, "table1")


DataGridView1.DataSource = dataSet1.Tables("table1")
DataGridView1.Refresh()
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox1.Focus()
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
'nhập mã ID
If Asc(e.KeyChar) = 13 Then
If Len(Trim(TextBox1.Text)) > 0 Then
TextBox2.Focus()
Else
TextBox1.Focus()
End If
End If
End Sub
'--------------------
Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
If Asc(e.KeyChar) = 13 Then
If Len(Trim(TextBox2.Text)) > 0 Then
TextBox3.Focus()
Else
TextBox2.Focus()
End If
End If
End Sub

'---------------------
Private Sub TextBox3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox3.Click
MonthCalendar1.Visible = True
End Sub
'

Private Sub TextBox4_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress
Dim row As DataRow
If Asc(e.KeyChar) = 13 Then
'-----------nhap-----
If bien_chon = 1 Then
row = dataSet1.Tables("table1").NewRow
If Len(Trim(TextBox3.Text)) > 0 Then
row("ID") = TextBox1.Text
row("ho_ten") = TextBox2.Text
row("ngay_sinh") = TextBox3.Text
row("thu_nhap") = Val(TextBox4.Text)
dataSet1.Tables("table1").Rows.Add(row)
DataGridView1.DataSource = dataSet1.Tables("table1")
DataGridView1.Refresh()
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox1.Focus()
End If
End If
'-----------sua ---------------------

If bien_chon = 2 Then
row = dataSet1.Tables("table1").NewRow
If Len(Trim(TextBox3.Text)) > 0 Then
DataGridView1.Rows(hang).Cells(0).Value = TextBox1.Text
DataGridView1.Rows(hang).Cells(1).Value = TextBox2.Text
DataGridView1.Rows(hang).Cells(3).Value = TextBox3.Text
DataGridView1.Rows(hang).Cells(2).Value = TextBox4.Text
row = dataSet1.Tables("table1").Rows(hang)
row("ID") = TextBox1.Text
row("ho_ten") = TextBox2.Text
row("ngay_sinh") = TextBox3.Text
row("thu_nhap") = Val(TextBox4.Text)
DataGridView1.Refresh()
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox1.Focus()
End If
End If
End If
End Sub
'-------------------------
Private Sub MonthCalendar1_DateSelected(ByVal sender As Object, ByVal e
As System.Windows.Forms.DateRangeEventArgs) Handles
MonthCalendar1.DateSelected
Dim date1 As Date
Dim dongngay As String = ""
date1 = e.Start
dongngay = Convert.ToString(date1.Day) + "/" +

Convert.ToString(date1.Month) + "/" + Convert.ToString(date1.Year)
TextBox3.Text = dongngay
MonthCalendar1.Visible = False
TextBox4.Focus()
End Sub
'--------------------
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
'sửa data
bien_chon = 2 'sửa
'-----------------------
If GroupBox2.Visible = False Then
GroupBox2.Visible = True
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
'ghi lại data
Dim builder As OleDbCommandBuilder = New
OleDbCommandBuilder(adapter)
adapter.Update(dataSet1, "table1")
MsgBox("ghi xong")
DataGridView1.ClearSelection()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button4.Click
'thôi nhập sửa
'-----------------xóa sach DataGridView1------------
Dim queryString As String = "select * from table1 where ho_ten='$' "
adapter.SelectCommand = New OleDbCommand(queryString, connect1)

adapter.Fill(dataSet1, "table1")
DataGridView1.DataSource = dataSet1.Tables("table1")
GroupBox1.Visible = False
GroupBox2.Visible = False
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button5.Click
'OK sửa
Dim queryString1 As String = ""
Dim menh_de_chon As String = "select * from table1 where "
Dim dong_dkien As String = ""
Dim str1 As String
If queryString1.Length = 0 Then
str1 = Trim(TextBox5.Text) 'mã số
If str1.Length > 0 Then
dong_dkien = "ID like " + "'" & str1 & "'"
queryString1 = menh_de_chon + " " + dong_dkien
End If
End If
'------------họ tên
If queryString1.Length = 0 Then

×