我试图这样做,所以每当学生登录到这个数据库时,它会创建一个查询,通过DLookup和QueryDef匹配LoginTable中的FullName和StudentGrades表中的FullName(我几乎没有关于如何使用的想法,我可能完全错了)。如果你知道如何解决这个问题,请解释QueryDef,或者知道一种更简单的方法,请帮助我,谢谢。
登录按钮的VBA代码:
Private Sub Command1_Click()
'If there is no password or username then shows pop-up
'usertext is username box and passtext is password box
If IsNull(Me.usertext) Then
MsgBox "Please enter login", vbInformation, "LOGIN REQUIRED"
Me.usertext.SetFocus
ElseIf IsNull(Me.passtext) Then
MsgBox "Please enter password", vbInformation, "PASSWORD REQUIRED"
Me.passtext.SetFocus
'Sets actual values from table to values started above and checks if username and password actually match up with table
Else
If (IsNull(DLookup("[UserLogin]", "LoginTable", "[UserLogin] ='" & Me.usertext.Value & "' and password = '" & Me.passtext.Value & "'"))) Then
MsgBox "Username/password not valid"
'Checks for what securitylvl is the login and sets up studentqry if user is student
Else
Dim SecurityLvl As Variant
Dim StudentName As Variant
Dim StudentQry As QueryDef
SecurityLvl = DLookup("SecurityLvl", "LoginTable", "[UserLogin] ='" & Me.usertext.Value & "'")
StudentName = DLookup("FullName", "LoginTable", "[UserLogin] ='" & Me.usertext.Value & "'")
If (SecurityLvl = "Admin") Then
MsgBox "Admin Login successful"
DoCmd.Close
DoCmd.OpenForm "AdminForm"
ElseIf (SecurityLvl = "Professor") Then
MsgBox "Teacher Login successful"
DoCmd.Close
DoCmd.OpenForm "TeacherForm"
ElseIf (SecurityLvl = "Student") Then
MsgBox "Student Login successful"
DoCmd.Close
Set StudentQry = DBVeryinitialprototype.CreateQueryDef("StudentQuery", "Select * from StudentGrades where `FullName` = StudentName")
DoCmd.OpenQuery "StudentQuery"
End If
End If
End If
End
End Sub
答案 0 :(得分:1)
每次登录都不需要创建/删除临时查询。只需创建一个在动态数据上运行的永久查询。具体来说,将 StudentGrades 加入 LoginTable ,其中输出将是当前登录的用户。
由于您的登录过程不明确,可能您有一个状态字段,用于标识当前会话中的哪个学生(除非您每次都清除 LoginTable 中的所有记录)。
SELECT s.*
FROM StudentGrades s
INNER JOIN LoginTable l
ON l.FullName = s.StudentName
WHERE l.LoginStatus = True
理想情况下,您加入ID而不是完整的字符串值。登录应该有一个组合框,允许选择具有隐藏的绑定字段作为ID的学生,或者在键入的文本框字段中检索相应的 StudentID 。这样, StudentQuery 就会显示为:
SELECT s.*
FROM StudentGrades s
INNER JOIN LoginTable l
ON l.StudentID = s.StudentID
WHERE l.LoginStatus = True
如何创建永久查询?这应该包含在MS Access 101课程中:
目前,您将使用querydef创建查询的编码路径。但同样没有必要动态地执行此操作,只需创建一次查询,数据应相应地对齐。