使用相同 "string value" 但 ID 不同的组合框对组合框中的记录进行分组

Grouping records in a combobox on split-form with the same "string value" but different ID's

我知道我可以对多值字段执行此操作,但这会导致其他问题。

我有一个带有组合框的主拆分表单,用于对各种不同的组合进行排序。例如,我有一个 cbo_Customerscbo_CustomerLocations 等...(请参阅底部的 VBA 代码)

相同的CustomerLocation可以有不同的Customer。所以有可能有 2 个不同的 Customer 具有相同的 CustomerLocation.

在我的主要拆分表单中,我有一个名为 cbo_CustomerLocations 的组合框,它从 tbl_CustomerLocations 中查找值。 tbl_CustomerLocations 由 4 个字段组成。 CustomerLocationIDLocationCompanyNameLocationCompanyPlaceCustomerID(链接到 tbl_Customers

我有 1 个位置命名为:TESTLOCATION
我有 2 名客户:CUSTOMER_1CUSTOMER_2

我试图避免使用多值字段,因此 在 tbl_CustomerLocations 中,值为 TESTLOCATION 的记录在此 table 中出现两次,因为字段 CustomerID 链接到 CUSTOMER_1 而另一条记录链接到 CUSTOMER_2

现在在组合框的主窗体中 cbo_CustomerLocations 这个 TESTLOCATION 也显示了两次(2 个不同的 CustomerLocationID

我想要这个组合框 GROUP BY CustomerLocationName。而且,如果我 select 这个(分组)TESTLOCATION 我的表格必须显示找到字符串 "TESTLOCATION" 的所有记录。 (TESTLOCATION 有不同的 ID)

这是我的 SQL,其中 TESTLOCATION 显示了两次:

SELECT tbl_CustomerLocations.CustomerLocationID, 
tbl_CustomerLocations.LocationCompanyName, 
tbl_CustomerLocations.LocationCompanyPlace
FROM tbl_CustomerLocations
ORDER BY tbl_CustomerLocations.LocationCompanyName;

我试过类似的方法:

SELECT Count(tbl_CustomerLocations.CustomerLocationID) AS
CountOfCustomerLocationID, tbl_CustomerLocations.LocationCompanyName
FROM tbl_CustomerLocations
GROUP BY tbl_CustomerLocations.LocationCompanyName;

这确实在组合框中合并了 TESTLOCATION,但我的记录没有显示在我的拆分表单中

我也试过这个,在 Stack Overflow 上找到:

SELECT * FROM tbl_CustomerLocations e1, tbl_CustomerLocations e2 
WHERE e1.LocationCompanyName = e2.LocationCompanyName
AND e1.CustomerLocationID != e2.CustomerLocationID;

这也是死路一条

在我的主要拆分表单中,在我更新组合框 cbo_CustomerLocations 后调用以下 VBA 代码:

Private Sub cbo_CustomerLocations_AfterUpdate()
Call SearchCriteria
End Sub


Function SearchCriteria()
Dim Customer, CustomerLocation as String
Dim task, strCriteria As String

If IsNull(Me.Cbo_Customers) Then
    Customer = "[CustomerID] like '*'"
Else
    Customer = "[CustomerID] = " & Me.Cbo_Customers
End If

If IsNull(Me.cbo_CustomerLocations) Then
    CustomerLocation = "[CustomerLocationID] like '*'"
Else
    CustomerLocation = "[CustomerLocationID] = " & cbo_CustomerLocations
End If

strCriteria = Customer & "And" & CustomerLocation

task = "Select * from qry_Administration where (" & strCriteria & ")"

Me.Form.RecordSource = task
Me.Form.Requery

所以基本上我想 select cbo_CustomerLocations 中的(分组)TESTLOCATION。然后调用函数 SearchCriteria,我的表单显示找到字符串 TESTLOCATION 的所有记录。 TESTLOCATION 有不同的 ID。

我想我还必须以某种方式编辑这些行?

strCriteria = Customer & "And" & CustomerLocation
task = "Select * from qry_Administration where (" & strCriteria & ")"

因为 strCriteria 这个分组字段有问题?

我知道这是很多信息,但我尽量说清楚。

如果我的理解正确,您希望主窗体中的 cbo_CustomerLocations 组合框包含不同客户位置的列表。选择一个值后,您希望过滤子表单以便只显示所选位置的客户,是吗?以下是我的建议:

组合框: tbl_CustomerLocations 包含这样的数据:

CustomerLocationID LocationCompanyName LocationCompanyPlace CustomerID
1 Company A Netherlands 1
2 Company B Netherlands 2
3 Company C England 4
4 Company D Spain 5
5 Company E England 16

您希望组合框包含此列表:

LocationCompanyPlace
England
Netherlands
Spain

为此,组合框的数据源应如下所示:

SELECT LocationCompanyPlace
FROM tbl_CustomerLocations
GROUP BY LocationCompanyPlace

SELECT DISTINCT LocationCompanyPlace
FROM tbl_CustomerLocations
GROUP BY LocationCompanyPlace

(以上两个查询应该 return 相同的结果。)

现在,您需要更新 VBA 代码。

子cbo_CustomerLocations_AfterUpdate(): (无需更改)

Private Sub cbo_CustomerLocations_AfterUpdate()
    Call SearchCriteria
End Sub

函数 SearchCriteria():

Function SearchCriteria()
    Dim Customer, CustomerLocation as String
    Dim task, strCriteria As String
    Dim recordSource As string


    If IsNull(Me.Cbo_Customers) = False Then                    'If a Customer is selected, filter by selected Customer
        recordSource = "SELECT * FROM qry_Administration WHERE [CustomerID] = " & Me.Cbo_Customers
    Else                                                        'If a Customer is NOT selected, check to see if a CustomerLocation is selected
        If IsNull(me.cbo_CustomerLocations) = False Then        'If a Customer Location is selected, filter by selected CustomerLocation
            'Use this line if the CustomerLocationPlace field exists in qry_Administration
            recordSource = "SELECT * FROM qry_Administration WHERE [CustomerLocationPlace]='" & Me.Cbo_CustomerLocations & "'"
            
            'Use the recordSource below if the CustomerLocationPlace field DOES NOT exist in qry_Administration
            'This line joins qry_Administration to the tbl_CustomerLocations table so that you can filter on the CustomerLocationPlace field
            'By using "SELECT a.*", only records from qry_Administration are returned
            'recordSource = "SELECT a.* FROM qry_Administration a INNER JOIN dbo.tbl_CustomerLocations l ON a.CustomerID = l.CustomerID WHERE l.CustomerLocationPlace='" & Me.Cbo_CustomerLocations & "'"
        Else                                                    'If neither a Customer nor a Customer Location are selected, return all records in qry_Administration           
            recordSource = "SELECT * FROM qry_Administration"
        End If
    End If

    Me.Form.RecordSource = recordSource
    Me.Form.Requery
End Function

我已经添加了注释来解释上面的代码,但请注意,您可能需要调整嵌套 if 语句的 If 部分中的代码。如果 CustomerLocationPlace 字段存在于 qry_Administration 中,那么您可以按原样使用代码。但是,如果 CustomerLocationPlace 字段在 qry_Administration 中不存在,那么您应该注释掉第 12 行 (recordSource = "SELECT * FROM qry_Administration WHERE [CustomerLocationPlace]='" & Me.Cbo_CustomerLocations & "'") 并取消注释第 17 行 ('recordSource = "SELECT a.* FROM qry_Administration a INNER JOIN dbo.tbl_CustomerLocations l ON a.CustomerID = l.CustomerID WHERE l.CustomerLocationPlace='" & Me.Cbo_CustomerLocations & "'").

我相信这个解决方案应该可以解决您的问题。如果您有任何问题或疑虑,请告诉我。

通过对您的解决方案进行一些编辑,我设法让它工作了。

分组 combobox 的解决方案有效。关于 CustomerLocationPlace 是查询的一部分,您也是正确的。我没有使用 CustomerLocationPlace 字段,而是使用了 CustomerLocationName 字段。我还必须添加以下行:

CustomerLocationPlace = "[LocationCompanyPlace] = '" & Me.cbo_CustomerLocations.Column(1) & "'"

我去掉了我的 VBA 这样你们就更容易了。我的脚本比我之前发布的要复杂一些。我确实使用了您的解决方案,但我像以前一样完整地保留了 VBA 脚本。只是为了解释目的,这里是我完整的 VBA 函数 StrCriteria 脚本,其中实施了 David Buck 的解决方案:

Function SearchCriteria()

    Dim Customer, CustomerLocation, CustomerLocationPlace, Protocol, SampleProvider, BRL, ProjectLeader, LabNumber, ExecutionDate, Classification, SampleProvider2, Material As String
    Dim Extern, Intern As String
    Dim strText, strSearch As String
    Dim task, strCriteria As String

    Me.FilterOn = True

    If IsNull(Me.txt_Search) Or Me.txt_Search = "" Then
        strText = "[DataID] like '*'"

    Else
        strSearch = Me.txt_Search.Value
            strText = "(LabNumberPrimary like  ""*" & strSearch & "*"")" & "Or" & _
            "(LabNumber_2_MH like  ""*" & strSearch & "*"")" & "Or" & _
            "(LabNumber_3_ASB like  ""*" & strSearch & "*"")" & "Or" & _
            "(LabNumber_4_CT like  ""*" & strSearch & "*"")" & "Or" & _
            "(LabNumber_5_LA like  ""*" & strSearch & "*"")" & "Or" & _
            "(LabNumber_6_CBR like  ""*" & strSearch & "*"")" & "Or" & _
            "(HerkeuringNumber like  ""*" & strSearch & "*"")" & "Or" & _
            "(Protocol like  ""*" & strSearch & "*"")" & "Or" & _
            "(BRL like  ""*" & strSearch & "*"")" & "Or" & _
            "(PrincipalCompanyName like  ""*" & strSearch & "*"")" & "Or" & _
            "(ProjectLeaderName like  ""*" & strSearch & "*"")" & "Or" & _
            "(Material like  ""*" & strSearch & "*"")" & "Or" & _
            "(Classification like  ""*" & strSearch & "*"")" & "Or" & _
            "(PrincipalContactName like  ""*" & strSearch & "*"")" & "Or" & _
            "(LocationContactName like  ""*" & strSearch & "*"")" & "Or" & _
            "(SampleProviderName like  ""*" & strSearch & "*"")" & "Or" & _
            "(QuotationNumber like  ""*" & strSearch & "*"")" & "Or" & _
            "(LocationCompanyName like  ""*" & strSearch & "*"")"
    End If

    If Me.chk_Ex = True Then
        Extern = "[AuditExID] = 2"
    Else
        Extern = "[AuditExID] like '*'"
    End If

    If Me.chk_In = True Then
        Intern = "[AuditInID] = 2"
    Else
        Intern = "[AuditInID] like '*'"
    End If

    If IsNull(Me.cbo_CustomerLocations) Then
        CustomerLocation = "[CustomerLocationID] like '*'"
    Else
        CustomerLocation = "[LocationCompanyName] = '" & Me.cbo_CustomerLocations.Column(0) & "'"
        CustomerLocationPlace = "[LocationCompanyPlace] = '" & Me.cbo_CustomerLocations.Column(1) & "'"
    End If

    If IsNull(Me.Cbo_Customers) Then
        Customer = "[CustomerID] like '*'"
    Else
        Customer = "[CustomerID] = " & Me.Cbo_Customers
    End If

    If IsNull(Me.cbo_Protocol) Or Me.cbo_Protocol = "" Then
        Protocol = "[ProtocolID] like '*'"
    ElseIf Me.cbo_Protocol = 5 Then
        Protocol = "[ProtocolID] in (" & TempVars!tempProtocol & ")"
    Else
        Protocol = "([ProtocolID] = " & Me.cbo_Protocol & ")"
    End If

    If IsNull(Me.cbo_Classification) Or Me.cbo_Classification = "" Then
        Classification = "[ClassificationID] like '*'"
    ElseIf Me.cbo_Classification = 5 Then
        Classification = "[ClassificationID] in (" & TempVars!tempClassification & ")"
    Else
        Classification = "([ClassificationID] = " & Me.cbo_Classification & ")"
    End If

    If IsNull(Me.cbo_SampleProviders) Or Me.cbo_SampleProviders = "" Then
        SampleProvider = "[SampleProviderPrimaryID] like '*'"
    ElseIf Me.cbo_SampleProviders = 6 Then
        SampleProvider = "[SampleProviderPrimaryID] in (" & TempVars!tempSampleProviders & ")"
    Else
        SampleProvider = "([SampleProviderPrimaryID] = " & Me.cbo_SampleProviders & ")"
    End If

    If IsNull(Me.cbo_SampleProviders2) Then
        SampleProvider2 = "[SampleProviderSecondaryID] like '*'"
    Else
        SampleProvider2 = "[SampleProviderSecondaryID] = " & Me.cbo_SampleProviders2
    End If

    If IsNull(Me.cbo_BRL) Or Me.cbo_BRL = "" Then
        BRL = "[BRLID] like '*'"
    ElseIf Me.cbo_BRL = 5 Then
        BRL = "[BRLID] in (" & TempVars!tempBRL & ")"
    Else
        BRL = "([BRLID] = " & Me.cbo_BRL & ")"
    End If

    If IsNull(Me.cbo_ProjectLeaders) Then
        ProjectLeader = "[ProjectLeaderID] like '*'"
    Else
        ProjectLeader = "[ProjectLeaderID] = " & Me.cbo_ProjectLeaders
    End If

    If IsNull(Me.txt_ExecutionDateTo) Then
        ExecutionDate = "[ExecutionDate] like '*'"
    Else
        If IsNull(Me.txt_ExecutionDateFrom) Then
            ExecutionDate = "[ExecutionDate] like '" & Me.txt_ExecutionDateTo & "'"
        Else
            ExecutionDate = "([ExecutionDate] >= #" & Format(Me.txt_ExecutionDateFrom, "mm/dd/yyyy") & "# And [ExecutionDate] <= #" & Format(Me.txt_ExecutionDateTo, "mm/dd/yyyy") & "#)"
        End If
    End If

    If IsNull(Me.cbo_Material) Or Me.cbo_Material = "" Then
        Material = "[MaterialID] like '*'"
    ElseIf Me.cbo_Material = 6 Then
        Material = "[MaterialID] in (" & TempVars!tempMaterial & ")"
    Else
        Material = "([MaterialID] = " & Me.cbo_Material & ")"
    End If

    strCriteria = Customer & "And" & CustomerLocation & "And" & CustomerLocationPlace & "And" & Protocol & "And" & SampleProvider & "And" & BRL & "And" & ProjectLeader & "And" _
            & ExecutionDate & "And" & Extern & "And" & Intern & "And" & Classification & "And" _
            & SampleProvider2 & "And" & Material & "And" & strText
                
    task = "Select * from qry_Administration where (" & strCriteria & ") order by ExecutionDate DESC"

    Debug.Print (task)
    
    Me.Form.RecordSource = task
    Me.Form.Requery

End Function

部分查询及解决方案:

If IsNull(Me.cbo_CustomerLocations) Then
        CustomerLocation = "[CustomerLocationID] like '*'"
    Else
        CustomerLocation = "[LocationCompanyName] = '" & Me.cbo_CustomerLocations.Column(0) & "'"
        CustomerLocationPlace = "[LocationCompanyPlace] = '" & Me.cbo_CustomerLocations.Column(1) & "'"
    End If

对于 strCriteria 的调整:

strCriteria = Customer & "And" & CustomerLocation & "And" & CustomerLocationPlace & .........