【问题标题】:VBA vs Query: Discrepancy in Result CountVBA 与查询:结果计数的差异
【发布时间】:2015-10-25 17:29:18
【问题描述】:

this question 中所述,我正在实施一个系统以将现有数据库转换为更有效的布局。将针对该问题的 union/maketable 解决方案应用于示例数据库最初似乎可以正常工作。但是,在尝试使用 VBA 函数检查新表 应该 的条目数时,我发现这两种方法会产生不同的值,查询输出的行数少于 VBA 测试的预期行数.

VBA 计数条目:

Function countStreets()
Dim rst As DAO.Recordset
Dim count As Integer
count = 0
Set rst = CurrentDb.OpenRecordset("Map index")
rst.MoveFirst
While (Not rst.EOF)
    With rst
        If (Not IsNull(!Street)) Then count = count + 1
        If (Not IsNull(!Street_2)) Then count = count + 1
        If (Not IsNull(!Street_3)) Then count = count + 1
        If (Not IsNull(!Street_4)) Then count = count + 1
        If (Not IsNull(!Street_5)) Then count = count + 1
        If (Not IsNull(!Street_6)) Then count = count + 1
        If (Not IsNull(!Street_7)) Then count = count + 1
        If (Not IsNull(!Street_8)) Then count = count + 1
        If (Not IsNull(!Street_9)) Then count = count + 1
        If (Not IsNull(!Street_10)) Then count = count + 1
        If (Not IsNull(!Street_11)) Then count = count + 1
        If (Not IsNull(!Street_12)) Then count = count + 1
        .MoveNext
    End With
Wend
MsgBox (count)
rst.Close
Set rst = Nothing
End Function

联合查询:

SELECT fileID, Street FROM [Map index] WHERE Street IS NOT NULL;
UNION
SELECT fileID, Street_2 FROM [Map index] WHERE Street_2 IS NOT NULL;
UNION
SELECT fileID, Street_3 FROM [Map index] WHERE Street_3 IS NOT NULL;
UNION
SELECT fileID, Street_4 FROM [Map index] WHERE Street_4 IS NOT NULL;
UNION
SELECT fileID, Street_5 FROM [Map index] WHERE Street_5 IS NOT NULL;
UNION
SELECT fileID, Street_6 FROM [Map index] WHERE Street_6 IS NOT NULL;
UNION
SELECT fileID, Street_7 FROM [Map index] WHERE Street_7 IS NOT NULL;
UNION
SELECT fileID, Street_8 FROM [Map index] WHERE Street_8 IS NOT NULL;
UNION
SELECT fileID, Street_9 FROM [Map index] WHERE Street_9 IS NOT NULL;
UNION
SELECT fileID, Street_10 FROM [Map index] WHERE Street_10 IS NOT NULL;
UNION
SELECT fileID, Street_11 FROM [Map index] WHERE Street_11 IS NOT NULL;
UNION
SELECT fileID, Street_12 FROM [Map index] WHERE Street_12 IS NOT NULL;

可生成查询:

SELECT * INTO [fileID-Street]
FROM MergeStreetsUnionQry;

什么可能导致这两种方法的结果计数存在差异?差异足够小(这里是查询中的 28227 与计数器中的 28260),我确信这不是一种方法完全失败的结果,而是大到足以显得显着。

【问题讨论】:

    标签: sql ms-access vba ms-access-2007


    【解决方案1】:

    UNION 删除重复项,因此这表明您在 Street* 列中有一个或多个文件 ID 的重复值。

    UNION 更改为UNION ALL

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多