📄 frmsysusermanage.frm
字号:
VERSION 5.00
Begin VB.Form SysUserManage
BorderStyle = 1 'Fixed Single
Caption = "管理员登录信息管理"
ClientHeight = 2145
ClientLeft = 45
ClientTop = 330
ClientWidth = 4635
LinkTopic = "Form1"
MaxButton = 0 'False
MDIChild = -1 'True
MinButton = 0 'False
ScaleHeight = 2145
ScaleWidth = 4635
Begin VB.TextBox txtNum
Height = 270
Left = 1559
MaxLength = 5
TabIndex = 0
Top = 450
Width = 2175
End
Begin VB.CommandButton cmdSave
Caption = "保存"
Height = 300
Left = 2302
TabIndex = 4
Top = 1365
Width = 735
End
Begin VB.CommandButton cmdExit
Cancel = -1 'True
Caption = "退出"
Height = 300
Left = 3037
TabIndex = 5
Top = 1365
Width = 735
End
Begin VB.CommandButton cmdDelete
Caption = "删除"
Height = 300
Left = 1582
TabIndex = 3
Top = 1365
Width = 735
End
Begin VB.CommandButton cmdAdd
Caption = "添加"
Height = 300
Left = 862
TabIndex = 2
Top = 1365
Width = 735
End
Begin VB.TextBox txtPWD
Height = 270
Left = 1544
MaxLength = 6
TabIndex = 1
Top = 870
Width = 2175
End
Begin VB.PictureBox picNavigation
AutoSize = -1 'True
BorderStyle = 0 'None
Height = 350
Left = 1149
ScaleHeight = 345
ScaleWidth = 2415
TabIndex = 10
Top = 1665
Width = 2410
Begin VB.CommandButton cmdMove
Height = 270
Index = 1
Left = 270
Picture = "frmSysUserManage.frx":0000
Style = 1 'Graphical
TabIndex = 7
Top = 0
Width = 300
End
Begin VB.CommandButton cmdMove
Height = 270
Index = 0
Left = -15
Picture = "frmSysUserManage.frx":0044
Style = 1 'Graphical
TabIndex = 6
Top = 0
Width = 300
End
Begin VB.CommandButton cmdMove
Height = 270
Index = 3
Left = 2010
Picture = "frmSysUserManage.frx":0091
Style = 1 'Graphical
TabIndex = 9
Top = 0
Width = 300
End
Begin VB.CommandButton cmdMove
Height = 270
Index = 2
Left = 1725
Picture = "frmSysUserManage.frx":00DD
Style = 1 'Graphical
TabIndex = 8
Top = 0
Width = 300
End
Begin VB.TextBox txtNews
Height = 270
Left = 555
Locked = -1 'True
TabIndex = 11
TabStop = 0 'False
Top = 0
Width = 1185
End
End
Begin VB.Label Label1
AutoSize = -1 'True
Caption = "用户名"
Height = 180
Left = 975
TabIndex = 13
Top = 480
Width = 540
End
Begin VB.Label Label2
AutoSize = -1 'True
Caption = "口 令"
Height = 180
Left = 960
TabIndex = 12
Top = 900
Width = 540
End
End
Attribute VB_Name = "SysUserManage"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim isAdding As Boolean '定义操作状态标志
Dim objAdmin As Recordset '用于保存系统用户数据表记录
Dim objCn As Connection '用于建立数据库联接
Private Sub cmdExit_Click()
Unload Me '关闭管理员登录信息管理窗体
End Sub
Private Sub Form_Load()
'建立数据库联接
Set objCn = New Connection '实例化联接对象
With objCn '建立数据库联接
.Provider = "SQLOLEDB"
.ConnectionString = "User ID=sa;PWD=123;Data Source=(local);" & _
"Initial Catalog=自测考试"
.Open
End With
'获取系统用户记录
Set objAdmin = New Recordset '实例化objAdmin对象
With objAdmin
Set .ActiveConnection = objCn
.CursorLocation = adUseClient '指定使用客户端游标
.CursorType = adOpenStatic '指定使用静态游标
.LockType = adLockOptimistic
.Open "SELECT * FROM 管理员" '获取系统用户信息
End With
cmdMove(0).Value = True '触发按钮单击事件,显示第一个记录
End Sub
Private Sub cmdMove_Click(Index As Integer)
With objAdmin
Select Case Index '切换当前记录
Case 0 '使第一个记录成为当前记录
If .RecordCount > 0 And Not .BOF Then .MoveFirst
Case 1 '使上一个记录成为当前记录
If .RecordCount > 0 And Not .BOF Then
.MovePrevious
If .BOF Then .MoveFirst
End If
Case 2 '使下一个记录成为当前记录
If .RecordCount > 0 And Not .EOF Then
.MoveNext
If .EOF Then .MoveLast
End If
Case 3 '使最后一个记录成为当前记录
If .RecordCount > 0 And Not .EOF Then .MoveLast
End Select
If .RecordCount < 1 Then
txtNews = "记录:无" '显示无记录提示
txtNum = ""
txtPwd = ""
Else
'显示当前记录数据
txtNum = .Fields("用户名")
txtPwd = .Fields("口令")
'显示当前记录编号和记录总数
txtNews = "记录:" & .AbsolutePosition & "/" & .RecordCount
End If
End With
If isAdding Then isAdding = False
End Sub
Private Sub cmdAdd_Click()
txtNews = "添加新记录"
txtNum = ""
txtPwd = ""
isAdding = True
txtNum.SetFocus
End Sub
Private Sub cmdDelete_Click()
'根据是否处于添加记录状态执行不同的操作
If isAdding Then
'退出添加记录状态,显示当前记录
isAdding = False
If objAdmin.RecordCount <= 0 Then
txtNews = "记录:无" '显示无记录提示
Else
'显示当前记录数据
txtNum = objAdmin.Fields("用户名")
txtPwd = objAdmin.Fields("口令")
'显示当前记录编号和记录总数
txtNews = "记录:" & objAdmin.AbsolutePosition & "/" & objAdmin.RecordCount
End If
Else
If objAdmin.RecordCount > 0 Then
If MsgBox("是否删除当前记录?", vbYesNo + vbQuestion, "系统用户管理") = vbYes Then
'执行删除当前记录操作
objAdmin.Delete
'显示下一记录数据
cmdMove(2).Value = True
Else
'显示当前记录数据
txtNum = objAdmin.Fields("用户名")
txtPwd = objAdmin.Fields("口令")
'显示当前记录编号和记录总数
txtNews = "记录:" & objAdmin.AbsolutePosition & "/" & objAdmin.RecordCount
End If
End If
End If
End Sub
Private Sub cmdSave_Click()
Dim objCopy As New Recordset
If Trim(txtNum) = "" Then
MsgBox "用户名不能为空!", vbCritical, "系统用户管理"
txtNum.SetFocus
txtNum = ""
ElseIf Trim(txtPwd) = "" Then
MsgBox "系统登录口令不能为空!", vbCritical, "系统用户管理"
txtPwd.SetFocus
txtPwd = ""
Else
Set objCopy = objAdmin.Clone
With objCopy
If .RecordCount > 0 Then
'检查用户名是否被使用
.MoveFirst
.Find "用户名='" & Trim(txtNum) & "'"
If (isAdding And Not .EOF) Or _
(Not isAdding And Not .EOF And _
.AbsolutePosition <> objAdmin.AbsolutePosition) Then
MsgBox "用户名:" & Trim(txtNum) & "已被使用,请使用其他用户名!", _
vbCritical, "系统用户管理"
txtNum.SetFocus
txtNum.SelStart = 0
txtNum.SelLength = Len(txtNum)
Exit Sub
Else
'保存或添加记录
If isAdding Then objAdmin.AddNew
objAdmin.Fields("用户名") = Trim(txtNum)
objAdmin.Fields("口令") = Trim(txtPwd)
objAdmin.Update
MsgBox "数据保存成功!", vbInformation, "系统用户管理"
isAdding = False
'显示当前记录编号和记录总数
txtNews = "记录:" & objAdmin.AbsolutePosition & "/" & objAdmin.RecordCount
End If
Else
'添加新记录
If isAdding Then
objAdmin.AddNew
objAdmin.Fields("用户名") = Trim(txtNum)
objAdmin.Fields("口令") = Trim(txtPwd)
objAdmin.Update
MsgBox "数据保存成功!", vbInformation, "系统用户管理"
isAdding = False
'显示当前记录编号和记录总数
txtNews = "记录:" & objAdmin.AbsolutePosition & "/" & objAdmin.RecordCount
End If
End If
End With
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
objCn.Close '关闭数据联接
Set objCn = Nothing '释放数据库联接
Set objAdmin = Nothing '释放记录集对象
End Sub
'限制用户名输入
Private Sub txtNum_KeyPress(KeyAscii As Integer)
'如果敲回车键,使口令输入框获得焦点
If KeyAscii = vbKeyReturn Then txtPwd.SetFocus
If Not (Chr(KeyAscii) Like "[0-9]" Or Chr(KeyAscii) Like "[a-z]" _
Or Chr(KeyAscii) Like "[A-Z]" Or KeyAscii = vbKeyBack) Then
KeyAscii = 0 '输入不是数字、英文字母或退格键,取消输入
End If
End Sub
'限制口令输入
Private Sub txtPwd_KeyPress(KeyAscii As Integer)
'如果敲回车键,使保存按钮获得焦点
If KeyAscii = vbKeyReturn Then cmdSave.SetFocus
If Not (Chr(KeyAscii) Like "[0-9]" Or Chr(KeyAscii) Like "[a-z]" _
Or Chr(KeyAscii) Like "[A-Z]" Or KeyAscii = vbKeyBack) Then
KeyAscii = 0 '输入不是数字、英文字母或退格键,取消输入
End If
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -