thieumao

Thêm sửa xóa table (VB6, Access)

Oct 20th, 2013
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.85 KB | None | 0 0
  1. Private Sub cmdDoc_Click()
  2.     Dim Cn  As ADODB.Connection
  3.     Dim Rs  As ADODB.Recordset
  4.     Dim strSQL As String
  5.     Dim bHople As Boolean
  6.     'Khoi tao moi mot doi tuong Connection
  7.     Set Cn = New ADODB.Connection
  8.     Cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;  Data Source = testAccess.MDB"
  9.     Cn.Open
  10.     'Thuc thi cau lenh SQL de lay tat ca Ten va Matkhau co trong CSDL
  11.     strSQL = "Select FirstName, LastName from FullName"
  12.     Set Rs = New ADODB.Recordset
  13.     Rs.Open strSQL, Cn, adOpenStatic, adLockOptimistic
  14.     'Kiem tra xem du lieu doc ra co hop le khong
  15.     If (Rs.BOF = True) Then
  16.     MsgBox "Khong doc duoc thong tin nguoi dung trong co so du lieu"
  17.     Rs.Close
  18.     Cn.Close
  19.     Exit Sub
  20.     End If
  21.     Rs.MoveFirst
  22.     bHople = False
  23.     'Lan luot Tim kiem xem username va password co ton tai trong CSDL khong
  24.     Do While ((Not Rs.EOF) And (Not bHople = True))
  25.         If (Rs![FirstName] = txtMot.Text And Rs![LastName] = txtHai.Text) Then
  26.             bHople = True
  27.         Else
  28.             Rs.MoveNext
  29.         End If
  30.     Loop
  31.     'Dong ket noi voi CSDL
  32.     Rs.Close
  33.     Cn.Close
  34.     If (bHople = True) Then
  35.         MsgBox "Co Phan Tu Nay Trong Table"
  36.         'Unload Me
  37.     Else
  38.         MsgBox "Nhap sai roi"
  39.     End If
  40. End Sub
  41.  
  42. Private Sub cmdLoad_Click()
  43.     LoadLai
  44. End Sub
  45.  
  46. Private Sub cmdSua_Click()
  47.     Dim Cn  As ADODB.Connection
  48.     Dim Rs  As ADODB.Recordset
  49.     Dim strSQL As String
  50.     Dim bHople As Boolean
  51.     'Khoi tao moi mot doi tuong Connection
  52.     Set Cn = New ADODB.Connection
  53.     Cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;  Data Source = testAccess.MDB"
  54.     Cn.Open
  55.     'Thuc thi cau lenh SQL de lay tat ca Ten va Matkhau co trong CSDL
  56.     'strSQL = "INSERT INTO FullName(FirstName, LastName) VALUES ('" & txtMot.Text & "','" & txtHai.Text & "')"
  57.     strSQL = "UPDATE FullName SET FirstName='" & txtMot.Text & "', LastName ='" & txtHai.Text & "' WHERE ID = " & txtIDsua.Text
  58.     Set Rs = New ADODB.Recordset
  59.     Rs.Open strSQL, Cn, adOpenStatic, adLockOptimistic
  60.     Cn.Close
  61.     LoadLai
  62. End Sub
  63.  
  64. Private Sub cmdThem_Click()
  65.     Dim Cn  As ADODB.Connection
  66.     Dim Rs  As ADODB.Recordset
  67.     Dim strSQL As String
  68.     Dim bHople As Boolean
  69.     'Khoi tao moi mot doi tuong Connection
  70.     Set Cn = New ADODB.Connection
  71.     Cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;  Data Source = testAccess.MDB"
  72.     Cn.Open
  73.     'Thuc thi cau lenh SQL de lay tat ca Ten va Matkhau co trong CSDL
  74.     strSQL = "INSERT INTO FullName(FirstName, LastName) VALUES ('" & txtMot.Text & "','" & txtHai.Text & "')"
  75.     Set Rs = New ADODB.Recordset
  76.     Rs.Open strSQL, Cn, adOpenStatic, adLockOptimistic
  77.     Cn.Close
  78.     LoadLai
  79. End Sub
  80.  
  81. Private Sub LoadLai()
  82.     Dim id As Integer
  83.     id = 0
  84.     With testMSFlexGrid
  85.         .Rows = 1
  86.         .Cols = 3
  87.         .TextMatrix(0, 0) = "ID"
  88.         .TextMatrix(0, 1) = "First Name"
  89.         .TextMatrix(0, 2) = "Last Name"
  90.     End With
  91.    
  92.     Dim Cn  As ADODB.Connection
  93.     Dim Rs  As ADODB.Recordset
  94.     Dim strSQL As String
  95.  
  96.     'Khoi tao moi mot doi tuong Connection
  97.     Set Cn = New ADODB.Connection
  98.     Cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;  Data Source = testAccess.MDB"
  99.     Cn.Open
  100.     'Thuc thi cau lenh SQL de lay tat ca Ten va Matkhau co trong CSDL
  101.     strSQL = "Select ID, FirstName, LastName from FullName"
  102.     Set Rs = New ADODB.Recordset
  103.     Rs.Open strSQL, Cn, adOpenStatic, adLockOptimistic
  104.     'Kiem tra xem du lieu doc ra co hop le khong
  105.     If (Rs.BOF = True) Then
  106.         MsgBox "Khong doc duoc thong tin nguoi dung trong co so du lieu"
  107.         Rs.Close
  108.         Cn.Close
  109.         Exit Sub
  110.     End If
  111.     Rs.MoveFirst
  112.     'Lan luot Tim kiem xem username va password co ton tai trong CSDL khong
  113.     Do While ((Not Rs.EOF))
  114.         id = id + 1
  115.         With testMSFlexGrid
  116.             .Rows = .Rows + 1
  117.             .TextMatrix(id, 0) = Rs![id]
  118.             .TextMatrix(id, 1) = Rs![FirstName]
  119.             .TextMatrix(id, 2) = Rs![LastName]
  120.         End With
  121.         Rs.MoveNext
  122.     Loop
  123.     'Dong ket noi voi CSDL
  124.     Rs.Close
  125.     Cn.Close
  126.     MsgBox "Da Load xong"
  127. End Sub
  128.  
  129. Private Sub cmdXoa_Click()
  130.     Dim Cn  As ADODB.Connection
  131.     Dim Rs  As ADODB.Recordset
  132.     Dim strSQL As String
  133.     Dim bHople As Boolean
  134.     'Khoi tao moi mot doi tuong Connection
  135.     Set Cn = New ADODB.Connection
  136.     Cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;  Data Source = testAccess.MDB"
  137.     Cn.Open
  138.     'Thuc thi cau lenh SQL de lay tat ca Ten va Matkhau co trong CSDL
  139.     'strSQL = "INSERT INTO FullName(FirstName, LastName) VALUES ('" & txtMot.Text & "','" & txtHai.Text & "')"
  140.     strSQL = "DELETE FROM FullName WHERE ID = " & txtIDsua.Text
  141.     '" & txtIDsua.Text & "'"
  142.     Set Rs = New ADODB.Recordset
  143.     Rs.Open strSQL, Cn, adOpenStatic, adLockOptimistic
  144.     Cn.Close
  145.     LoadLai
  146. End Sub
Advertisement
Add Comment
Please, Sign In to add comment