2016-07-14 116 views
0

我正在構建一個excel應用程序,其中審覈表具有表單(即用戶將數據填充到該表單中)。我有另一張名爲Data_upload,它保存了在審計工作表中填寫的信息。將大量數據存儲到SQL數據庫

我被困在數據沒有從Data_upload 上載到我的SQL表中超過100個字符時。

無論數據長度如何,我可以做些什麼來保存數據?

'Opens the SQL server 

dbs.Open "Data Source =; Initial Catalog = ;Trusted_connection = Yes; Provider = ;; Integrated Security=SSPI;" 

dbs.Execute "INSERT INTO Acdbo. CHECKLIST([FileTime], [FileName], [AccName], [EffDate], [PolicyType], [Premium], [Underwriter], [Auditor],[UT_Score],[Underwriter_Score]) " _ 
      & "VALUES ('" & FileTime & "','" & FileName & "','" & AccName & "','" & EffDate & "','" & policy_type & "','" & premium_amt & "','" & UW_Name & "','" & Aud & "','" & ut * 100 & "','" & uw_score * 100 & "')" 

Set rcd = dbs.Execute(_ 
     "SELECT Acdbo.AUDIT_CHECKLIST.FileID " _ 
     & "FROM Acdbo.AUDIT_CHECKLIST " _ 
     & " WHERE Acdbo.AUDIT_CHECKLIST.FileTime = " & Chr(39) & FileTime & Chr(39) _ 
     & " AND Acdbo.AUDIT_CHECKLIST. FileName = " & Chr(39) & FileName & Chr(39)) 


If rcd.EOF Then 
    MsgBox "Error", vbCritical 
End 
End If 

rcd.MoveFirst 
FileID = rcd!FileID 
rcd.Close 


Dim iRowNo As Integer 
Dim sLabel As String 
Dim sData As String 
Dim sAdditionalComments As String 
'Dim sLink As String 

    With Sheets("Data_upload") 

    'Skip the header row 
    iRowNo = 2 

    'Loop until empty cell in CustomerId 
    Do Until .Cells(iRowNo, 2) = "" 

     sLabel = .Cells(iRowNo, 2) 
     sData = .Cells(iRowNo, 4) 
     sAdditionalComments = .Cells(iRowNo, 5) 
     'sLink = .Cells(iRowNo, 6) 

     'Generate and execute sql statement to import the excel rows to SQL Server table 
     dbs.Execute "Insert into Acdbo. CHECKLIST_DATA([FileID], [Label], [Data], [AdditinalComments]) values ('" & FileID & "', '" & sLabel & "', '" & sData & "','" & sAdditionalComments & "')" 
     On Error Resume Next 

     iRowNo = iRowNo + 1 
    Loop 
End With 


    endTime = Timer 
    dbs.Execute "UPDATE Acdbo. CHECKLIST SET [UploadTime] = " & endTime - startTime & " WHERE FileID = " & FileID  'Upload the time it takes to upload Checklist 
    dbs.Close 

Dim Response As VbMsgBoxResult 

Response = MsgBox("File Uploaded", vbOKOnly, "Database Upload") 
End 

'The following block of code provide procedures once an error occurs 
Error_Handler: 

'Upon error, hide RDT's "DatabaseExtract" tab and lock down Audit checklist's structure 
'ActiveWorkbook.Sheets("DatabaseExtract").Visible = False 
'ActiveWorkbook.Protect Structure:=True, Windows:=False, password:=pwd_WorkBook 

'Then display with the error message and exit the macro 
MsgBox "Error " & Err.Number & ": " & Err.Description & " in " & Application.VBE.ActiveCodePane.CodeModule, vbOKOnly, "Error" 

Application.ScreenUpdating = False 

End Sub 
+0

您可以提供完整的Excel子程序VBA塊,因爲它的一部分被切斷,並且'Error_Handler'如何集成?並請清理未聲明的「End」行。 – Parfait

+0

另外,在兩個查詢中使用了多少個字符串列,哪些字段沒有被填充?你是否收到錯誤?請在編輯的文章中包含這些信息。 – Parfait

回答

0

關於你說的是什麼長度?如果你需要有255個字符,那麼你可以在Access中使用文本類型的字段。否則,你必須使用備忘錄。限制不在您的VBA代碼中,而在數據庫中。檢查這for data limits of data types in Access和其他信息how to change the type of field在這裏。

如果您使用的是SQL Server,則類似的情況適用。您必須檢查您嘗試存儲長文本的列的數據類型,如果它不限於,例如,100個字符。將類型VARCHAR(MAX)更改爲非常長的文本。

+0

目前我正在使用這個數據庫中的列。 Data和AdditinalComments列需要存儲大型文本數據[Data] [nvarchar](max)NULL,[AdditinalComments] [nvarchar](max)NULL, – Swathi

0

這裏實際上存在2個問題。

1)由於Ondrej Holman指出SQL表中的接收數據類型可能是問題,您需要檢查該數據類型。但是,只要有可能,請使用NVARCHAR over VARCHAR,因爲它更向前兼容。

2)第二個問題可能在代碼中,具體取決於您使用的編碼方式,因爲在構建字符串時必須牢記字符串可能具有最大緩衝區容量,甚至可能會超過你超過了你擁有的數據庫字段。例如在VBA中,字符串長度最大緩衝區容量爲@ 255個字符,這意味着您的註釋字段的長度將增加其他插入變量的可用空間,並且插入命令本身會縮小。如果是這種情況,那麼首先插入所有其他數據,然後用自己後面的註釋更新記錄---假設註釋的最大長度仍然不會引起問題---在這種情況下計算出多少字符串空間假設1個字符評論的評論更新,看看還有多少空間,並且+1是您評論的最大長度,而不做一些特別的討論。