将列表框搜索结果发送到表

时间:2011-06-29 23:29:15

标签: ms-access access-vba

好的,所以我痛苦地制作了这段代码,并且它可以工作,但是它经过几百次迭代后很慢并且崩溃了。我有一个多列表框显示数据库搜索的结果。我想获取这些结果并将其放在名为“MailingListTemp”的临时表中,单击命令按钮。有没有办法让这个更快,而不是崩溃或替代方法?最终结果是将列表导出为ex​​cel。

感谢。这是我的代码:

    Private Sub AllToMailingListCommand_Click()
        Dim i As Variant
        Dim Item As Variant
        Dim var0Search As Variant
        Dim var1Search As Variant
        Dim var2Search As Variant
        Dim var3Search As Variant
        Dim var4Search As Variant
        Dim var5Search As Variant
        Dim var6Search As Variant
        Dim var7Search As Variant
        Dim var8Search As Variant
        Dim var9Search As Variant
        Dim var10Search As Variant
        Dim var11Search As Variant
        Dim var12Search As Variant
        Dim strX As String
        Dim DValue As String
        DValue = " "
        DValue = Format(date, "mm/dd/yyyy")

        Dim dbThisDatabase As DAO.Database
        Dim rstMailingListTemp As DAO.Recordset

        Set dbThisDatabase = CurrentDb
        Set rstMailingListTemp = dbThisDatabase.OpenRecordset("MailingListTemp")


        For i = 0 To Me.List0.ListCount

        Me.List0.Selected(i) = True
        For Each Item In List0.ItemsSelected

        var0Search = List0.Column(0, Item)
        var1Search = List0.Column(1, Item)
        var2Search = List0.Column(4, Item)
        var3Search = List0.Column(5, Item)
        Next

        var4Search = DLookup("[FirstName]", "[Master Marketing List]", "[MMLID] = " & var0Search)
        var5Search = DLookup("[LastName]", "[Master Marketing List]", "[MMLID] = " & var0Search)
        var6Search = DLookup("[Zip]", "[Master Marketing List]", "[MMLID] = " & var0Search)
        var7Search = DLookup("[Address1]", "[Master Marketing List]", "[MMLID] = " & var0Search)
        var8Search = DLookup("[Address2]", "[Master Marketing List]", "[MMLID] = " & var0Search)
        var9Search = DLookup("[Address3]", "[Master Marketing List]", "[MMLID] = " & var0Search)
        var10Search = DLookup("[Prefix]", "[Master Marketing List]", "[MMLID] = " & var0Search)
        var11Search = DLookup("[Suffix]", "[Master Marketing List]", "[MMLID] = " & var0Search)
        var12Search = DLookup("[Title]", "[Master Marketing List]", "[MMLID] = " & var0Search)

        rstMailingListTemp.AddNew
        rstMailingListTemp("ID").value = var0Search
        rstMailingListTemp("CompanyName").value = var1Search
        rstMailingListTemp("City").value = var2Search
        rstMailingListTemp("State").value = var3Search
        rstMailingListTemp("FirstName").value = var4Search
        rstMailingListTemp("LastName").value = var5Search
        rstMailingListTemp("Zip").value = var6Search
        rstMailingListTemp("Address1").value = var7Search
        rstMailingListTemp("Address2").value = var8Search
        rstMailingListTemp("Address3").value = var9Search
        rstMailingListTemp("Prefix").value = var10Search
        rstMailingListTemp("Suffix").value = var11Search
        rstMailingListTemp("Title").value = var12Search

        rstMailingListTemp.Update

        Next i

    End Sub

1 个答案:

答案 0 :(得分:1)

您说“显示数据库搜索结果的多列表框”。如何将搜索过滤到列表框中?您是否使用各种搜索条件动态创建SQL Where子句?如果是这样,那么忽略rstMailingListTemp表以及该子例程中的所有代码。

而是使用Automation和CopyFromRecordset函数将附加到查询的Where子句的结果直接发送到您的邮件列表表中。

Modules: Transferring Records to Excel with Automation

已添加:创建一个类似于

的字符串变量

“SELECT * From MailingListTable WHERE”& (在此处插入您的Where子句)

然后将其放入转移记录到Excel代码中。

或者使用docmd.TransferSpreadsheet函数来实现这一目标。这可能足以满足您的要求。请注意,tablename参数可以是包含SELECT查询的字符串表达式。