访问VBA Recordset

时间:2014-04-01 17:22:14

标签: sql vba ms-access recordset continuous-forms

我有一个访问数据库,其中包含一个包含员工信息的表。我的问题是我想循环遍历另一个表,以便首先确定特定特征是真还是假,然后以连续形式显示所有真实的记录。它仍然填充所有记录,而不仅仅是那些真实的记录。请参阅下面的代码。

Private Sub RunQuery_Click()
Dim strSQL As String
Dim dba As Database
Dim tbl As Recordset
Dim Code As String
Dim status As String

Set dba = CurrentDb
strSQL = "SELECT DISTINCT EmployeeName,SSN,Location,SystemAssignedPersonID FROM dbo_tbl_Random "
strSQL = strSQL & "WHERE MenuUsed = 'Random' ORDER BY Location,EmployeeName"
Set tbl = dba.OpenRecordset(strSQL, dbOpenDynaset, dbSeeChanges)
With tbl
  .MoveFirst
  If tbl.EOF Then
    MsgBox "There are no employees on Random at this time.", , "Oops! Try Again"
  Else
    Do Until tbl.EOF
      status = getEmpStatusID(tbl!SystemAssignedPersonID)
      If status = "A" Then
        Set Me.Recordset = tbl
        .MoveNext
      Else
        .MoveNext
      End If
    Loop
  End If
End With
Set tbl = Nothing
Set dba = Nothing
End Sub

getEmpStatusID是一个单独的函数,不会给我带来麻烦。它查找Employee ID以获取信息并将其返回。

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

我认为你已经完成了大部分工作。取决于getEmpStatusID你可以做这样的事情

SELECT DISTINCT EmployeeName,SSN,Location,T.SystemAssignedPersonID 
FROM dbo_tbl_Random R
Inner JOin Table_Where_Status_Is_Found as T 
    on T.SystemAssignedPersonID = R.SystemAssignedPersonID
WHERE MenuUsed = 'Random' and T.SystemAssignedPersonID = 'A'
ORDER BY Location,EmployeeName

将其用作表单的行源,不要使用VBA。但是,您的表单不可更新,因为您使用了distinct。这有必要吗?