VB6 & .accdb 数据库登录表单
VB6 & .accdb database Login Form
我想知道是否有人可以帮助我解决这个问题。我有一个登录表单,其中包含一个名为 cboUsername 的组合框,以及一个名为 txtPassword 的密码文本框。我有访问数据库的 Employee table 的表单,并用 .accdb 中 strEmpName 字段中的员工姓名填充组合框,我在检查密码时遇到问题 table字段名称为 strEmpPassword。我想做的是,如果密码匹配,则允许访问该软件。除此之外,我还有一个名为 strAccess 的字段,它允许用户访问软件的某些部分。 strAccess 将为 "Admin"、"User" 或 "Read Only"。谁能帮我解决问题?我的工作代码如下:
Option Explicit
Private Sub Form_Load()
Dim db_file As String
Dim statement As String
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
cboUsername.Text = "Select Username"
' Get the data.
db_file = App.Path
If Right$(db_file, 1) <> "\" Then db_file = db_file & "\"
db_file = db_file & "FluidFlo.accdb"
' Open a connection.
Set conn = New ADODB.Connection
conn.ConnectionString = _
"Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & db_file & ";" & "Persist Security Info=False"
conn.Open
' Select the data.
statement = "SELECT strEmpName FROM tblEmployees ORDER BY strEmpName"
' Get the records.
Set rs = conn.Execute(statement, , adCmdText)
' Load the results into the ComboBox
Do Until rs.EOF
cboUsername.AddItem rs!strEmpName
rs.MoveNext
Loop
' Close the recordset and connection.
rs.Close
conn.Close
End Sub
我希望我已经明确了我想要完成的事情,并且一如既往地提前感谢大家!
我有一个生成 Windows 用户名的函数
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Public Function LoginName() As Variant
Dim ret As Long
Dim s As String * 255
Dim pos As Long
ret = GetUserName(s, 255)
pos = InStr(s, vbNullChar)
If pos <= 1 Then
LoginName = Null
Else
LoginName = Left$(s, pos - 1)
End If
End Function
使用此功能,您可以直接询问密码,而无需在 ComboBox 中填写用户名。
使用其他功能
Public Function SqlStr(ByVal s As String) As String
'Input: s="" Returns: NULL
'Input: s="abc" Returns: 'abc'
'Input: s="x'y" Returns: 'x''y'
If s = "" Then
SqlStr = "NULL"
Else
SqlStr = "'" & Replace(s, "'", "''") & "'"
End If
End Function
您可以使用
检查密码
If DCount("*", "tblEmployees", _
"strEmpName = " & SqlStr(LoginName()) & " AND strPwd = " & SqlStr(txtPassword)) = 1 Then
...
Else
...
End If
顺便说一句,如果此代码位于带有表的 accdb 中或这些表链接到的 accdb 中,则无需创建连接和使用 ADODB。只需将此查询 SELECT strEmpName FROM tblEmployees ORDER BY strEmpName
放入 ComboBox 的 Row Source
属性。
注意:这是访问 VBA 代码。
更新
从您新发布的代码中,我看到您正在将员工 ID 与员工姓名 ([lngEmpID]=" & Me.cboUsername.Value
) 进行比较,这当然行不通。
确保 ComboBox 的 Row Source Type
是 Table/Query
。将此代码放在 ComboBox 的 Row Source
属性.
SELECT lngEmpID, strEmpName FROM tblEmployees ORDER BY strEmpName
与您的代码相比,这增加了 Id。将 Column Count
设置为 2
,将 Column Widths
设置为 0cm
。这会隐藏 ComboBox 中的 Id 列。现在,ComboBox 的值是 Id 而不是名称。
我想知道是否有人可以帮助我解决这个问题。我有一个登录表单,其中包含一个名为 cboUsername 的组合框,以及一个名为 txtPassword 的密码文本框。我有访问数据库的 Employee table 的表单,并用 .accdb 中 strEmpName 字段中的员工姓名填充组合框,我在检查密码时遇到问题 table字段名称为 strEmpPassword。我想做的是,如果密码匹配,则允许访问该软件。除此之外,我还有一个名为 strAccess 的字段,它允许用户访问软件的某些部分。 strAccess 将为 "Admin"、"User" 或 "Read Only"。谁能帮我解决问题?我的工作代码如下:
Option Explicit
Private Sub Form_Load()
Dim db_file As String
Dim statement As String
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
cboUsername.Text = "Select Username"
' Get the data.
db_file = App.Path
If Right$(db_file, 1) <> "\" Then db_file = db_file & "\"
db_file = db_file & "FluidFlo.accdb"
' Open a connection.
Set conn = New ADODB.Connection
conn.ConnectionString = _
"Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & db_file & ";" & "Persist Security Info=False"
conn.Open
' Select the data.
statement = "SELECT strEmpName FROM tblEmployees ORDER BY strEmpName"
' Get the records.
Set rs = conn.Execute(statement, , adCmdText)
' Load the results into the ComboBox
Do Until rs.EOF
cboUsername.AddItem rs!strEmpName
rs.MoveNext
Loop
' Close the recordset and connection.
rs.Close
conn.Close
End Sub
我希望我已经明确了我想要完成的事情,并且一如既往地提前感谢大家!
我有一个生成 Windows 用户名的函数
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Public Function LoginName() As Variant
Dim ret As Long
Dim s As String * 255
Dim pos As Long
ret = GetUserName(s, 255)
pos = InStr(s, vbNullChar)
If pos <= 1 Then
LoginName = Null
Else
LoginName = Left$(s, pos - 1)
End If
End Function
使用此功能,您可以直接询问密码,而无需在 ComboBox 中填写用户名。
使用其他功能
Public Function SqlStr(ByVal s As String) As String
'Input: s="" Returns: NULL
'Input: s="abc" Returns: 'abc'
'Input: s="x'y" Returns: 'x''y'
If s = "" Then
SqlStr = "NULL"
Else
SqlStr = "'" & Replace(s, "'", "''") & "'"
End If
End Function
您可以使用
检查密码If DCount("*", "tblEmployees", _
"strEmpName = " & SqlStr(LoginName()) & " AND strPwd = " & SqlStr(txtPassword)) = 1 Then
...
Else
...
End If
顺便说一句,如果此代码位于带有表的 accdb 中或这些表链接到的 accdb 中,则无需创建连接和使用 ADODB。只需将此查询 SELECT strEmpName FROM tblEmployees ORDER BY strEmpName
放入 ComboBox 的 Row Source
属性。
注意:这是访问 VBA 代码。
更新
从您新发布的代码中,我看到您正在将员工 ID 与员工姓名 ([lngEmpID]=" & Me.cboUsername.Value
) 进行比较,这当然行不通。
确保 ComboBox 的 Row Source Type
是 Table/Query
。将此代码放在 ComboBox 的 Row Source
属性.
SELECT lngEmpID, strEmpName FROM tblEmployees ORDER BY strEmpName
与您的代码相比,这增加了 Id。将 Column Count
设置为 2
,将 Column Widths
设置为 0cm
。这会隐藏 ComboBox 中的 Id 列。现在,ComboBox 的值是 Id 而不是名称。