Giáo trình Visual Basic Tạo ứng dụng cụ thể
BÀI 15 - THIẾT KẾ MỘT ỨNG DỤNG CỤ THỂImports System.WindowsImports System.Data.OleDbPublic 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 str1 = Trim(TextBox6.Text) 'họ tên If str1.Length > 0 Then dong_dkien = "ho_ten like " + "'%" & str1 & "%'" queryString1 = menh_de_chon + " " + dong_dkien End If End If '-------------------ngày sinh If queryString1.Length = 0 Then str1 = Trim(TextBox7.Text) 'họ tên If str1.Length > 0 Then dong_dkien = "ngay_sinh like " + "'" & str1 & "'" queryString1 = menh_de_chon + " " + dong_dkien End If End If '----------------------thu nhâp------- If (queryString1.Length = 0) Then str1 = Trim(TextBox8.Text) ' If (CheckBox1.Checked = True And str1.Length > 0 And IsNumeric(str1)) Then dong_dkien = "thu_nhap >=" & Val(str1) queryString1 = menh_de_chon + " " + dong_dkien End If End If '------------------------ If queryString1.Length = 0 Then str1 = Trim(TextBox9.Text) If (CheckBox2.Checked = True And str1.Length > 0 And IsNumeric(str1)) Then dong_dkien = "thu_nhap <=" & Val(str1) queryString1 = menh_de_chon + " " + dong_dkien End If End If '------------------------------------------------ If (queryString1.Length = 0) Then MsgBox(" cần có tiêu chuẩn tìm") Exit Sub End If '--------------------------------- adapter.SelectCommand = New OleDbCommand(queryString1, connect1) dataSet1 = New DataSet adapter.Fill(dataSet1, "table1") If dataSet1.Tables("table1").Rows.Count > 0 Then If GroupBox1.Visible = False Then GroupBox1.Visible = True End If DataGridView1.DataSource = dataSet1.Tables("table1") TextBox1.Clear() TextBox2.Clear() TextBox3.Clear() TextBox4.Clear() Else 'DataGridView1.DataSource = bang MsgBox(" không tìm thấy") End If End Sub Private Sub DataGridView1_CellMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick If bien_chon = 2 Then 'sua cot = e.ColumnIndex hang = e.RowIndex TextBox1.Text = DataGridView1.Rows(hang).Cells(0).Value 'ID TextBox2.Text = DataGridView1.Rows(hang).Cells(1).Value 'ho_ten TextBox3.Text = DataGridView1.Rows(hang).Cells(3).Value TextBox4.Text = DataGridView1.Rows(hang).Cells(2).Value End If End Sub Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click bien_chon = 3 If GroupBox3.Visible = False Then GroupBox3.Visible = True End If End Sub Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click 'tra cứu thông tin Dim queryString1 As String = "" '---------------------------tính tổng thu nhập If queryString1.Length = 0 Then If CheckBox3.Checked = True Then queryString1 = "SELECT SUM(thu_nhap) as tong_thu_nhap FROM table1" tra_cuu_thong_tin(str1, queryString1) Exit Sub End If End If '------------------------tính thu nhạp trung bình------------ If queryString1.Length = 0 Then If CheckBox4.Checked = True Then queryString1 = "SELECT AVG(thu_nhap)as thu_nhap_binh FROM table1" tra_cuu_thong_tin(str1, queryString1) Exit Sub End If End If '-------------tổng số cán bộ---------- If queryString1.Length = 0 Then If CheckBox5.Checked = True Then queryString1 = "SELECT COUNT(*) as so_can_bo FROM table1" tra_cuu_thong_tin(str1, queryString1) Exit Sub End If End If End Sub '-------------------- Private Sub tra_cuu_thong_tin(ByVal connect_String As String, ByVal dong_query As String) Dim adp As New OleDbDataAdapter(dong_query, connect_String) Dim bang As New DataTable Try adp.Fill(bang) DataGridView2.DataSource = bang Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button9_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click GroupBox3.Visible = False End Sub Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click TextBox5.Clear() TextBox6.Clear() TextBox7.Clear() TextBox8.Clear() TextBox9.Clear() '-----------------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") If GroupBox1.Visible = True Then GroupBox1.Visible = False End If GroupBox2.Visible = False End Sub Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click 'kết thúc chương trình End End SubEnd ClassCÁC LƯU Ý- Biến môi trường Application.StartupPath- Tạo query qua hội thoại- Cách sửa dữ liệu từ DataGridView- Cách tạo hội thoại tiện lợi logic- Tạo biến chọn bien_chon kiểm soát người sử dụng bấm gì- Sử dụng GroupBox phân chia nhóm công việc để không bị rối- Tham khảo thêm cách sort, find trên DataSet và DataGridview . BÀI 15 - THIẾT KẾ MỘT ỨNG DỤNG CỤ THỂImports System.WindowsImports System.Data.OleDbPublic Class Form1 . dữ liệu từ DataGridView- Cách tạo hội thoại tiện lợi logic- Tạo biến chọn bien_chon kiểm soát người sử dụng bấm gì- Sử dụng GroupBox phân chia nhóm công