Option Explicit
'窗口始终在最前
Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Public Const HWND_TOPMOST& = -1
' 将窗口置于列表顶部,并位于任何最顶部窗口的前面
Public Const SWP_NOSIZE& = &H1
' 保持窗口大小
Public Const SWP_NOMOVE& = &H2
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strSQL As String
Dim LoginSuccess(0 To 5)
Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 0
If Me.DataCombo1.Text = "" Or Text1.Text = "" Then '当用户名或密码未填时,提示错误
MsgBox "Login Error!", vbInformation, Me.Caption
Text1.SetFocus
Exit Sub
End If
rs.Close
'查询出与用户名相同的记录
strSQL = "SELECT * FROM User_Table WHERE UserID='" & Me.DataCombo1.Text & "'"
rs.Open strSQL, cnn, adOpenStatic, adLockReadOnly
If Text1.Text = rs.Fields("UserPWD").Value Then '如果密码与记录相同
Me.Hide
Form2.Show
LoginSuccess(0) = Me.DataCombo1.Text '用户名
LoginSuccess(1) = rs.Fields("UserLevel").Value '用户级别
LoginSuccess(2) = Format(Now, "YYYY-MM-DD HH:MM:SS") '登录时间
Else
MsgBox "Login Error!", vbInformation, Me.Caption
Text1.Text = ""
Text1.SetFocus
End If
Case 1
rs.Close
cnn.Close
Set cnn = Nothing
Set FormLogin = Nothing
End
End Select
End Sub
Private Sub DataCombo1_Change()
Text1.SetFocus
End Sub
Private Sub Form_Load()
'窗口位于最前,API
SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
'引用 Microsoft ActiveX Data Objects 2.8 Library
'连接Access数据库
Set cnn = New ADODB.Connection
cnn.Provider = "Microsoft.Jet.OLEDB.4.0"
Dim cnnFile As String
cnnFile = "Data Source=" & App.Path & "/data.mdb;Persist Security Info=False;Jet OLEDB:Database Password=123456789"
cnn.Open cnnFile
'查询用户表,并将用户名显示在DataCombo控件中
Set rs = New ADODB.Recordset
strSQL = "SELECT UserID FROM User_Table"
rs.Open strSQL, cnn, adOpenStatic, adLockReadOnly 'Open table "User"
Set Me.DataCombo1.DataSource = rs
Set Me.DataCombo1.RowSource = rs
Me.DataCombo1.ListField = rs.Fields("UserID").Name
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
rs.Close
cnn.Close
Set cnn = Nothing
Set FormLogin = Nothing
End
End Sub