2016-07-14 59 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是您评论的最大长度,而不做一些特别的讨论。