首页 > 解决方案 > 通过 VBA 访问添加到现有的直通查询

问题描述

我很好奇是否可以在不更改当前内置内容的情况下添加到现有的直通查询。

例子; 如果我有一个包含以下代码的直通查询:

SELECT MailYear, offer, CompanyCode, SeasonFlag, Season_Id, Description_Full, EarlyBirdDate, Description, Price_Type, Offer_Type, Page_Count, FirstReleaseMailed, InternetActiveDate
from CatCov
where offer = 'VG'and MailYear in ('2021', '2020')

我想在下一行添加一些东西。这是可能的吗?如果是这样,是否有人有任何 VBA 示例,他们应该向我展示或指出我的方向?

我在尝试

db.QueryDefs.Delete "qryMaster"

If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst

strSELECT = "select MailYear, offer, CompanyCode, SeasonFlag, Season_Id, Description_Full, EarlyBirdDate, Description, Price_Type, Offer_Type, Page_Count, FirstReleaseMailed, InternetActiveDate " _
    & "from CatCov where offer = 'VG'and MailYear in ('2021', '2020') "

strSQL1 = strSELECT

    Set qdfPassThrough = db.CreateQueryDef("qryMaster")
    qdfPassThrough.Connect = "ODBC;DSN=SupplyChainMisc;Description=SupplyChainMisc;Trusted_Connection=Yes;DATABASE=SupplyChain_Misc;"
    qdfPassThrough.ReturnsRecords = True
    qdfPassThrough.SQL = strSQL

rs.MoveNext
Do Until rs.EOF = True

strUNION = " Union SELECT MailYear, offer, CompanyCode, SeasonFlag, Season_Id, Description_Full, EarlyBirdDate, Description, Price_Type, Offer_Type, Page_Count, FirstReleaseMailed, InternetActiveDate " _
    & "from CatCov where offer = 'TK' "

strSQL2 = StrUnion

qdfPassThrough.SQL = strSQL

rs.MoveNext
Loop

Else
    MsgBox "No records"
End If

但这行不通。由于我有一个循环运行它If Not (rs.EOF And rs.BOF) Then说“对象变量或未设置块变量”时出错。这是因为我添加了 With 语句,否则没关系。

我正在尝试设置一个循环,该循环将不断添加到直通查询,同时不影响已经存在的内容。

任何帮助,将不胜感激。

标签: vbams-access

解决方案


推荐阅读