本文介绍了在Access 2007中使用NT登录作为通过VBA运行的SQL查询的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Access 2007中,我试图使用NTlogin通过SQL查询从表中检索值(请参见下面的代码).加载表单时,出现一条错误消息,提示编译错误:期望的函数或变量".有人可以告诉我如何解决此错误.
In Access 2007, I'm trying to use the NTlogin to retrieve a value from a table via a SQL query (see code below). WHen the form loads, I get an error message saying "Compile Error: Expected Function or Variable". Can someone tell me how to fix this error.
Private Sub Form_Load()
Dim UserName As String
Dim strSQL As String
Dim strDept As String
UserName = Environ("USERNAME")
strSQL = "SELECT DEPT FROM IDs WHERE NTLOGIN =" & UserName
strDept = DoCmd.RunSQL(strSQL)
cmdSave.Enabled = False
cmdEdit.Enabled = True
cmdPrevious.Enabled = True
cmdNext.Enabled = True
End Sub
推荐答案
您不能将RunSQL与select语句一起使用.它仅用于操作查询.
You cannot use RunSQL with a select statement. It is only for action queries.
如果您想要一个记录集,则可以说:
If you want a recordset, you can say, amongst other things:
strSQL = "SELECT DEPT FROM IDs WHERE NTLOGIN ='" & UserName & "'"
Set rs=CurrentDB.OpenRecordset(strSQL)
rs是DAO.Recordset的地方
Where rs is DAO.Recordset
这篇关于在Access 2007中使用NT登录作为通过VBA运行的SQL查询的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!