第一步:获取本计算机用户名
Private Sub Form_Load()重点内容
Dim sBuffer As String '缓冲器
Dim lSize As Long
sBuffer = Space$(255) '缓冲器容量255Byte,sBuffer为255个空格字符串
lSize = Len(sBuffer) 'sBuffer串的长度
Call GetUserName(sBuffer, lSize) '读取用户名到sBuffer中
If lSize > 0 Then
txtUserName.Text = ""
Else
第二步:判断文本框
Private Sub cmdOK_Click()
Dim txtSQL As String
Dim mrc As ADODB.Recordset
Dim MsgText As String
UserName = ""
If Trim(txtUserName.Text = "") Then
MsgBox "没有这个用户,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
txtUserName.SetFocus
Else
txtSQL = "select*from user_Info where user_ID='" & txtUserName.Text & "'"
Set mrc = ExecuteSQL(txtSQL, MsgText)
第三步:ExecuteSQL函数执行
Public Function ExecuteSQL(ByVal SQL As String, MsgString As String) As ADODB.Recordset
'execute SQL and return recordset
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String
On Error GoTo ExecuteSQL_Error
sTokens = Split(SQL)
Set cnn = New ADODB.Connection
cnn.Open ConnectString
第四步:打开ConnectString,连接数据库
Public Function ConnectString() As String '以文件DSN标记,访问ODBC数据源
ConnectString = "FileDSN=student.dsn;UID=sa;PWD=123456" '连接文件dsn
End Function
第五步:判断增删改查
If InStr("INSERT,DELETE,UPDATE", UCase$(sTokens(0))) Then
cnn.Execute SQL
MsgString = sTokens(0) & " query successful"
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(SQL), cnn, adOpenKeyset, adLockOptimistic
Set ExecuteSQL = rst
MsgString = "查询到" & rst.RecordCount & "条记录"
End If
ExecuteSQL_Exit:
Set rst = Nothing
Set cnn = Nothing
Exit Function
第六步:数据库查询后的判断
If mrc.EOF Then
MsgBox "没有这个用户,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
txtUserName.SetFocus
Else
If Trim(mrc.Fields(1)) = Trim(txtPassword.Text) Then
OK = True
mrc.Close
Me.Hide
UserName = Trim(txtUserName.Text)
Else
MsgBox "输入密码不正确,请重新输入!", vbOKOnly + vbExclamation, "警告"
txtPassword.SetFocus
txtPassword.Text = ""
End If
第七步:登录成功