access VBA 凑select 语句

Private Sub Command0_Click()If Text4 = "" Then MsgBox "请填写用户名"ElseIf Text6 = "" Then MsgBox "请填写密码"End IfDim data As New ADODB.RecordsetDim sql As StringDim mm As Stringmm = Me.Text4.ValueDebug.Print Me.Text4.Value sql = "select * from 用户名 where 用户名 =" & Me.Text4.Value Debug.Print sql data.Open sql, CurrentProject.Connection, adOpenKeyset, adLockOptimistic If data.Fields("密码") = Me![Text6] Then DoCmd.OpenForm ("主界面") Else MsgBox "用户名或密码错误请从新输入" Me![Text6] = "" Me![Text6] = "" End IfEnd Sub这是全部的代码。运行之后提示说至少有一个变量未指定。求教还有就是 我希望把 sql这个变量凑成一个select语句,但是debugprint出来时select * from 用户名 where 用户名 =songmingfeng,明显songmingfeng那里少了一组单引号但是加上了以后后面的却成了备注。所以求各位大神帮助
2026年09月24日 07:23
有2个网友回答
网友(1):

按你的写法,用户名表中只有第一条记录是能用的,其他的都查不到。所以给你改了一下。
Private Sub Command0_Click()
If Text4 = "" Then
MsgBox "请填写用户名"
exit sub
ElseIf Text6 = "" Then
MsgBox "请填写密码"
exit sub
End If
Dim myUser As String
Dim mm As String
myUser= Me.Text4.Value
mm = Me.Text6.Value
if dcount("用户名","用户名","用户名 ='" & myUser& "'")=0 then
msgbox "无此用户名"
Me.Text4.Value = ""
exit sub
end if
if dlookup("密码","用户名","用户名 ='" & myUser & "'") <> mm then
msgbox "密码错误"
Me.Text6.Value = ""
end if
End Sub
另外,你的第2个问题,楼下已有正解

网友(2):

sql = "select * from 用户名 where 用户名 ='" & Me.Text4.Value & "'"