当前位置: 源码素材网 » VB教程 » 详情页

VB 6.0 把控件作为字段、操作符和内容进行查询

  •   时间:2020-03-26
  • 概述:字段 数据库查询

VB 6.0 把控件作为字段、操作符和内容进行查询的例子,本代码可在VB6.0环境下编译成功,细看一下,还是挺简单的代码:

VB 6.0 把控件作为字段、操作符和内容进行查询

VERSION 5.00
Object = "{67397AA1-7FB1-11D0-B148-00A0C922E820}#6.0#0"; "MSADODC.OCX"
Object = "{CDE57A40-8B86-11D0-B3C6-00A0C90AEA82}#1.0#0"; "MSDATGRD.OCX"
Begin VB.Form Form1 
   Caption         =   "控件作为字段、操作符和内容进行查询"
   ClientHeight    =   2970
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   6240
   LinkTopic       =   "Form1"
   LockControls    =   -1  'True
   MaxButton       =   0   'False
   ScaleHeight     =   2970
   ScaleWidth      =   6240
   StartUpPosition =   2  '屏幕中心
   Begin VB.Frame Frame2 
      Caption         =   "查询结果"
      Height          =   2325
      Left            =   0
      TabIndex        =   6
      Top             =   615
      Width           =   6225
      Begin MSAdodcLib.Adodc Adodc1 
         Height          =   330
         Left            =   975
         Top             =   1785
         Visible         =   0   'False
         Width           =   1575
         _ExtentX        =   2778
         _ExtentY        =   582
         ConnectMode     =   0
         CursorLocation  =   3
         IsolationLevel  =   -1
         ConnectionTimeout=   15
         CommandTimeout  =   30
         CursorType      =   3
         LockType        =   3
         CommandType     =   8
         CursorOptions   =   0
         CacheSize       =   50
         MaxRecords      =   0
         BOFAction       =   0
         EOFAction       =   0
         ConnectStringType=   1
         Appearance      =   1
         BackColor       =   -2147483643
         ForeColor       =   -2147483640
         Orientation     =   0
         Enabled         =   -1
         Connect         =   ""
         OLEDBString     =   ""
         OLEDBFile       =   ""
         DataSourceName  =   ""
         OtherAttributes =   ""
         UserName        =   ""
         Password        =   ""
         RecordSource    =   ""
         Caption         =   "Adodc1"
         BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
            Name            =   "宋体"
            Size            =   9
            Charset         =   134
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         _Version        =   393216
      End
      Begin MSDataGridLib.DataGrid DataGrid1 
         Height          =   2040
         Left            =   90
         TabIndex        =   7
         Top             =   195
         Width           =   6045
         _ExtentX        =   10663
         _ExtentY        =   3598
         _Version        =   393216
         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
         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
   End
   Begin VB.Frame Frame1 
      Height          =   645
      Left            =   0
      TabIndex        =   0
      Top             =   -45
      Width           =   6225
      Begin VB.CommandButton Cmd_Refresh 
         Caption         =   "刷新"
         Height          =   315
         Left            =   5205
         TabIndex        =   5
         Top             =   180
         Width           =   850
      End
      Begin VB.CommandButton Cmd_Find 
         Caption         =   "查询"
         Height          =   315
         Left            =   4290
         TabIndex        =   4
         Top             =   180
         Width           =   850
      End
      Begin VB.TextBox Txt_Key 
         Height          =   300
         Left            =   2730
         TabIndex        =   3
         Top             =   195
         Width           =   1485
      End
      Begin VB.ComboBox Cbx_Oper 
         Height          =   300
         Left            =   1635
         TabIndex        =   2
         Top             =   195
         Width           =   1095
      End
      Begin VB.ComboBox Cbx_Field 
         Height          =   300
         Left            =   105
         TabIndex        =   1
         Top             =   195
         Width           =   1530
      End
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim fld
Dim MyField
Dim sql As String
Dim MyRs As New ADODB.Recordset
'QQ223857666勾月
Private Sub Cmd_Find_Click()
'On Error GoTo MYERR
   Adodc1.RecordSource = "select * from tb_stu"
   MyField = Adodc1.Recordset.Fields.Item(Cbx_Field.ListIndex).Name
   Select Case Adodc1.Recordset.Fields(Cbx_Field.ListIndex).Type
    Case 200  '字符数据
      If Cbx_Oper.Text = "like" Then
         sql = "select * from tb_stu where " & Cbx_Field & " like+ '%'+'" + Txt_Key + "'+'%'"
      Else
         sql = "select * from tb_stu where " & Cbx_Field & Cbx_Oper & "'" + Txt_Key + "'"
      End If
    Case 135    '日期数据
      If Cbx_Oper.Text = "like" Then
         MsgBox "日期型数据不能选用“Like”作为运算符!", , "提示窗口"
         Cbx_Oper.ListIndex = 1
      End If
      If IsDate(Txt_Key) = False Then
         MsgBox "请输入正确的日期!", , "提示窗口"
         rs.Close
         Exit Sub
      End If
      sql = "select * from tb_stu where " & Cbx_Field & Cbx_Oper & "'" + Txt_Key + "'"
    Case 6     '货币数据
      If IsNumeric(Txt_Key) = False Then
         MsgBox "请输入正确的数据!", , "提示窗口"
         rs.Close
         Exit Sub
      End If
      If Cbx_Oper.Text = "like" Then
         MsgBox "货币数据不能选用“Like”作为运算符!", , "提示窗口"
         Cbx_Oper.ListIndex = 1
      End If
      sql = "select * from tb_stu where " & Cbx_Field & Txt_Key
   Case 131     '数字数据
      If Cbx_Oper.Text = "like" Then
         MsgBox "数字数据不能选用“Like”作为运算符!", , "提示窗口"
         Cbx_Oper.ListIndex = 1
      End If
      If IsNumeric(Txt_Key) = False Then
         MsgBox "请输入正确的数据!", , "提示窗口"
         rs.Close
         Exit Sub
      End If
      sql = "select * from tb_stu where " & Cbx_Field & Cbx_Oper & Txt_Key
   Case Else
      If Cbx_Oper.Text = "like" Then
         sql = "select * from tb_stu where " & Cbx_Field & " like+ '%'+'" + Txt_Key + "'+'%'"
      Else
         sql = "select * from tb_stu where " & Cbx_Field & Cbx_Oper & "'" + Txt_Key + "'"
      End If
  End Select
  Adodc1.RecordSource = sql
  Adodc1.Refresh
  Set DataGrid1.DataSource = Adodc1
  Exit Sub
MYERR:
   MsgBox Err.Description, vbInformation, "信息提示"
End Sub
Private Sub Cmd_Refresh_Click()
Adodc1.RecordSource = "select * from tb_stu"
Adodc1.Refresh
Set DataGrid1.DataSource = Adodc1
End Sub
Private Sub Form_Load()
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db_ExpStu.mdb;Persist Security Info=False"
Adodc1.RecordSource = "select * from tb_stu"
Adodc1.Refresh
Set DataGrid1.DataSource = Adodc1
  Call AddData
  Cbx_Oper.AddItem ("like")
  Cbx_Oper.AddItem (">")
  Cbx_Oper.AddItem ("=")
  Cbx_Oper.AddItem (">=")
  Cbx_Oper.AddItem ("<")
  Cbx_Oper.AddItem ("<=")
  Cbx_Oper.AddItem ("<>")
  Cbx_Oper.ListIndex = 0
End Sub
Function AddData()
   Adodc1.RecordSource = "select * from  tb_stu"
   Adodc1.Refresh
   If Adodc1.Recordset.RecordCount > 0 Then
      For i = 0 To Adodc1.Recordset.Fields.Count - 1
         Cbx_Field.AddItem Form1.DataGrid1.Columns(i).Caption
      Next i
   End If
   Cbx_Field.ListIndex = 0
End Function
Private Sub Txt_Key_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then Cmd_Find.SetFocus
End Sub

完整的可编译的源码,请点此下载:VB以控件为字段、操作符和内容进行数据库查询的例子

    相关内容:

    相关声明:

      若“VB 6.0 把控件作为字段、操作符和内容进行查询”有损您的权益,请告之我们删除内容。
      部分文章来源于网络,版权归原作者所有。