VB操作 ADO的筛选,查找,排序等基本方法

'作者:CSDN 许仙
'Homepage : jjweb.126.com
'MSN :Coderxu#hotmail.com
'QQ:19030300
'转载请保持文章完整,保存以上作者信息 请珍惜他人劳动成果

VERSION 5.00
Object = "{CDE57A40-8B86-11D0-B3C6-00A0C90AEA82}#1.0#0"; "MSDATGRD.OCX"
Begin VB.Form main
   Appearance      =   0  'Flat
   BackColor       =   &H80000004&
   Caption         =   "ADO属性示例"
   ClientHeight    =   5055
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   9420
   LinkTopic       =   "Form1"
   ScaleHeight     =   5055
   ScaleWidth      =   9420
   Begin VB.CommandButton cmdFindNEXT
      Caption         =   "Find向后查找"
      Height          =   375
      Left            =   6000
      TabIndex        =   8
      Top             =   1584
      Width           =   1455
   End
   Begin VB.CommandButton cmdAll
      Caption         =   "All"
      Height          =   375
      Left            =   6000
      TabIndex        =   7
      Top             =   2400
      Width           =   1455
   End
   Begin VB.CommandButton cmdFilter
      Caption         =   "Filter"
      Height          =   375
      Left            =   6000
      TabIndex        =   6
      Top             =   1992
      Width           =   1455
   End
   Begin VB.TextBox txtName
      Height          =   375
      Left            =   6120
      TabIndex        =   5
      Text            =   "法师"
      Top             =   3480
      Width           =   1695
   End
   Begin VB.CommandButton cmdFind
      Caption         =   "Find向前查找"
      Height          =   375
      Left            =   6000
      TabIndex        =   4
      Top             =   1176
      Width           =   1455
   End
   Begin VB.CommandButton cmdDESC
      Caption         =   "降序"
      Height          =   375
      Left            =   6000
      TabIndex        =   3
      Top             =   768
      Width           =   1455
   End
   Begin VB.CommandButton cmdASC
      Caption         =   "升序"
      Height          =   375
      Left            =   6000
      TabIndex        =   2
      Top             =   360
      Width           =   1455
   End
   Begin MSDataGridLib.DataGrid DataGrid1
      Height          =   4455
      Left            =   0
      TabIndex        =   0
      Top             =   240
      Width           =   5775
      _ExtentX        =   10186
      _ExtentY        =   7858
      _Version        =   393216
      AllowUpdate     =   0   'False
      BackColor       =   12640511
      ForeColor       =   16711680
      HeadLines       =   1
      RowHeight       =   15
      BeginProperty HeadFont {0BE35203-8F91-11CE-9DE3-00AA004BB851}
         Name            =   "宋体"
         Size            =   9
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851}
         Name            =   "宋体"
         Size            =   9
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Caption         =   "点击可以查看怪物信息"
      ColumnCount     =   2
      BeginProperty Column00
         DataField       =   ""
         Caption         =   ""
         BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED}
            Type            =   0
            Format          =   ""
            HaveTrueFalseNull=   0
            FirstDayOfWeek  =   0
            FirstWeekOfYear =   0
            LCID            =   2052
            SubFormatType   =   0
         EndProperty
      EndProperty
      BeginProperty Column01
         DataField       =   ""
         Caption         =   ""
         BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED}
            Type            =   0
            Format          =   ""
            HaveTrueFalseNull=   0
            FirstDayOfWeek  =   0
            FirstWeekOfYear =   0
            LCID            =   2052
            SubFormatType   =   0
         EndProperty
      EndProperty
      SplitCount      =   1
      BeginProperty Split0
         BeginProperty Column00
         EndProperty
         BeginProperty Column01
         EndProperty
      EndProperty
   End
   Begin VB.Label lblLabel1
      Caption         =   "筛选条件和查找条件"
      Height          =   255
      Left            =   6120
      TabIndex        =   9
      Top             =   3120
      Width           =   1695
   End
   Begin VB.Label Label11
      AutoSize        =   -1  'True
      Height          =   180
      Left            =   0
      TabIndex        =   1
      Top             =   0
      Width           =   90
   End
End
Attribute VB_Name = "main"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'<CSCM>
'--------------------------------------------------------------------------------
' Project    :       scanmir3
' Procedure  :
' Description:       本例介绍了 ADO的筛选,查找,排序等方法
' Created by :       Project Administrator
' Machine    :       XiaoXu_
' Date-Time  :       12-13-2005-23:06:46
'HomePage    :      http://blog.csdn.net/hot1kang1/
' Parameters :
'--------------------------------------------------------------------------------
'</CSCM>
Dim Con As New ADODB.Connection
Dim Rst As New ADODB.Recordset
Dim rs As New ADODB.Recordset

Private Sub cmdAll_Click()
    Set Rst = Rst.Clone
    Set DataGrid1.DataSource = Rst
    Label11.Caption = "当前记录为:" & Rst.RecordCount & "条"
End Sub

Private Sub cmdASC_Click()
    Rst.Sort = "Name ASC" '升序 默认
End Sub

Private Sub cmdDESC_Click()
    Rst.Sort = "Name DESC" '降序
End Sub

Private Sub cmdFilter_Click()
    Rst.Filter = "Name like '%" & txtName & "%'"
    Label11.Caption = "当前记录为:" & Rst.RecordCount & "条"
    '筛选
End Sub

Private Sub cmdFind_Click()
    '向前查找
    On Error GoTo hErr
    Rst.Find "Name like '%" & txtName & "%'", , adSearchBackward, Rst.Bookmark - 1
    '查找/
    Label11.Caption = "当前记录为:" & Rst.RecordCount & "条"
hErr:
    If Err.Number = 3021 Or Rst.BOF Or Rst.EOF Then
        MsgBox "查找到最前了,没有匹配记录"
        Rst.MoveFirst
    End If
End Sub

Private Sub cmdFindNEXT_Click()
    '向后查找
    On Error GoTo hErr
    Dim lngBookMark As Long
    lngBookMark = Rst.Bookmark
    Rst.Find "Name like '%" & txtName & "%'", , adSearchForward, Rst.Bookmark + 1
    '查找/
    Label11.Caption = "当前记录为:" & Rst.RecordCount & "条"
hErr:
    If Err.Number = 3021 Or Rst.BOF Or Rst.EOF Then
        MsgBox "查找到最后了,没有匹配记录"
        Rst.MoveLast
    End If
End Sub

Private Sub DataGrid1_Click()
Me.txtName = DataGrid1.Columns(1).Text
End Sub

Private Sub Form_Load()
    Con.Open ("Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=" & App.Path & "/mir3.mdb")
    Rst.CursorLocation = adUseClient
    '必须制定是客户端游标 否则升降顺序不好用
    Rst.Open ("select * from Monster "), Con, adOpenKeyset, adLockPessimistic
    Set DataGrid1.DataSource = Rst
    Label11.Caption = "当前记录为:" & Rst.RecordCount & "条"
End Sub

Public Function FilterField(rstTemp As ADODB.Recordset, _
                            strField As String, _
                            strFilter As String) As ADODB.Recordset
    ' 在指定的记录集对象上设置筛选操作并打开一个新的记录集对象。
    rstTemp.Filter = strField & " = '" & strFilter & "'"
    Set FilterField = rstTemp
End Function

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值