这个窗体比较简单,以下是代码展示:
添加和删除用户:
Private Sub cboUserLevel_Click()
Dim txtsql As String
Dim msgtext As String
Dim mrc As ADODB.Recordset
txtsql = "select * from user_info where level= '" & Trim(cboUserLevel.Text) & "'"
Set mrc = ExecuteSQL(txtsql, msgtext)
If mrc.EOF Then
MsgBox "没有内容!", 0, "温馨提示"
Else
With MSHFlexGrid1
.Rows = 1
.CellAlignment = 4
.ColAlignment = 4
.TextMatrix(0, 0) = "用户名"
.TextMatrix(0, 1) = "姓名"
.TextMatrix(0, 2) = "开户人"
End With
Do While Not mrc.EOF
With MSHFlexGrid1
.Rows = .Rows + 1
.CellAlignment = 4
.TextMatrix(.Rows - 1, 0) = mrc.fields(0)
.TextMatrix(.Rows - 1, 1) = mrc.fields(2)
.TextMatrix(.Rows - 1, 2) = mrc.fields(3)
mrc.MoveNext
End With
Loop
mrc.Close
End If
End Sub
Private Sub cmdAdd_Click()
Me.Hide
Adduser.Show
End Sub
Private Sub cmdBack_Click()
Me.Hide
End Sub
Private Sub cmdDelete_Click()
Dim txtsql As String
Dim msgtext As String
Dim mrc As ADODB.Recordset
MSHFlexGrid1.SelectionMode = flexSelectionByRow '单机的时候选择整行
MSHFlexGrid1.FocusRect = flexFocusNone '在当前单元的周围画一个焦点框
MSHFlexGrid1.HighLight = flexHighlightWithFocus '该值决定了所选定的单元是否突出显示
With MSHFlexGrid1
If .RowSel = 0 Then
MsgBox "请选择数据!", 0 + 48, "提示"
Exit Sub
Else
If .RowSel > 0 Then
If Trim(MSHFlexGrid1.TextMatrix(.RowSel, 0)) = UserName Then
MsgBox "该用户正在登陆,不能删除!", 0 + 48, "温馨提示"
Exit Sub
Else
txtsql = "delete from user_info where userid= '" & Trim(MSHFlexGrid1.TextMatrix(.RowSel, 0)) & "'"
Set mrc = ExecuteSQL(txtsql, msgtext)
MSHFlexGrid1.RemoveItem MSHFlexGrid1.Row
End If
End If
End If
End With
End Sub
Private Sub Form_Load()
cboUserLevel.AddItem "一般用户"
cboUserLevel.AddItem "操作员"
cboUserLevel.AddItem "管理员"
End Sub
添加用户窗体:
Private Sub cmdDelete_Click()
txtUser.Text = ""
txtPassword.Text = ""
cboUserLevel.Text = ""
txtName.Text = ""
Text4.Text = ""
End Sub
Private Sub cmdOk_Click()
Dim txtsql As String
Dim msgtext As String
Dim mrc As ADODB.Recordset
txtsql = "select * from user_info"
Set mrc = ExecuteSQL(txtsql, msgtext)
If Trim(txtUser.Text) = "" Then
MsgBox "请填写用户名", 0 + 48, "温馨提示"
Else
If Trim(txtName.Text) = "" Then
MsgBox "请填写姓名", 0 + 48, "温馨提示"
Else
If Trim(txtPassword.Text) = "" Then
MsgBox "请输入密码", 0 + 48, "温馨提示"
End If
End If
End If
If Trim(Text4.Text) <> Trim(txtPassword.Text) Then
MsgBox "密码不一致,请重新输入", 0 + 48, "温馨提示"
Exit Sub
Text4.SetFocus
Text4.Text = ""
End If
If mrc.EOF = True Then
MsgBox "有相同记录,请重新输入!", 0 + 48, "提示"
txtUser.SetFocus
txtUser.Text = ""
txtPassword.Text = ""
cboUserLevel.Text = ""
txtName.Text = ""
Text4.Text = ""
mrc.Close
End If
If Trim(txtUser.Text) = mrc.fields(0) Then
MsgBox "已经有相同用户了,不能添加重复用户", 0 + 46, "提示"
txtUser.Text = ""
txtUser.SetFocus
Exit Sub
Else
mrc.AddNew
mrc.fields(0) = Trim(txtUser.Text)
mrc.fields(1) = Trim(txtPassword.Text)
mrc.fields(2) = Trim(cboUserLevel.Text)
mrc.fields(3) = Trim(txtName.Text)
mrc.fields(4) = "开户人"
mrc.Update
mrc.Close
MsgBox "添加用户成功", 0 + 48, "提示"
Unload Me
End If
End Sub
Private Sub Form_Load()
cboUserLevel.AddItem "一般用户"
cboUserLevel.AddItem "操作员"
cboUserLevel.AddItem "管理员"
End Sub
Private Sub txtUser_KeyPress(KeyAscii As Integer)
Dim ctemp As String
ctemp = "#$%^&*" '禁止输入的字符
If InStr(1, ctemp, Chr(KeyAscii)) <> 0 Then
KeyAscii = 0
End If
End Sub