📄 form1.frm
字号:
VERSION 5.00
Begin VB.Form Form1
Caption = "课程表维护窗口"
ClientHeight = 2085
ClientLeft = 60
ClientTop = 450
ClientWidth = 7620
LinkTopic = "Form1"
ScaleHeight = 2085
ScaleWidth = 7620
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command9
Caption = "取消"
Height = 375
Left = 6840
TabIndex = 16
Top = 1560
Width = 615
End
Begin VB.CommandButton Command8
Caption = "确定"
Height = 375
Left = 6000
TabIndex = 15
Top = 1560
Width = 735
End
Begin VB.CommandButton Command7
Caption = "修改"
Height = 375
Left = 5160
TabIndex = 14
Top = 1560
Width = 735
End
Begin VB.CommandButton Command6
Caption = "添加"
Height = 375
Left = 4320
TabIndex = 13
Top = 1560
Width = 735
End
Begin VB.CommandButton Command5
Caption = "删除"
Height = 375
Left = 3480
TabIndex = 12
Top = 1560
Width = 735
End
Begin VB.CommandButton Command4
Caption = "末记录"
Height = 375
Left = 2640
TabIndex = 11
Top = 1560
Width = 735
End
Begin VB.CommandButton Command3
Caption = "后移"
Height = 375
Left = 1800
TabIndex = 10
Top = 1560
Width = 735
End
Begin VB.CommandButton Command2
Caption = "前移"
Height = 375
Left = 960
TabIndex = 9
Top = 1560
Width = 735
End
Begin VB.CommandButton Command1
Caption = "首记录"
Height = 375
Left = 120
TabIndex = 8
Top = 1560
Width = 735
End
Begin VB.TextBox Text4
Height = 375
Left = 4560
TabIndex = 7
Text = "Text4"
Top = 960
Width = 2055
End
Begin VB.TextBox Text3
Height = 375
Left = 1680
TabIndex = 5
Text = "Text3"
Top = 960
Width = 1335
End
Begin VB.TextBox Text2
Height = 375
Left = 4560
TabIndex = 3
Text = "Text2"
Top = 360
Width = 2295
End
Begin VB.TextBox Text1
Height = 375
Left = 1680
TabIndex = 1
Text = "Text1"
Top = 360
Width = 1335
End
Begin VB.Data Data1
Caption = "Data1"
Connect = "Access"
DatabaseName = "C:\Documents and Settings\tongaihong\桌面\VB数据库程序与数据\09\例9-18\教学.mdb"
DefaultCursorType= 0 '缺省游标
DefaultType = 2 '使用 ODBC
Exclusive = 0 'False
Height = 375
Left = 2520
Options = 0
ReadOnly = 0 'False
RecordsetType = 1 'Dynaset
RecordSource = "课程表"
Top = 2280
Width = 2415
End
Begin VB.Label Label4
Caption = "讲课教师号:"
Height = 375
Left = 3360
TabIndex = 6
Top = 960
Width = 1215
End
Begin VB.Label Label3
Caption = "课时数:"
Height = 255
Left = 840
TabIndex = 4
Top = 960
Width = 855
End
Begin VB.Label Label2
Caption = "课程名:"
Height = 375
Left = 3720
TabIndex = 2
Top = 360
Width = 735
End
Begin VB.Label Label1
Caption = "课程号:"
Height = 255
Left = 840
TabIndex = 0
Top = 360
Width = 735
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 SqlString As String '查询字符串
Dim Connstring As String '连接字符串
Dim RS1 As New ADODB.Recordset '记录集变量
Dim bm '定义一个变量用来存放添加记录时原来记录的位置
Dim tj As Boolean
Private Sub Command1_Click() '移到首记录
RS1.MoveFirst
Command1.Enabled = False
Command2.Enabled = False
Command3.Enabled = True
Command4.Enabled = True
Call DispValue '显示当前记录的值
End Sub
Private Sub Command2_Click() '前移
RS1.MovePrevious
If RS1.BOF Then
RS1.MoveFirst
Command1.Enabled = False
Command2.Enabled = False
End If
Command3.Enabled = True
Command4.Enabled = True
Call DispValue '显示当前记录的值
End Sub
Private Sub Command3_Click() '后移
RS1.MoveNext
If RS1.EOF Then
RS1.MoveLast
Command3.Enabled = False
Command4.Enabled = False
End If
Command1.Enabled = True
Command2.Enabled = True
Call DispValue '显示当前记录的值
End Sub
Private Sub Command4_Click() '移到末记录
RS1.MoveLast
Command3.Enabled = False
Command4.Enabled = False
Command1.Enabled = True
Command2.Enabled = True
Call DispValue '显示当前记录的值
End Sub
Private Sub Command5_Click() '删除记录
If (MsgBox("真的要删除该记录吗?", vbQuestion + vbYesNo, "删除提示") = vbYes) Then
RS1.Delete '删除
RS1.MoveNext
If RS1.EOF Then '如果删除的是末记录
RS1.MoveLast
End If
End If
Call DispValue '显示当前记录的值
End Sub
Private Sub Command6_Click() '添加记录
bm = RS1.Bookmark '用书签记下当前记录位置
RS1.AddNew '调用AddNew方法增加新记录
Call EditStatus '置按钮和文本框状态
Call EmptyText '清空文本框
tj = True '是添加新记录
End Sub
Private Sub Command7_Click() '修改记录
Call EditStatus '置按钮和文本框状态
tj = False '不是添加记录,是修改记录
End Sub
Private Sub Command8_Click() '确定操作
Call SetValue '给各字段赋值
RS1.Update '调用Update方法保存
Call OkStatus '置按钮和文本框状态
MsgBox ("保存成功") '给出保存成功提示
End Sub
Private Sub Command9_Click() '取消操作
If (MsgBox("真的要取消修改吗?", vbQuestion + vbYesNo)) = vbYes Then
RS1.CancelUpdate
If tj Then '如果是增加新记录
RS1.Bookmark = bm '把记录指针移动到原来的位置
End If
Call DispValue '显示当前记录的值
Call OkStatus '置按钮和文本框状态
End If
End Sub
Private Sub Form_Load()
Connstring = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source= " & App.Path & "\教学.mdb;Persist Security Info=False"
'设置连接字符串
With Conn
.ConnectionString = Connstring '给ConnectionString属性赋值
.ConnectionTimeout = 10
.Open '给连接的ConnectionString属性赋值,然后使用求带参数的Open方法打开连接
End With
MsgBox ("连接成功")
RS1.CursorType = adOpenDynamic
RS1.CursorLocation = adUseClient
RS1.LockType = adLockOptimistic
RS1.Open "课程表", Conn, 3, 3
If RS1.EOF And RS1.BOF Then '如果表为空
MsgBox "表为空,请添加记录"
Else
Call DispValue '显示当前记录的值
End If
Call TextDisable '使文本框不可用
Command8.Enabled = False
Command9.Enabled = False
End Sub
Private Sub DispValue()
Text1.Text = RS1.Fields("课程号")
Text2.Text = RS1.Fields("课程名")
Text3.Text = RS1.Fields("课时数")
Text4.Text = RS1.Fields("教师编号")
End Sub
Private Sub SetValue()
RS1.Fields("课程号") = Text1.Text
RS1.Fields("课程名") = Text2.Text
RS1.Fields("课时数") = Text3.Text
RS1.Fields("教师编号") = Text4.Text
End Sub
Private Sub TextEnable() '使所有的文本框可用
Text1.Enabled = True
Text2.Enabled = True
Text3.Enabled = True
Text4.Enabled = True
End Sub
Private Sub TextDisable() '使所有的文本框不可用
Text1.Enabled = False
Text2.Enabled = False
Text3.Enabled = False
Text4.Enabled = False
End Sub
Private Sub EditStatus() '设置在编辑或添加记录时的各控件状态
Command1.Enabled = False
Command2.Enabled = False
Command3.Enabled = False
Command4.Enabled = False
Command5.Enabled = False
Command6.Enabled = False
Command7.Enabled = False
Command8.Enabled = True
Command9.Enabled = True
Call TextEnable
End Sub
Private Sub EmptyText() '清空文本框
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
End Sub
Private Sub OkStatus() '设置不在编辑或添加记录时的各控件状态
Command1.Enabled = True
Command2.Enabled = True
Command3.Enabled = True
Command4.Enabled = True
Command5.Enabled = True
Command6.Enabled = True
Command7.Enabled = True
Command8.Enabled = False
Command9.Enabled = False
Call TextDisable
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -