2016-11-16 113 views
1

我需要此案例的幫助。我需要從Excel導入表格到SQL Server,我有兩列DATE類型。從Excel導入到SQL Server時的日期數據類型轉換錯誤

問題是,當運行宏,顯示以下信息從字符串

在Excel表中包含的日期這種格式交談日期和/或時間時

轉換失敗dd/mm/YYYY和SQL Server列的數據類型爲DATE。請幫我解決這個問題。感謝您的支持。

連接的代碼在VBA

'Create Conection for MySQL Database in AR_TELCEL 
Dim conn As New ADODB.Connection 
conn.CommandTimeout = 0 
'Define Data Types to Cells for Upload a SQL Database 
Dim iRowNo As Long 
Dim CUST_ID As Long, DOC_TYPE As String, BILL_DOC As Long, REFERENCE As String, STATUS_PAYMENT As String, INV_CON As String, OSALES_FOUR As String, INV_REF As String, DOC_NUM As Long, GL As Long, DOC_DATE As Date, DUE_DATE As Date, DOC_CURRENCY As String, DOC_AMNT As Currency, LOC_AMNT As Currency, PAY_TERM As String, POST_K As String, LINE_N As Long 



    With Sheets("I. One Report Sap") 

     '_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
     'Connect with MSSQL PCC AR_TELCEL Database 
     '_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

     conn.Open "Provider=SQLOLEDB;Data Source=146.250.36.68,1433;Initial Catalog=AR_TELCEL;User Id=user;Password=Password;" 

     'Omitimos la linea de encabezados 
     iRowNo = 10 

     'Loop until empty cell in CustomerId 
     Do Until .Cells(iRowNo, 1) = "" 
      ACCOUNT = .Cells(iRowNo, 1) 
      DOC_TYPE = .Cells(iRowNo, 2) 
      BILL_DOC = .Cells(iRowNo, 3) 
      REFERENCE = .Cells(iRowNo, 4) 
      STATUS_PAYMENT = .Cells(iRowNo, 5) 
      INV_CON = .Cells(iRowNo, 6) 
      ORDER_SALES = .Cells(iRowNo, 7) 
      INV_REF = .Cells(iRowNo, 8) 
      DOC_NUM = .Cells(iRowNo, 9) 
      GL = .Cells(iRowNo, 10) 
      DOC_DATE = .Cells(iRowNo, 11) 
      DUE_DATE = .Cells(iRowNo, 12) 
      DOC_CURRENCY = .Cells(iRowNo, 13) 
      DOC_AMNT = .Cells(iRowNo, 14) 
      LOC_AMNT = .Cells(iRowNo, 15) 
      PAY_TERM = .Cells(iRowNo, 16) 
      POST_K = .Cells(iRowNo, 17) 
      LINE_N = .Cells(iRowNo, 18) 
+2

這是你真正的'sa'密碼嗎? – cha

+2

請勿使用'sa'用戶。期。 –

+0

嘗試使用DATEFORMAT DMY; – ZLK

回答

0

我可以修復蒙山這一點。

原始代碼:

conn.Execute "insert into one_rep_sap_temp (ACCOUNT, DOC_TYPE, BILL_DOC, REFERENCE, STATUS_PAYMENT, INV_CON, OSALES_FOUR, INV_REF, DOC_NUM, GL, DOC_DATE, DUE_DATE, DOC_CURRENCY, DOC_AMNT, LOC_AMNT, PAY_TERM, POST_K, LINE_N) values ('" & ACCOUNT & "', '" & DOC_TYPE & "', '" & BILL_DOC & "', '" & REFERENCE & "', '" & STATUS_PAYMENT & "', '" & INV_CON & "', '" & OSALES_FOUR & "', '" & INV_REF & "', '" & DOC_NUM & "', '" & GL & "', '" & DOC_DATE & "', '" & DUE_DATE & "', '" & DOC_CURRENCY & "', '" & DOC_AMNT & "', '" & LOC_AMNT & "', '" & PAY_TERM & "', '" & POST_K & "', '" & LINE_N & "')" 

新代碼:

conn.Execute "insert into one_rep_sap_temp (ACCOUNT, DOC_TYPE, BILL_DOC, REFERENCE, STATUS_PAYMENT, INV_CON, OSALES_FOUR, INV_REF, DOC_NUM, GL, DOC_DATE, DUE_DATE, DOC_CURRENCY, DOC_AMNT, LOC_AMNT, PAY_TERM, POST_K, LINE_N) values ('" & ACCOUNT & "', '" & DOC_TYPE & "', '" & BILL_DOC & "', '" & REFERENCE & "', '" & STATUS_PAYMENT & "', '" & INV_CON & "', '" & OSALES_FOUR & "', '" & INV_REF & "', '" & DOC_NUM & "', '" & GL & "', '" & Format(DOC_DATE, "YYYY-MM-DD") & "', '" & Format(DUE_DATE, "YYYY-MM-DD") & "', '" & DOC_CURRENCY & "', '" & DOC_AMNT & "', '" & LOC_AMNT & "', '" & PAY_TERM & "', '" & POST_K & "', '" & LINE_N & "')" 

的解決方法是這樣

格式(DOC_DATE, 「YYYY-MM-DD」)

感謝對你對這個問題的幫助。 :D

相關問題