Laufzeitfehler für diesen Code sagt 'Set Myrs = myqry.openrecordsheet() ist der Fehler, irgendwelche Ideen?VBA Laufzeitfehler
Ich habe einen Knopf mit diesem Code:
Option Compare Database
Private Sub Command5_Click()
GetDataFromDate DateStart.Value, DateEnd.Value
End Sub
dann habe ich vba mit diesem:
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
Wie kann ich dieses Problem beheben, so dass ich aktualisieren meine Abfrage über mein Formular mit einem neuen Datumsbereich?
[Von Kommentar] die ursprüngliche Abfrage wie folgt aussieht:
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')
Was ist die Fehlermeldung, die Sie erhalten? Und haben Sie überprüft, ob Ihre Verbindungszeichenfolge korrekt ist? – Leviathan
Laufzeitfehler '3146' und dann markiert es "SET MyRS = MyQry.OpenRecordSet() // Ja Verbindungszeichenfolge ist korrekt. – SQUISH
Komm schon, es sagt sicherlich etwas mehr als nur '3146'? – Leviathan