如何更改我的 VBA 脚本以显示特定字段设置为“空”的所有记录?

Posted

技术标签:

【中文标题】如何更改我的 VBA 脚本以显示特定字段设置为“空”的所有记录?【英文标题】:How do I alter my VBA script so it shows all the records where a specific field is set to 'Null'? 【发布时间】:2021-11-25 12:16:01 【问题描述】:

我有这个拆分表单,其中包含一些基于组合框和搜索字段的基本搜索功能。函数 SearchCriteria 在组合框的 OnChange 事件上调用,或者通过单击拆分表单顶部的搜索图标调用。

我目前用来过滤记录源的 VBA 代码qry_Administration

Function SearchCriteria()

Dim Customer, CustomerLocation, CustomerLocationPlace, ExecutionDate, Material As String
Dim Intern, Extern As String
Dim task, strCriteria As String

If Me.chk_AuditEX = True Then
    Extern = "[AuditEX] = " & Me.chk_AuditEX
Else
    Extern = "[AuditEX] like '*'"
End If

 If Me.chk_AuditIN = True Then
    Intern = "[AuditIN] = " & Me.chk_AuditIN
Else
    Intern = "[AuditIN] like '*'"
End If

If IsNull(Me.cbo_CustomerLocations) Then
    CustomerLocation = "[CustomerLocationID] like '*'"
    CustomerLocationPlace = "[LocationCompanyPlace] 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.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" & _
        & ExecutionDate & Material & "And" & Extern & "And" & Intern
            
task = "Select * from qry_Administration where (" & strCriteria & ") order by ExecutionDate DESC"

Debug.Print (task)

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

调用SearchCriteria 后,所有例如CustomerLocationID = Null 的记录都不会显示在我的拆分表单上。 我需要在哪里编辑我的 VBA 脚本以显示所有记录?即使一个字段是空的。 例如:我没有CustomerlocationID 或者我还没有ExecutionDate

【问题讨论】:

试试[CustomerLocationID] is Null w3schools.com/sql/sql_null_values.asp 【参考方案1】:

假设您要问的是“当我对某个字段的搜索条件为空时,我如何让它返回所有内容而不管该字段中有什么?”然后像这样更改您的行:

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

到这里:

If IsNull(Me.cbo_Customers) Then
    Customer = " 1=1 "
Else
    Customer = "[CustomerID] = " & Me.cbo_Customers
End If

这会导致 WHERE 过滤忽略该列中的内容。有更复杂的方法可以做到这一点,但对于您的代码来说,这是最简单也是最快的。


另一方面,如果您要问的是“当我对某个字段的搜索条件为空时,我如何只返回那些在该列中为空的行?”然后把你的台词改成这样:

If IsNull(Me.cbo_Customers) Then
    Customer = "[CustomerID] IS NULL "
Else
    Customer = "[CustomerID] = " & Me.cbo_Customers
End If

【讨论】:

以上是关于如何更改我的 VBA 脚本以显示特定字段设置为“空”的所有记录?的主要内容,如果未能解决你的问题,请参考以下文章

Laravel API - 当状态字段为空时显示所有地区

oMail - 将更改的合并字段从数据库表单添加到 vba 代码

使用 VBA 将数据透视表设置为基于另一个字段的特定日期

如何在 Access VBA 中评估字段名称的文本

如何vba将一个值放在相邻列中以获得特定值

怎么写一个SQL语句查询 我的字段是float型的, 如何让它如果是空则显示空 如果是0则显示出来为 0