📄 form1.frm
字号:
VERSION 5.00
Begin VB.Form Form1
Caption = "使用Command对象"
ClientHeight = 3705
ClientLeft = 60
ClientTop = 450
ClientWidth = 6480
LinkTopic = "Form1"
ScaleHeight = 3705
ScaleWidth = 6480
StartUpPosition = 2 '屏幕中心
Begin VB.TextBox TxtName
Height = 375
Left = 2400
TabIndex = 6
Top = 1200
Width = 2175
End
Begin VB.CommandButton CmdLast
Caption = "尾记录"
Height = 495
Left = 3840
TabIndex = 5
Top = 3000
Width = 1215
End
Begin VB.CommandButton CmdNext
Caption = "下记录"
Height = 495
Left = 2640
TabIndex = 4
Top = 3000
Width = 1215
End
Begin VB.CommandButton CmdPrevious
Caption = "上记录"
Height = 495
Left = 1440
TabIndex = 3
Top = 3000
Width = 1215
End
Begin VB.CommandButton CmdFirst
Caption = "首记录"
Height = 495
Left = 240
TabIndex = 2
Top = 3000
Width = 1215
End
Begin VB.TextBox TxtStudentno
Height = 375
Left = 2400
TabIndex = 1
Top = 600
Width = 2175
End
Begin VB.CommandButton CmdExit
Caption = "退 出"
Height = 495
Left = 5040
TabIndex = 0
Top = 3000
Width = 1215
End
Begin VB.Label LblName
Caption = "学生姓名:"
Height = 375
Left = 1080
TabIndex = 8
Top = 1320
Width = 1095
End
Begin VB.Label LblStudentno
Caption = "学生学号:"
Height = 375
Left = 1080
TabIndex = 7
Top = 720
Width = 1095
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 rs As New ADODB.Recordset
'定义一个Command对象
Dim cmd As New ADODB.Command
Dim connstr As String
Dim sql As String
'单击退出按钮后的执行代码
Private Sub CmdExit_Click()
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
Unload Me
End Sub
'单击首记录按钮后的执行代码
Private Sub CmdFirst_Click()
rs.MoveFirst
Call ShowData
End Sub
'单击尾记录按钮后的执行代码
Private Sub CmdLast_Click()
rs.MoveLast
Call ShowData
End Sub
'单击下记录按钮后的执行代码
Private Sub CmdNext_Click()
rs.MoveNext
If rs.EOF Then
rs.MoveFirst
End If
Call ShowData
End Sub
'单击上记录按钮后的执行代码
Private Sub CmdPrevious_Click()
rs.MovePrevious
If rs.BOF Then
rs.MoveLast
End If
Call ShowData
End Sub
'启动表单时加载的内容
Private Sub Form_Load()
connstr = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Initial Catalog=db_student;Data Source = mynetserver"
conn.ConnectionString = connstr
conn.Open connstr
sql = "Select * From T_STUDENT"
'设置Command对象使用的Connection对象
Set cmd.ActiveConnection = conn
'设置Command对象使用的SQL语句
cmd.CommandText = sql
rs.CursorLocation = adUseServer
'执行Command对象,结果返回给Recordset对象
Set rs = cmd.Execute
rs.MoveFirst
Call ShowData
End Sub
'显示数据的子过程
Sub ShowData()
TxtStudentno.Text = rs.Fields(0)
TxtName.Text = rs.Fields(1)
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -