系统资源超出 - Ms Access中的直通查询和VBA

时间:2018-01-30 15:05:45

标签: sql ms-access access-vba

我编写了下面的代码,它将excel文件导入Access数据库,从中获取所有采购订单编号,将它们引入查询条件,将此条件传递给直通查询,然后执行查询。如果PO的数量有限,则一切正常,但是当有多个时,系统会在将条件传递给查询定义时返回错误超出系统资源。有趣的是,实际的查询条件(无论有多少PO)会传递给查询,但脚本执行会停止。

考虑到我无法在底层Oracle数据库中创建表(理想情况下我可以使用PO编号创建一个表并进行连接),我可以采取哪些措施来改进脚本并避免此错误?

谢谢。

Public Function ImportGoodPOs()
On Error GoTo ImportGoodPOs_Err

'This function imports the spreadsheet with te POs
Dim tbl As String, fd As FileDialog, fn As String, qry As String, tbl1 As String, tbl2 As String, db As DAO.Database, rst As DAO.Recordset, qString As String, qdExtData As QueryDef, qry2 As String, pos As String, sql As String
tbl = "tbl_TempPos"
tbl2 = "tbl_PoNumbers"
qry = "qry_GoodPosWithVendorDetails"
qry2 = "qry_GoodsPosWithVendorDetailsPQ"
tbl1 = "tbl_GoodPosWithVendorDetails"
Set fd = Application.FileDialog(msoFileDialogFilePicker)

fd.AllowMultiSelect = False

MsgBox "Browse for the PO file to import...", vbOKOnly, "Attention!"

If fd.Show = True Then
    If fd.SelectedItems(1) <> vbNullString Then
        fn = fd.SelectedItems(1)
    End If
Else
    'Exit code if no file is selected
    End
End If

DoCmd.SetWarnings False
DoCmd.DeleteObject acTable, tbl
DoCmd.TransferSpreadsheet acImport, , tbl, fn, -1

' setting a dummy query definition to avoid future errors
sql = "SELECT APPS.PO_HEADERS_ALL.SEGMENT1 FROM APPS.PO_HEADERS_ALL WHERE APPS.PO_HEADERS_ALL.SEGMENT1 = '201410377'"
CurrentDb.QueryDefs(qry2).sql = sql

Set rst = CurrentDb.OpenRecordset(tbl)
sql = "SELECT DISTINCT " & tbl & ".[PO Number]"
sql = sql & "INTO " & tbl2 & " "
sql = sql & "FROM " & tbl

DoCmd.RunSQL sql

Set rst = CurrentDb.OpenRecordset(tbl2)

rst.MoveFirst
pos = rst("PO Number")
qString = "(APPS.PO_HEADERS_ALL.SEGMENT1 = '" & pos & "')"

For X = 2 To rst.RecordCount
    rst.MoveNext
    pos = rst("PO Number")
    qString = qString & " OR (APPS.PO_HEADERS_ALL.SEGMENT1 = '" & pos & "')"
Next X

sql = "SELECT APPS.PO_HEADERS_ALL.PO_HEADER_ID, APPS.PO_HEADERS_ALL.SEGMENT1, APPS.AP_SUPPLIER_SITES_ALL.ADDRESS_LINE1, APPS.AP_SUPPLIER_SITES_ALL.ADDRESS_LINES_ALT, APPS.AP_SUPPLIER_SITES_ALL.ADDRESS_LINE2, APPS.AP_SUPPLIER_SITES_ALL.ADDRESS_LINE3, APPS.AP_SUPPLIER_SITES_ALL.CITY, APPS.AP_SUPPLIER_SITES_ALL.STATE, APPS.AP_SUPPLIER_SITES_ALL.ZIP, APPS.AP_SUPPLIER_SITES_ALL.PROVINCE, APPS.AP_SUPPLIER_SITES_ALL.COUNTRY, APPS.AP_SUPPLIER_SITES_ALL.AREA_CODE, APPS.AP_SUPPLIER_SITES_ALL.PHONE, APPS.AP_SUPPLIER_SITES_ALL.FAX, APPS.AP_SUPPLIER_SITES_ALL.FAX_AREA_CODE, APPS.AP_SUPPLIER_SITES_ALL.TELEX, APPS.AP_SUPPLIER_SITES_ALL.PAYMENT_METHOD_LOOKUP_CODE, APPS.AP_SUPPLIER_SITES_ALL.BANK_ACCOUNT_NAME, APPS.AP_SUPPLIER_SITES_ALL.BANK_ACCOUNT_NUM, APPS.AP_SUPPLIER_SITES_ALL.BANK_NUM, APPS.AP_SUPPLIER_SITES_ALL.BANK_ACCOUNT_TYPE, APPS.AP_SUPPLIER_SITES_ALL.TERMS_DATE_BASIS, APPS.AP_SUPPLIER_SITES_ALL.CURRENT_CATALOG_NUM, APPS.AP_SUPPLIER_SITES_ALL.VAT_CODE, "
sql = sql & "APPS.AP_SUPPLIER_SITES_ALL.INVOICE_AMOUNT_LIMIT, APPS.AP_SUPPLIER_SITES_ALL.PAY_DATE_BASIS_LOOKUP_CODE, APPS.AP_SUPPLIER_SITES_ALL.ALWAYS_TAKE_DISC_FLAG, APPS.AP_SUPPLIER_SITES_ALL.INVOICE_CURRENCY_CODE, APPS.AP_SUPPLIER_SITES_ALL.PAYMENT_CURRENCY_CODE, APPS.AP_SUPPLIER_SITES_ALL.HOLD_ALL_PAYMENTS_FLAG, APPS.AP_SUPPLIER_SITES_ALL.HOLD_FUTURE_PAYMENTS_FLAG, APPS.AP_SUPPLIER_SITES_ALL.HOLD_REASON, APPS.AP_SUPPLIER_SITES_ALL.HOLD_UNMATCHED_INVOICES_FLAG, APPS.AP_SUPPLIER_SITES_ALL.AP_TAX_ROUNDING_RULE, APPS.AP_SUPPLIER_SITES_ALL.AUTO_TAX_CALC_FLAG, APPS.AP_SUPPLIER_SITES_ALL.AUTO_TAX_CALC_OVERRIDE, APPS.AP_SUPPLIER_SITES_ALL.AMOUNT_INCLUDES_TAX_FLAG, APPS.AP_SUPPLIER_SITES_ALL.EXCLUSIVE_PAYMENT_FLAG, APPS.AP_SUPPLIER_SITES_ALL.TAX_REPORTING_SITE_FLAG, APPS.AP_SUPPLIER_SITES_ALL.VAT_REGISTRATION_NUM, APPS.AP_SUPPLIER_SITES_ALL.OFFSET_VAT_CODE, APPS.AP_SUPPLIER_SITES_ALL.CHECK_DIGITS, APPS.AP_SUPPLIER_SITES_ALL.BANK_NUMBER, APPS.AP_SUPPLIER_SITES_ALL.ADDRESS_LINE4, "
sql = sql & "APPS.AP_SUPPLIER_SITES_ALL.COUNTY, APPS.AP_SUPPLIER_SITES_ALL.ADDRESS_STYLE, APPS.AP_SUPPLIER_SITES_ALL.EDI_TRANSACTION_HANDLING, APPS.AP_SUPPLIER_SITES_ALL.EDI_ID_NUMBER, APPS.AP_SUPPLIER_SITES_ALL.EDI_PAYMENT_METHOD, APPS.AP_SUPPLIER_SITES_ALL.EDI_PAYMENT_FORMAT, APPS.AP_SUPPLIER_SITES_ALL.EDI_REMITTANCE_METHOD, APPS.AP_SUPPLIER_SITES_ALL.BANK_CHARGE_BEARER, APPS.AP_SUPPLIER_SITES_ALL.EDI_REMITTANCE_INSTRUCTION, APPS.AP_SUPPLIER_SITES_ALL.BANK_BRANCH_TYPE, APPS.AP_SUPPLIER_SITES_ALL.PAY_ON_CODE, APPS.AP_SUPPLIER_SITES_ALL.DEFAULT_PAY_SITE_ID, APPS.AP_SUPPLIER_SITES_ALL.PAY_ON_RECEIPT_SUMMARY_CODE, APPS.AP_SUPPLIER_SITES_ALL.PCARD_SITE_FLAG, APPS.AP_SUPPLIER_SITES_ALL.MATCH_OPTION, APPS.AP_SUPPLIER_SITES_ALL.COUNTRY_OF_ORIGIN_CODE, APPS.AP_SUPPLIER_SITES_ALL.FUTURE_DATED_PAYMENT_CCID, APPS.AP_SUPPLIER_SITES_ALL.CREATE_DEBIT_MEMO_FLAG, APPS.AP_SUPPLIER_SITES_ALL.OFFSET_TAX_FLAG, APPS.AP_SUPPLIER_SITES_ALL.SUPPLIER_NOTIF_METHOD, APPS.AP_SUPPLIER_SITES_ALL.EMAIL_ADDRESS, "
sql = sql & "APPS.AP_SUPPLIER_SITES_ALL.REMITTANCE_EMAIL, APPS.AP_SUPPLIER_SITES_ALL.PRIMARY_PAY_SITE_FLAG, APPS.AP_SUPPLIER_SITES_ALL.SHIPPING_CONTROL, APPS.AP_SUPPLIER_SITES_ALL.SELLING_COMPANY_IDENTIFIER, APPS.AP_SUPPLIER_SITES_ALL.GAPLESS_INV_NUM_FLAG, APPS.AP_SUPPLIER_SITES_ALL.DUNS_NUMBER, APPS.AP_SUPPLIER_SITES_ALL.RETAINAGE_RATE, APPS.AP_SUPPLIER_SITES_ALL.TCA_SYNC_STATE, APPS.AP_SUPPLIER_SITES_ALL.TCA_SYNC_PROVINCE, APPS.AP_SUPPLIER_SITES_ALL.TCA_SYNC_COUNTY, APPS.AP_SUPPLIER_SITES_ALL.TCA_SYNC_CITY, APPS.AP_SUPPLIER_SITES_ALL.TCA_SYNC_ZIP, APPS.AP_SUPPLIER_SITES_ALL.TCA_SYNC_COUNTRY, APPS.AP_SUPPLIER_SITES_ALL.PAY_AWT_GROUP_ID, APPS.AP_SUPPLIER_SITES_ALL.CAGE_CODE, APPS.AP_SUPPLIER_SITES_ALL.LEGAL_BUSINESS_NAME, APPS.AP_SUPPLIER_SITES_ALL.DOING_BUS_AS_NAME, APPS.AP_SUPPLIER_SITES_ALL.DIVISION_NAME, APPS.AP_SUPPLIER_SITES_ALL.SMALL_BUSINESS_CODE, APPS.AP_SUPPLIER_SITES_ALL.CCR_COMMENTS, APPS.AP_SUPPLIER_SITES_ALL.DEBARMENT_START_DATE, APPS.AP_SUPPLIER_SITES_ALL.DEBARMENT_END_DATE "
sql = sql & "FROM APPS.PO_HEADERS_ALL INNER JOIN APPS.AP_SUPPLIER_SITES_ALL ON (APPS.PO_HEADERS_ALL.VENDOR_ID = APPS.AP_SUPPLIER_SITES_ALL.VENDOR_ID AND APPS.PO_HEADERS_ALL.VENDOR_SITE_ID = APPS.AP_SUPPLIER_SITES_ALL.VENDOR_SITE_ID) "
sql = sql & "WHERE " & qString

qString = ""

CurrentDb.QueryDefs(qry2).sql = sql

sql = ""

DoCmd.OpenQuery qry2, acViewNormal, acEdit
DoCmd.OpenQuery qry, acViewNormal, acEdit
DoCmd.Close acQuery, qry2, acSaveYes
DoCmd.Close acQuery, qry, acSaveYes
DoCmd.OpenTable tbl1, acViewNormal, acEdit
DoCmd.SetWarnings True

1 个答案:

答案 0 :(得分:1)

您是否尝试过使用ODBC链接到Oracle APPS.AP_SUPPLIER_SITES_ALLWHERE表?你应该有权限这样做。这将绕过与直通查询相关的问题。只需将ODBC链接表连接到从Excel派生的PO编号表即可。

(原始答案) 我认为安德烈遇到了这个问题:Barattolo_67报告查询的长度为335,229个字符。访问规范说最大查询大小约为64,000个字符。所以Barattolo_67显然超过了这个限制,这很可能是系统资源超出错误的原因。

请注意,查询只返回89个字段,因此这不是问题。查询的SELECT部分​​只有4,000个字符。最大的问题是COMMIT子句,它增加了另外300,000个字符。