VBA与查询:结果计数的差异

时间:2015-08-03 14:38:02

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

我正在实现一个系统,将现有数据库转换为更高效的布局,如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;

Maketable query:

SELECT * INTO [fileID-Street]
FROM MergeStreetsUnionQry;

可能导致这两种方法之间结果计数差异的原因是什么?差异足够小(此处查询为28227,而计数器为28260),我确定这不是一种方法完全失败的结果,而是足够大,看起来很重要。

1 个答案:

答案 0 :(得分:4)

<li> <%= link_to destroy_user_session_path, method: :delete do %> <span> <i class="fa fa-sign-out"></i> Sign Out </span> <% end %> </li> 会删除重复项,因此这表示您在UNION *列中有一个或多个文件ID的重复值。

Street更改为UNION