'
今天的客户也是小客户,需要对文件的一些操作记录在数据库里面,这里采用的数据库ACCCESS,用的是VB自带的可视化数据管理器创建的mdb数据库文件。实现了数据的增删改查。简单方便。基本可以用这套代码模板实现大部分的数据库功能了。想研发或学习的朋友可以加我q1016058890
Dim DataRst As New ADODB.Recordset
    
 Private Sub Command10_Click()
   Set VSFlexGrid1.DataSource = DataRst  '刷新表格
 End Sub
Private Sub Command11_Click()
   Text2 = Format(Now, "yyyy/mm/dd")
 End Sub
Private Sub Command12_Click()
    Dim DataRst2     As New ADODB.Recordset
       sSQL = "Select * from 文件信息表 where  文件 like '%" & Text3 & "%'" & "ORDER BY 日期 ASC"
       DataRst2.Open sSQL, cnn, adOpenKeyset, adLockReadOnly  ' 重新打开表
       If Not DataRst2.EOF Or Not DataRst2.BOF Then  '没有到顶 或者 没有到底
         Set VSFlexGrid1.DataSource = DataRst2  '刷新表格
       End If
         Label3.Caption = DataRst2.RecordCount '显示查询结果的总数
       DataRst2.Close '最好是写上,不过不谢也没事
       
End Sub
Private Sub Command13_Click()
   Dim DataRst2     As New ADODB.Recordset
       sSQL = "Select * from 文件信息表 where  文件 like '%" & Text3 & "%'" & "ORDER BY 日期 DESC"
       DataRst2.Open sSQL, cnn, adOpenKeyset, adLockReadOnly  ' 重新打开表
       If Not DataRst2.EOF Or Not DataRst2.BOF Then  '没有到顶 或者 没有到底
         Set VSFlexGrid1.DataSource = DataRst2  '刷新表格
       End If
         Label3.Caption = DataRst2.RecordCount '显示查询结果的总数
       DataRst2.Close '最好是写上,不过不谢也没事
 End Sub
Private Sub Command14_Click()
     Dim DataRst2     As New ADODB.Recordset
       sSQL = "Select * from 文件信息表 where  文件 like '%" & Text3 & "%'" & "ORDER BY 文件 ASC"
       DataRst2.Open sSQL, cnn, adOpenKeyset, adLockReadOnly  ' 重新打开表
       If Not DataRst2.EOF Or Not DataRst2.BOF Then  '没有到顶 或者 没有到底
         Set VSFlexGrid1.DataSource = DataRst2  '刷新表格
       End If
         Label3.Caption = DataRst2.RecordCount '显示查询结果的总数
       DataRst2.Close '最好是写上,不过不谢也没事
       
End Sub
Private Sub Command15_Click()
   Dim DataRst2     As New ADODB.Recordset
       sSQL = "Select * from 文件信息表 where  文件 like '%" & Text3 & "%'" & "ORDER BY 文件 DESC"
       DataRst2.Open sSQL, cnn, adOpenKeyset, adLockReadOnly  ' 重新打开表
       If Not DataRst2.EOF Or Not DataRst2.BOF Then  '没有到顶 或者 没有到底
         Set VSFlexGrid1.DataSource = DataRst2  '刷新表格
       End If
         Label3.Caption = DataRst2.RecordCount '显示查询结果的总数
       DataRst2.Close '最好是写上,不过不谢也没事
 End Sub
 Private Sub Command9_Click()
     Dim DataRst2     As New ADODB.Recordset
       sSQL = "Select * from 文件信息表 where  文件 like '%" & Text3 & "%'"
       DataRst2.Open sSQL, cnn, adOpenKeyset, adLockReadOnly  ' 重新打开表
       If Not DataRst2.EOF Or Not DataRst2.BOF Then  '没有到顶 或者 没有到底
         Set VSFlexGrid1.DataSource = DataRst2  '刷新表格
       End If
         Label3.Caption = DataRst2.RecordCount '显示查询结果的总数
       DataRst2.Close '最好是写上,不过不谢也没事
       
       
   
 End Sub
Private Sub Form_Load()
     
     Call 数据库连接
    Dim sSQL As String
    sSQL = "Select * from 文件信息表"
    DataRst.Open sSQL, cnn, adOpenKeyset, adLockPessimistic   '读取表  adLockReadOnly只读
    Label3.Caption = DataRst.RecordCount '显示查询结果的总数
     Set VSFlexGrid1.DataSource = DataRst  '刷新表格
     
     
     
     Text2 = Format(Now, "yyyy/mm/dd")
     
     
     
     设置VSFlexGrid1数据表格
  End Sub
  
  Private Sub Form_Unload(Cancel As Integer)
    DataRst.Close
 End Sub
  
  
  
  
  
  
 Private Sub 设置VSFlexGrid1数据表格()
  VSFlexGrid1.BackColorAlternate = RGB(255, 250, 230)  '间隔行背景色
      VSFlexGrid1.RowHeightMin = 400                      '最小行高
      
             Dim i  As Long
             For i = 1 To 7
                   If i = 1 Then
                     VSFlexGrid1.ColWidth(i) = 10000              '指定列宽
                   ElseIf i = 2 Then
                      VSFlexGrid1.ColWidth(i) = 1000              '指定列宽
                  ElseIf i = 3 Then
                      VSFlexGrid1.ColWidth(i) = 1000         '指定列宽
                      
                  ElseIf i = 4 Then
                      VSFlexGrid1.ColWidth(i) = 3000         '指定列宽
                 Else
                      
                   End If
                 
             Next i
 '           VSFlexGrid1.ColAlignment(-1) = flexAlignCenterCenter
 End Sub
 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  Private Sub Command1_Click()
 If DataRst.RecordCount > 0 Then
   DataRst.MoveFirst
   
   '  如果你当前是指向数据集的第一项,那么moveprevious就会出错。方法是,用rs.Eof来判断是否到尾端,用Rs.Bof判断是否在首端。
    If DataRst.EOF = False And DataRst.BOF = False Then  '没有到顶 或者 没有到底
         Text1.Text = DataRst.Fields("姓名")
         Text2.Text = DataRst.Fields("年龄")
     End If
     
      Label3.Caption = DataRst.AbsolutePosition & "/" & DataRst.RecordCount
End If
  
    
   End Sub
 Private Sub Command2_Click()
If DataRst.RecordCount > 0 Then
   '  如果你当前是指向数据集的第一项,那么moveprevious就会出错。方法是,用rs.Eof来判断是否到尾端,用Rs.Bof判断是否在首端。
   DataRst.MoveNext
   If DataRst.EOF = False And DataRst.BOF = Flase Then
 '        Text1.Text = DataRst.Fields("姓名")
 '        Text2.Text = DataRst.Fields("年龄")
  Else
          DataRst.MoveLast
   End If
   
 '      Label3.Caption = DataRst.AbsolutePosition & "/" & DataRst.RecordCount
  End If
     'Err.Description
 End Sub
Private Sub Command3_Click()
If DataRst.RecordCount > 0 Then
            DataRst.MovePrevious
            
           '  如果你当前是指向数据集的第一项,那么moveprevious就会出错。方法是,用rs.Eof来判断是否到尾端,用Rs.Bof判断是否在首端。
            If DataRst.EOF = False And DataRst.BOF = False Then      '没有到顶 或者 没有到底
         
 '                Text1.Text = DataRst.Fields("姓名")
 '                Text2.Text = DataRst.Fields("年龄")
                 Else
                 
                  DataRst.MoveFirst
             End If
             
 '                  Label3.Caption = DataRst.AbsolutePosition & "/" & DataRst.RecordCount
 '
     End If
   
    'Err.Description
 End Sub
Private Sub Command4_Click()
If DataRst.RecordCount > 0 Then
 DataRst.MoveLast
   '  如果你当前是指向数据集的第一项,那么moveprevious就会出错。方法是,用rs.Eof来判断是否到尾端,用Rs.Bof判断是否在首端。
    If DataRst.EOF = False And DataRst.BOF = False Then  '没有到顶 或者 没有到底
 '        Text1.Text = DataRst.Fields("姓名")
 '        Text2.Text = DataRst.Fields("年龄")
     End If
 '        Label3.Caption = DataRst.AbsolutePosition & "/" & DataRst.RecordCount
  End If
    'Err.Description
 End Sub
Private Sub Command5_Click() '精确查询
 '      Dim DataRst2     As New ADODB.Recordset
 '      sSQL = "Select * from 人员信息表 where  姓名 = '" & Text3 & "'"
 '      DataRst2.Open sSQL, cnn, adOpenKeyset, adLockReadOnly  ' 重新打开表
 '      If Not DataRst2.EOF Or Not DataRst2.BOF Then  '没有到顶 或者 没有到底
 '        Text1.Text = DataRst2.Fields("姓名")
 '        Text2.Text = DataRst2.Fields("年龄")
 '      End If
 '      DataRst2.Close '最好是写上,不过不谢也没事
 End Sub
 Private Sub Command6_Click()
If Text1 = "" Or Text2 = "" Or Combo1.Text = "" Then
  MsgBox "请填写文件信息,否则无法新增!"
  Exit Sub
 End If
If Text4 = "" Then
     Text4 = "无"
 End If
  
 Dim AdoObj  As New ADODB.Recordset
  
     sSQL = "Select * from 文件信息表 "   '客户电话
     AdoObj.Open sSQL, cnn, adOpenKeyset, adLockPessimistic
     
             AdoObj.AddNew
             AdoObj!文件 = Text1.Text
             AdoObj!日期 = Text2.Text
             AdoObj!动作 = Combo1.Text
             AdoObj!备注 = Text4.Text
        
        
        
        
        
             AdoObj.Update
             AdoObj.Close
             
          
             
            sSQL = "Select * from 文件信息表 "   '客户电话
                               AdoObj.Open sSQL, cnn, adOpenKeyset, adLockPessimistic
                              
                              
                               
                                 
                                 Set VSFlexGrid1.DataSource = AdoObj  '刷新表格
                                 
                                 Label3.Caption = AdoObj.RecordCount '显示查询结果的总数
                                 
                                 AdoObj.Close
                             MsgBox "保存成功"
         
         
         
         
         
         
     
         
         
         
         
         
         
 End Sub
Private Sub Command7_Click()
If Text1 = "" Or Text2 = "" Or Combo1.Text = "" Then
  MsgBox "请在表格中选择你要删除的数据"
  Exit Sub
 End If
       Dim AdoObj  As New ADODB.Recordset
        Dim sSQL    As String
        If MsgBox("确定要删除吗?", vbYesNo, "提示") = vbNo Then Exit Sub
     
         sSQL = "Select * from 文件信息表 where 文件='" & Text1.Text & "'" & "  and  日期=#" & Text2 & "#" & "  and  动作='" & Combo1.Text & "'"
         AdoObj.Open sSQL, cnn, adOpenKeyset, adLockPessimistic
         If AdoObj.RecordCount > 0 Then
                         AdoObj.Delete
                         AdoObj.Update
                         AdoObj.Close
             
                         sSQL = "Select * from 文件信息表"
                         AdoObj.Open sSQL, cnn, adOpenKeyset, adLockPessimistic
                          Set VSFlexGrid1.DataSource = AdoObj  '刷新表格
                          Label3.Caption = AdoObj.RecordCount
                           AdoObj.Close
                           
                           
                           
                           Text1 = ""
                           
                            Text2 = ""
                            
                            Combo1.Text = ""
                            
          Else
                            
                              MsgBox "删除失败"
         End If
         
         
         
         
         
         
         
End Sub
Private Sub Command8_Click()
    Dim AdoObj  As New ADODB.Recordset
     Dim sSQL    As String
     '注意这里不能用 本界面的 txtUser 控件来获取姓名 进行修改查询的,因为这个时候你的名称已经修改了,你无法查询到这个名称,会修改失败的!
     sSQL = "Select * from 文件信息表 where 文件='" & VSFlexGrid1.TextMatrix(VSFlexGrid1.RowSel, 1) & "'" & "  and  日期=#" & VSFlexGrid1.TextMatrix(VSFlexGrid1.RowSel, 2) & "#" & "  and  动作='" & VSFlexGrid1.TextMatrix(VSFlexGrid1.RowSel, 3) & "'"
     AdoObj.Open sSQL, cnn, adOpenKeyset, adLockPessimistic
                If AdoObj.RecordCount > 0 Then
                               AdoObj!文件 = Text1.Text
                               AdoObj!日期 = Text2.Text
                               AdoObj!动作 = Combo1.Text
                                AdoObj!备注 = Text4.Text
                               AdoObj.Update
                               AdoObj.Close
                   
                               sSQL = "Select * from 文件信息表 "   '客户电话
                               AdoObj.Open sSQL, cnn, adOpenKeyset, adLockPessimistic
                               Set VSFlexGrid1.DataSource = AdoObj  '刷新表格
                                Label3.Caption = AdoObj.RecordCount
                               AdoObj.Close
                               
                               
                               
                            Text1 = ""
                           
                            Text2 = ""
                            
                            Combo1.Text = ""
                               
                               MsgBox "修改成功"
                               
                               
                               
                               
                              
                 Else
                               MsgBox "修改失败"
                 End If
   
   
   
   
 End Sub
Private Sub Text1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
     For i = 1 To Data.Files.Count ' 逐个读取文件路径
      Text1 = Data.Files(i)
     Next
 End Sub
 Private Sub VSFlexGrid1_Click()
       Text1.Text = VSFlexGrid1.TextMatrix(VSFlexGrid1.RowSel, 1)  '文件名
         Text2.Text = VSFlexGrid1.TextMatrix(VSFlexGrid1.RowSel, 2) ' 日期
         Combo1.Text = VSFlexGrid1.TextMatrix(VSFlexGrid1.RowSel, 3)  '动作
          Text4.Text = VSFlexGrid1.TextMatrix(VSFlexGrid1.RowSel, 4) ' 日期
 End Sub
  



















