联合查询的级联组合框查询

时间:2015-09-02 19:42:36

标签: sql vba ms-access-2010 cascadingdropdown subform

我一直致力于建立一个数据库,为指定的读者组织论文资源,以便在我的办公室轻松访问和审核。

我已经从here采用了级联组合框骨架,我希望让评审人员和分配人员根据标准在子表格中过滤论文。问题很大程度上是由于我的组织让我拥有一个分支然后线性的关系树,而不是一个直线性关系树。有很多有用的提示,但我似乎无法得到正确的组合因此,以解决我的障碍。

关系树如下:Paper Type> SubCommittee> Session> Papers。我已经确定了这一点,因为我正在审查来自一个来源(国际资源)的论文,该论文有两种论文类型(工作和非正式),并且有几个小组委员会(我的办公室只对两个委员会感兴趣)。

级联工作正常,子表单最初以正确的方式从查询填充,但是当选择选项时,查询无法匹配条件,因为它们是查找值。

标识为qrylstpapers的查询包含以下字段:

PaperTypeName, SubCommitteeName, SessionNumber, PaperNumber, Title, Subject, Origin.

这些字段是从PaperType,SubCommittee,Session,Papers

表中提取的

我有两个查询,一个用于工作,一个用于非正式的papertypes。我有第三个联合查询来组合这两个。

我认为,从联合查询中进行查询会使其更容易,并且当填充子表单时,排序不起作用。

我在VBA中使用的命令对联合查询的查询进行排序如下:

Private Sub FilterPaperList()

Dim strRS As String

strRS = "SELECT qrylstpapers.Title, qrylstpapers.PaperTypeName FROM qrylstpapers"

If Not IsNull(Me.cboPaperNumberAssign) Then
    strRS = strRS & " WHERE PaperNumber = " & Me.cboPaperNumberAssign
ElseIf Not IsNull(Me.cboSessionAssign) Then
    strRS = strRS & " WHERE SessionNumber = " & Me.cboSessionAssign
ElseIf Not IsNull(Me.cboSubCommitteeAssign) Then
    strRS = strRS & " WHERE SubCommitteeName = " & Me.cboSubCommitteeAssign
ElseIf Not IsNull(Me.cboPaperTypeAssign) Then
   strRS = strRS & " WHERE PaperTypeName = " & Me.cboPaperTypeAssign
End If

strRS = strRS & " ORDER BY qrylstpapers.Title;"

Me.lstPapers.RowSource = strRS

Me.lstPapers.Requery

End Sub

我尝试过的一件事就是指定“Me.cboPapertypeAssign.Column(1)”,它会弹出一个询问输入的对话框窗口,后面跟着papertypename“WP”或“INF”会给我适当的排序。

任何想法,见解和/或一般批评都会受到高度赞赏。即使这是关系的重组。

修改

Option Compare Database
Option Explicit

Private Sub cboAgency_AfterUpdate()

  ' Set the Mode combo box to be limited by the selected Agency
  Me.cboMode.RowSource = "SELECT tblMode.ModeID, tblMode.ModeName FROM tblMode " & _
     " WHERE AgencyID = " & Nz(Me.cboAgency) & _
     " ORDER BY ModeName"
  Me.cboMode = Null

    EnableControls


End Sub

Private Sub cboMode_AfterUpdate()
  ' Set the Branch combo box to be limited by the selected Mode
  Me.cboBranch.RowSource = "SELECT tblBranch.BranchID, tblBranch.BranchName FROM tblBranch " & _
     " WHERE ModeID = " & Nz(Me.cboMode) & _
     " ORDER BY BranchName"
  Me.cboBranch = Null

    EnableControls



End Sub

Private Sub cboBranch_AfterUpdate()
  ' Set the Office combo box to be limited by the selected Branch
  Me.cboOffice.RowSource = "SELECT tblOffice.OfficeID, tblOffice.OfficeNumber FROM tblOffice " & _
     " WHERE BranchID = " & Nz(Me.cboBranch) & _
     " ORDER BY OfficeNumber"
  Me.cboOffice = Null

    EnableControls


End Sub


Private Sub cboOffice_AfterUpdate()
  ' Set the Reviewer combo box to be limited by the selected Office
  Me.cboReviewer.RowSource = "SELECT tblReviewers.ReviewerID, tblReviewers.LastName FROM tblReviewers " & _
     " WHERE OfficeID = " & Nz(Me.cboOffice) & _
     " ORDER BY LastName"
  Me.cboReviewer = Null

    EnableControls



End Sub

Private Sub cboPaperTypeAssign_AfterUpdate()
'Enable the SubCommittee Combo Box
If Me.cboPaperTypeAssign.Column(1) = "WP" Then
    Me.cboSubCommitteeAssign.RowSource = "SELECT tblUNWPSubCommittee.SubCommitteeID, tblUNWPSubCommittee.SubCommitteeName From tblUNWPSubCommittee " & _
        " Where PaperTypeID = " & Nz(Me.cboPaperTypeAssign) & _
        " ORDER BY SubCommitteeName"
    Me.cboSubCommitteeAssign = Null
ElseIf Me.cboPaperTypeAssign.Column(1) = "INF" Then
    Me.cboSubCommitteeAssign.RowSource = "SELECT tblUNINFSubCommittee.SubCommitteeID, tblUNINFSubCommittee.SubCommitteeName From tblUNINFSubCommittee " & _
        " Where PaperTypeID = " & Nz(Me.cboPaperTypeAssign) & _
        " ORDER BY SubCommitteeName"
    Me.cboSubCommitteeAssign = Null
End If

    EnableControls
    FilterPaperList

End Sub

Private Sub cboSubCommitteeAssign_AfterUpdate()
'Enable the Session Combo Box
If Me.cboSubCommitteeAssign.Column(1) = "GHS" And Me.cboPaperTypeAssign.Column(1) = "INF" Then
    Me.cboSessionAssign.RowSource = "SELECT tblUNINFSessions.SessionID, tblUNINFSessions.SessionNumber From tblUNINFSessions " & _
        " Where SubCommitteeID = " & Nz(Me.cboSubCommitteeAssign) & _
        " ORDER BY SessionNumber"
    Me.cboSessionAssign = Null
ElseIf Me.cboSubCommitteeAssign.Column(1) = "TDG" And Me.cboPaperTypeAssign.Column(1) = "INF" Then
    Me.cboSessionAssign.RowSource = "SELECT tblUNINFSessions.SessionID, tblUNINFSessions.SessionNumber From tblUNINFSessions " & _
        " Where SubCommitteeID = " & Nz(Me.cboSubCommitteeAssign) & _
        " ORDER BY SessionNumber"
    Me.cboSessionAssign = Null
ElseIf Me.cboSubCommitteeAssign.Column(1) = "GHS" And Me.cboPaperTypeAssign.Column(1) = "WP" Then
    Me.cboSessionAssign.RowSource = "SELECT tblUNWPSessions.SessionID, tblUNWPSessions.SessionNumber From tblUNWPSessions " & _
        " Where SubCommitteeID = " & Nz(Me.cboSubCommitteeAssign) & _
        " ORDER BY SessionNumber"
    Me.cboSessionAssign = Null
ElseIf Me.cboSubCommitteeAssign.Column(1) = "TDG" And Me.cboPaperTypeAssign.Column(1) = "WP" Then
    Me.cboSessionAssign.RowSource = "SELECT tblUNWPSessions.SessionID, tblUNWPSessions.SessionNumber From tblUNWPSessions " & _
        " Where SubCommitteeID = " & Nz(Me.cboSubCommitteeAssign) & _
        " ORDER BY SessionNumber"
    Me.cboSessionAssign = Null
End If

    EnableControls
    FilterPaperList
End Sub

Private Sub cboSessionAssign_AfterUpdate()

'Enable the Paper Number Combo Box
If Me.cboSubCommitteeAssign.Column(1) = "GHS" And Me.cboPaperTypeAssign.Column(1) = "INF" Then
    Me.cboPaperNumberAssign.RowSource = "Select tblUNINFPapers.ID, tblUNINFpapers.PaperNumber From tblUNINFPapers " & _
        " Where SessionID = " & Nz(Me.cboSessionAssign) & _
        " Order by PaperNumber"
ElseIf Me.cboSubCommitteeAssign.Column(1) = "TDG" And Me.cboPaperTypeAssign.Column(1) = "INF" Then
    Me.cboPaperNumberAssign.RowSource = "Select tblUNINFPapers.ID, tblUNINFpapers.PaperNumber From tblUNINFPapers " & _
        " Where SessionID = " & Nz(Me.cboSessionAssign) & _
        " Order by PaperNumber"
ElseIf Me.cboSubCommitteeAssign.Column(1) = "GHS" And Me.cboPaperTypeAssign.Column(1) = "WP" Then
    Me.cboPaperNumberAssign.RowSource = "Select tblUNWPPapers.ID, tblUNWPPapers.PaperNumber From tblUNWPPapers " & _
        " Where SessionID = " & Nz(Me.cboSessionAssign) & _
        " Order by PaperNumber"
ElseIf Me.cboSubCommitteeAssign.Column(1) = "TDG" And Me.cboPaperTypeAssign.Column(1) = "WP" Then
    Me.cboPaperNumberAssign.RowSource = "Select tblUNWPPapers.ID, tblUNWPPapers.PaperNumber From tblUNWPPapers " & _
        " Where SessionID = " & Nz(Me.cboSessionAssign) & _
        " Order by PaperNumber"
End If
    EnableControls
    FilterPaperList
End Sub

Private Sub cboPaperNumberAssign_AfterUpdate()
    ' Filter the list of papers based on the selection(s)
    FilterPaperList
End Sub


Private Sub FilterPaperList()

    Dim strRS As String

'     Filter the list box appropriateley based on the combo box selection(s)
    strRS = "SELECT qrylstpapers.Title, qrylstpapers.PaperTypeName FROM qrylstpapers"

    If Not IsNull(Me.cboPaperNumberAssign) Then
        strRS = strRS & " WHERE PaperNumber = " & Me.cboPaperNumberAssign
    ElseIf Not IsNull(Me.cboSessionAssign) Then
        strRS = strRS & " WHERE SessionNumber = " & Me.cboSessionAssign
    ElseIf Not IsNull(Me.cboSubCommitteeAssign) Then
        strRS = strRS & " WHERE SubCommitteeName = " & Me.cboSubCommitteeAssign
    ElseIf Not IsNull(Me.cboPaperTypeAssign) Then
       strRS = strRS & " WHERE PaperTypeName = " & Me.cboPaperTypeAssign
    End If

    strRS = strRS & " ORDER BY qrylstpapers.Title;"

    Me.lstPapers.RowSource = strRS

    Me.lstPapers.Requery


End Sub


Private Sub EnableControls()

  ' Clear the Reviewer combo boxes
  If IsNull(Me.cboAgency) Then
     Me.cboMode = Null
  End If

  If IsNull(Me.cboMode) Then
    Me.cboBranch = Null
  End If

  If IsNull(Me.cboBranch) Then
    Me.cboOffice = Null
  End If

  If IsNull(Me.cboOffice) Then
    Me.cboReviewer = Null
  End If

' Clear the PaperAssign combo boxes
If IsNull(Me.cboPaperTypeAssign) Then
    Me.cboSubCommitteeAssign = Null
    End If
If IsNull(Me.cboSubCommitteeAssign) Then
    Me.cboSessionAssign = Null
    End If
If IsNull(Me.cboSessionAssign) Then
    Me.cboPaperNumberAssign = Null
    End If

' Enable or disable Reviewer combo boxes based on whether the combo box preceeding it has a value.

  Me.cboMode.Enabled = (Not IsNull(Me.cboAgency))
  Me.cboBranch.Enabled = (Not IsNull(Me.cboMode))
  Me.cboOffice.Enabled = (Not IsNull(Me.cboBranch))
  Me.cboReviewer.Enabled = (Not IsNull(Me.cboOffice))

' Enable or disable PaperAssign combo boxes based on whether the combo box preceeding it has a value.

 Me.cboSubCommitteeAssign.Enabled = (Not IsNull(Me.cboPaperTypeAssign))
 Me.cboSessionAssign.Enabled = (Not IsNull(Me.cboSubCommitteeAssign))
 Me.cboPaperNumberAssign.Enabled = (Not IsNull(Me.cboSessionAssign))

End Sub


Private Sub Form_Load()
    EnableControls
    FilterPaperList
End Sub

1 个答案:

答案 0 :(得分:0)

VBA查询中有一些突出的事情:

  1. 逻辑中不包含多个组合框实例;
  2. 可能无法完全遵守字符串和数字数据。除非SubCommitteeName和PaperTypeName是数字字段,否则它们的表达式需要单引号;
  3. 考虑以下内容以允许在WHERE语句中连接任何框(注意用于开始连接的真实语句1 = 1的看似多余的使用。此外,我打破了{{1因为任何combox box选择组合都应该有多个结果。

    ElseIfs