通过集合访问VBA循环,为每个项目创建SQL语句

时间:2015-02-12 23:47:59

标签: vba ms-access collections

我正在尝试编写一个代码来创建一个关联ID集合(Associates是我的集合的名称)。在任何给定时间都有10名员工,但收集将根据谁在本月做了什么工作而改变。所以一旦收集完成,我想循环遍历它并为每个项目创建一个SQL语句。有点像这样:

        For Each Item In Associates
            qryTopSQL = "SELECT TOP " & QA# & _
            " Date, ID, [L#], Deal, RndNum FROM tbl_Data WHERE Date Between #" & _
            StartDate & "# And #" & EndDate & "# AND ID = " & Associates() & _
            " ORDER BY RndNum"
        Next Item

所以我最终得到了很多SQL字符串,但我遇到了这个问题:

  1. 我是否正确地写了ID = " & Associates() & "部分?
  2. 如何命名这些不同的字符串,以便我以后可以访问它们?
  3. 一旦完成这些,我想对所有SQL字符串进行UNION ALL查询。我该怎么做?
  4. 如果可以请帮助,非常感谢。我是收藏和数组的新手,我不了解我在网上找到的一些东西。

    编辑更新: 我试过这个:

    j = 1
    k = 1
    For Each Item In Associates
        If j = 1 And k = 1 Then
            qryTopString1 = "SELECT * FROM qryTopSQL_" & k
        Else
            qryTopString2 = " UNION ALL SELECT * FROM qryTopSQL_" & k
        End If
    j = j + 1
    k = k + 1
    Next Item
    
    '
    Set qryTopUnionqdef = CurrentDb.CreateQueryDef("qryTopSQLUnion", qryTopString1 & qryTopString2)
    

    但是生成的查询是第一个和最后一个TopSQL之间的联合,而中间没有。显然这个循环就是问题,但我无法弄清楚到目前为止要做什么。

1 个答案:

答案 0 :(得分:0)

在Access中,有两种方法可以创建查询对象:VBA查询(代码中)或存储查询(使用功能区,向导或导航栏)。

基本上,你想要做到这两点。因此,为了将VBA SQL字符串迁移到实际存储的查询对象,必须使用QueryDefs。下面是如何迭代以动态创建10个Associates查询和一个联合查询。

Dim qryTopqdef As QueryDef, qryTopUnionqdef As QueryDef, findqdf As QueryDef
Dim i as Integer, j as Integer

' DELETE QUERIES IF EXIST
For each findqdf in CurrentDb.Querydefs
    If Instr(findqdf.Name, "qryTopSQL") > 0 Then
        db.QueryDefs.delete(findqdf.Name)
    End if
Next findqdf

' INDIVIDUAL 10 QUERIES
i = 1
For Each Item In Associates

    qryTopSQL = "SELECT TOP " & QA# & _
            " Date, ID, [L#], Deal, RndNum FROM tbl_Data WHERE Date Between #" & _
            StartDate & "# And #" & EndDate & "# AND ID = " & Item & _
            " ORDER BY RndNum"

    ' QUERY NAMES ARE SUFFIXED BY THE ITERATOR COUNT
    Set qryTopqdef = CurrentDb.CreateQueryDef("qryTopSQL_" & i, qryTopSQL)
    i = i + 1
Next Item


' UNION QUERY
j = 1
For Each Item In Associates
    If j = 1 Then
        qryTopSQL = "SELECT TOP " & QA# & _
                " Date, ID, [L#], Deal, RndNum FROM tbl_Data WHERE Date Between #" & _
                StartDate & "# And #" & EndDate & "# AND ID = " & Item & _
                " ORDER BY RndNum"
    Else
        ' UNIONS ARE SIMPLY STACKS OF SELECT STATEMENTS OF SAME COLUMN NUMBER AND DATA TYPE
        ' TOGETHER JOINED BY THE UNION OR UNION ALL CLAUSE
        qryTopSQL = qryTopSQL & " UNION SELECT TOP " & QA# & _
                " Date, ID, [L#], Deal, RndNum FROM tbl_Data WHERE Date Between #" & _
                StartDate & "# And #" & EndDate & "# AND ID = " & Item & _
                " ORDER BY RndNum"
    End if
    j = j + 1
Next Item

Set qryTopUnionqdef = CurrentDb.CreateQueryDef("qryTopSQLUnion", qryTopSQL)


' UNINTIALIZE OBJECTS
Set qryTopqdef = nothing
Set qryTopUnionqdef = nothing

另外 - 请参阅集合与数组的SO post