VBA运行时错误

时间:2016-04-28 17:34:36

标签: vba access-vba access

此代码上的运行时错误说' Set Myrs = myqry.openrecordsheet()是错误,有什么想法吗?

我有一个带有此代码的按钮:

选项比较数据库

Private Sub Command5_Click()

GetDataFromDate DateStart.Value, DateEnd.Value

End Sub

然后我有了这个vba:

Option Compare Database


Sub GetDataFromDate(dtStart As Date, dtEnd As Date)

 Dim MyDb As Database, MyQry As QueryDef, MyRS  As Recordset
 Set MyDb = CurrentDb()
 Set MyQry = MyDb.CreateQueryDef("")

 ' Type a connect string using the appropriate values for your
 ' server.
 MyQry.Connect = "ODBC;DSN=PRDDLPA1;UID=purch_edi1;PWD=useedi;DBQ=PRDDLPA1;DBA=W;APA=T;EXC=F;FEN=T;QTO=T;FRC=10;FDL=10;LOB=T;RST=T;BTD=F;BNF=F;BAM=IfAllSuccessful;NUM=NLS;DPM=F;MTS=T;MDI=F;CSR=F;FWC=F;FBS=64000;TLO=O;MLD=0;ODA=F;"

 ' Set the SQL query with Date Range passed to the sub as parameters
 MyQry.SQL = "SELECT NDC_CODE, SUM(INVOICE_QTY) AS TOTAL_QTY, SUM(INVOICE_QTY*UNIT_PRICE) AS EXT_DOLLAR_TOTAL, ITEM_NBR, TO_CHAR(EDI_INVOICE_DT, 'YYYYMM') " & _
 "FROM EDI_INVOICE_DETAIL_LINE" & _
 "WHERE AP_VENDOR_NBR IN('   242081') AND NOT((EDI_INVOICE_TYPE = 'C') AND (UNIT_PRICE = 0))" & _
 "AND (EDI_INVOICE_DT BETWEEN #" & dtStart & "# AND #" & dtEnd & "# " & _
 "AND LOCATION_NBR NOT IN('88017',' 88003',' 88010',' 88011',' 88018',' 88012',' 88008',' 88006',' 88001',' 88007',' 88009',' 88004',' 88019')" & _
" GROUP BY NDC_CODE, ITEM_NBR, TO_CHAR(EDI_INVOICE_DT, 'YYYYMM')"

 MyQry.ReturnsRecords = True
 Set MyRS = MyQry.OpenRecordset()

 MyRS.MoveFirst

 If Not MyRS.BOF Then

    While Not MyRS.EOF

        Debug.Print MyRS!TO_CHAR(EDI_INVOICE_DT)
        Debug.Print MyRS!NDC_CODE
        Debug.Print MyRS!TOTAL_QTY
        Debug.Print MyRS!EXT_DOLLAR_TOTAL
        Debug.Print MyRS!ITEM_NBR


        MyRS.MoveNext
    Wend


 End If

 MyQry.Close
 MyRS.Close
 MyDb.Close

End Sub

如何解决这个问题,以便我可以通过带有新日期范围的表单更新我的传递查询?

<小时/> [来自评论]原始查询如下:

SELECT NDC_CODE, SUM(INVOICE_QTY) AS TOTAL_QTY, SUM(INVOICE_QTY*UNIT_PRICE) AS EXT_DOLLAR_TOTAL, ITEM_NBR, TO_CHAR(EDI_INVOICE_DT, 'YYYYMM') FROM EDI_INVOICE_DETAIL_LINE WHERE AP_VENDOR_NBR IN(' 242081') AND NOT((EDI_INVOICE_TYPE = 'C') AND (UNIT_PRICE = 0)) AND (EDI_INVOICE_DT BETWEEN TO_DATE('03/01/2016', 'MM/DD/YYYY') AND TO_DATE('03/31/2016', 'MM/DD/YYYY')) AND LOCATION_NBR NOT IN('88017',' 88003',' 88010',' 88011',' 88018',' 88012',' 88008',' 88006',' 88001',' 88007',' 88009',' 88004',' 88019') GROUP BY NDC_CODE, ITEM_NBR, TO_CHAR(EDI_INVOICE_DT, 'YYYYMM')

1 个答案:

答案 0 :(得分:1)

好像你秘密连接到Oracle数据库。然后日期值必须格式化为 yyyy-mm-dd 字符串表达式:

"AND (EDI_INVOICE_DT BETWEEN '" & Format(dtStart, "yyyy-mm-dd") & "' AND '" & Format(dtEnd, "yyyy-mm-dd") & "' " & _

或尝试其他日期格式:

"AND (EDI_INVOICE_DT BETWEEN '" & Format(dtStart, "mm/dd/yyyy") & "' AND '" & Format(dtEnd, "mm/dd/yyyy") & "' " & _