2016-04-28 15 views
0

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')

+0

Was ist die Fehlermeldung, die Sie erhalten? Und haben Sie überprüft, ob Ihre Verbindungszeichenfolge korrekt ist? – Leviathan

+0

Laufzeitfehler '3146' und dann markiert es "SET MyRS = MyQry.OpenRecordSet() // Ja Verbindungszeichenfolge ist korrekt. – SQUISH

+0

Komm schon, es sagt sicherlich etwas mehr als nur '3146'? – Leviathan

Antwort

1

Scheint, wie Sie heimlich zu einer Oracle-Datenbank verbinden. Dann Datumswerte müssen yyyy-mm-dd String-Ausdrücke formatiert werden:

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

oder versuchen, das andere Datumsformat:

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

Danke, ich habe das versucht, bekomme aber trotzdem der gleiche Fehler. Ich bin neu bei VBA, was bedeutet "Set MyRS = MyQry.OpenRecordset()"? – SQUISH

+0

Es bedeutet, ein Recordset mit MyQry zu öffnen. – Gustav

+0

Vielleicht brauchen Sie das andere Datumsformat - siehe Bearbeiten. – Gustav

Verwandte Themen