我有一个VBA应用程序。当用户打开文档时,根据其访问级别(存储在SQL表中),他们将看到主菜单的两个版本之一。访问级别列是整数,并且为“ 1”或“ 0”。我正在尝试根据查询该表的结果在VBA中使用一个简单的if语句,但是我在rs对象上始终遇到类型不匹配的情况。
Sub accesscheck()
Dim Cn As ADODB.Connection
Dim Server_Name As String
Dim Database_Name As String
Dim SQLStr As String
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Server_Name = "SDL02-VM25"
Database_Name = "PIA"
SQLStr = "select [AccessLevel] from dbo.[AttendanceUsers] Where [Adusername] ='" & getWinUser & "'"
Set Cn = New ADODB.Connection
Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & vbNullString
rs.Open SQLStr, Cn, adOpenStatic
If rs = "1" Then MainMenu.Show vbModeless Else OLMenu.Show
rs.Close
Cn.Close
Set rs = Nothing
Set Cn = Nothing
End Sub
答案 0 :(得分:1)
您必须指定列名,并且也不需要""
。试试这个:
....
....
If rs("AccessLevel") = 1 Then MainMenu.Show vbModeless Else OLMenu.Show
....
....