form1.frm

来自「vb数据库编程资料」· FRM 代码 · 共 106 行

FRM
106
字号
VERSION 5.00
Object = "{5E9E78A0-531B-11CF-91F6-C2863C385E30}#1.0#0"; "MSFLXGRD.OCX"
Begin VB.Form Form1 
   Caption         =   "Form1"
   ClientHeight    =   2730
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   5850
   LinkTopic       =   "Form1"
   ScaleHeight     =   2730
   ScaleWidth      =   5850
   StartUpPosition =   3  '窗口缺省
   Begin VB.CommandButton Command3 
      Caption         =   "关闭"
      Enabled         =   0   'False
      Height          =   375
      Left            =   3840
      TabIndex        =   3
      Top             =   2160
      Width           =   1335
   End
   Begin MSFlexGridLib.MSFlexGrid MSFlexGrid1 
      Height          =   1695
      Left            =   240
      TabIndex        =   2
      Top             =   120
      Width           =   5535
      _ExtentX        =   9763
      _ExtentY        =   2990
      _Version        =   393216
   End
   Begin VB.CommandButton Command2 
      Caption         =   "显示"
      Enabled         =   0   'False
      Height          =   375
      Left            =   2160
      TabIndex        =   1
      Top             =   2160
      Width           =   1335
   End
   Begin VB.CommandButton Command1 
      Caption         =   "连接"
      Height          =   375
      Left            =   480
      TabIndex        =   0
      Top             =   2160
      Width           =   1215
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim Conn As New ADODB.Connection  '连接变量
Dim ExeString As String
Dim Connstring As String           '连接字符串
Dim RS1 As New ADODB.Recordset     '记录集变量
Private Sub Command1_Click()
  Connstring = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & App.Path & "\教学.mdb"
  '设置连接字符串
  With Conn
     .ConnectionString = Connstring '给ConnectionString属性赋值
     .ConnectionTimeout = 10
     .Open '给连接的ConnectionString属性赋值,然后使用求带参数的Open方法打开连接
  End With
  MsgBox ("连接成功")
  Command1.Enabled = False  '连接按钮不可用
  Command2.Enabled = True   '显示数据可用
End Sub
Private Sub Command2_Click()
 RS1.Open "学生表", Conn, adOpenForwardOnly, , adCmdTable '打开学生表,产生记录集
 Call ShowData(RS1, MSFlexGrid1) '调用过程显示记录集中的数据
 Command2.Enabled = False  '显示按钮不可用
 Command3.Enabled = True  '关闭按钮可用
End Sub

Private Sub ShowData(Rs As ADODB.Recordset, Dgrid As MSFlexGrid)
     '该过程用来在Dgrid网格中显示记录集RS中的内容
     Dim RowNum As Integer
     RowNum = 1                    '代表MsFlexGrid控件的行数
     Dgrid.Rows = RowNum              '设置MsFlexGrid控件的行数
     Dgrid.Cols = Rs.Fields.Count    '设置MsFlexGrid控件的列数
     For j = 0 To Rs.Fields.Count - 1  '遍列所有列
        Dgrid.TextMatrix(0, j) = Rs.Fields(j).Name  '把列名显示在设置MsFlexGrid控件的首行
     Next j
     Do While Not Rs.EOF  '如果不是记录集的结尾
        RowNum = RowNum + 1 '行数加1
        Dgrid.Rows = RowNum   '设置MsFlexGrid控件的行数
        For j = 0 To Rs.Fields.Count - 1  '遍列所有列
          If Not IsNull(Rs.Fields(j).Value) Then  '如果列中的数据不为空
             Dgrid.TextMatrix(RowNum - 1, j) = Rs.Fields(j).Value  '把列的数据显示在MsFlexGrid的相应网格中
          End If
        Next j
        Rs.MoveNext  '记录指针下移
     Loop
     Command2.Enabled = False
End Sub

Private Sub Command3_Click()
   RS1.Close    '关闭记录集
   Conn.Close    '关闭连接
   End           '结束程序运行
End Sub

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?