access VBA 凑select 语句

Private Sub Command0_Click()
If Text4 = "" Then
MsgBox "请填写用户名"
ElseIf Text6 = "" Then
MsgBox "请填写密码"
End If
Dim data As New ADODB.Recordset
Dim sql As String
Dim mm As String
mm = Me.Text4.Value
Debug.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 If
End Sub

这是全部的代码。运行之后提示说至少有一个变量未指定。求教
还有就是 我希望把 sql这个变量凑成一个select语句,但是debugprint出来时
select * from 用户名 where 用户名 =songmingfeng,明显songmingfeng那里少了一组单引号但是加上了以后后面的却成了备注。所以求各位大神帮助

按你的写法,用户名表中只有第一条记录是能用的,其他的都查不到。所以给你改了一下。
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个问题,楼下已有正解
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-11-24
sql = "select * from 用户名 where 用户名 ='" & Me.Text4.Value & "'"
相似回答