J
jamescox
Have a Access db with a tblUsers which contains a user ID field (text)
a DBAdmin field (Yes/No) and a LocalDBAdmin field (Yes/No). I'v
written a subroutine to determing what settings are in effect for
user, using a single DAO recordset and a QueryDef. It works fine fo
the first requery, but the second one always returns an empty recordset
even when there is something to be returned. Changing the order of th
requeries indicates that the SQL expresions are correct - regardless o
what order they are in, the third never works properly.
Any ideas what's going wrong?
Option Compare Database
Option Explicit
Public glUserClass As Long
Public Sub UserClass()
'Called from Form_Load of Switchboard; sets glUserClass
' 3 = DBAdmin
' 2 = Local DBAdmin
' 1 = Responsible Person
' 0 = no access
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim qryDef As QueryDef
Dim strSQL As String
Dim strUser As String
'Needed to handle the rs.MoveNext if rs is empty
On Error Resume Next
strUser = Environ$("USERNAME")
Set db = CurrentDb
'First check to see if user is in tblUsers at all - exit if not
strSQL = "SELECT * FROM tblUsers WHERE UserID = """ & strUser
""";"
'Create QueryDef - note that use of "" in the CreateQueryDef make
this a temporary QueryDef that
' is not added to the database's QueryDefs collection
Set qryDef = db.CreateQueryDef("", strSQL)
Set rs = qryDef.OpenRecordset
rs.MoveLast
' Set rs = db.OpenRecordset(strSQL)
If rs.EOF Then
glUserClass = 0
GoTo Clean_Up
End If
'Now check to see if user is DBAdmin - exit if True
' Set up new SQL string
strSQL = "SELECT * FROM tblUsers WHERE UserID = """ & strUser & ""
and DBAdmin = -1;"
'Update QueryDef SQL then requery existing recordset
qryDef.SQL = strSQL
rs.Requery qryDef
rs.MoveLast
If Not rs.EOF Then
glUserClass = 3
GoTo Clean_Up
End If
'Now check to see if user is Local DBAdmin - exit if True
' Set up new SQL string
strSQL = "SELECT * FROM tblUsers WHERE UserID = """ & strUser & ""
and LocalDBAdmin = -1;"
'Update QueryDef SQL then requery existing recordset
qryDef.SQL = strSQL
rs.Requery qryDef
rs.MoveLast
If Not rs.EOF Then
glUserClass = 2
GoTo Clean_Up
End If
'If we've made it to here, then the user is in class 1
glUserClass = 1
Clean_Up:
Set qryDef = Nothing
Set rs = Nothing
Set db = Nothing
End Su
a DBAdmin field (Yes/No) and a LocalDBAdmin field (Yes/No). I'v
written a subroutine to determing what settings are in effect for
user, using a single DAO recordset and a QueryDef. It works fine fo
the first requery, but the second one always returns an empty recordset
even when there is something to be returned. Changing the order of th
requeries indicates that the SQL expresions are correct - regardless o
what order they are in, the third never works properly.
Any ideas what's going wrong?
Option Compare Database
Option Explicit
Public glUserClass As Long
Public Sub UserClass()
'Called from Form_Load of Switchboard; sets glUserClass
' 3 = DBAdmin
' 2 = Local DBAdmin
' 1 = Responsible Person
' 0 = no access
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim qryDef As QueryDef
Dim strSQL As String
Dim strUser As String
'Needed to handle the rs.MoveNext if rs is empty
On Error Resume Next
strUser = Environ$("USERNAME")
Set db = CurrentDb
'First check to see if user is in tblUsers at all - exit if not
strSQL = "SELECT * FROM tblUsers WHERE UserID = """ & strUser
""";"
'Create QueryDef - note that use of "" in the CreateQueryDef make
this a temporary QueryDef that
' is not added to the database's QueryDefs collection
Set qryDef = db.CreateQueryDef("", strSQL)
Set rs = qryDef.OpenRecordset
rs.MoveLast
' Set rs = db.OpenRecordset(strSQL)
If rs.EOF Then
glUserClass = 0
GoTo Clean_Up
End If
'Now check to see if user is DBAdmin - exit if True
' Set up new SQL string
strSQL = "SELECT * FROM tblUsers WHERE UserID = """ & strUser & ""
and DBAdmin = -1;"
'Update QueryDef SQL then requery existing recordset
qryDef.SQL = strSQL
rs.Requery qryDef
rs.MoveLast
If Not rs.EOF Then
glUserClass = 3
GoTo Clean_Up
End If
'Now check to see if user is Local DBAdmin - exit if True
' Set up new SQL string
strSQL = "SELECT * FROM tblUsers WHERE UserID = """ & strUser & ""
and LocalDBAdmin = -1;"
'Update QueryDef SQL then requery existing recordset
qryDef.SQL = strSQL
rs.Requery qryDef
rs.MoveLast
If Not rs.EOF Then
glUserClass = 2
GoTo Clean_Up
End If
'If we've made it to here, then the user is in class 1
glUserClass = 1
Clean_Up:
Set qryDef = Nothing
Set rs = Nothing
Set db = Nothing
End Su