我正在构建一个excel应用程序,其中审计表具有表单(用户将数据填入其中)。我有另一张名为 Data_upload 的工作表,它保存了审计工作表中填写的信息。
我遇到的情况是数据无法从 Data_upload 上传 当有超过100个字符时到我的SQL表。
无论数据长度如何,我都可以进行哪些修改来保存数据?
'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 :(得分:0)
你在说什么长度?如果您需要255个字符,则可以在Access中使用文本类型的字段。否则你必须使用备忘录。限制不在您的VBA代码中,而是在数据库中。选中此for data limits of data types in Access,其他信息how to change the type of field就在这里。
如果您使用的是SQL Server,则类似的情况适用。你必须检查你试图存储长文本的列的数据类型,如果它不限于,比方说100个字符。为真正长文本更改类型VARCHAR(MAX)。
答案 1 :(得分:0)
这里实际上有两个问题。
1)正如Ondrej Holman所指出的,SQL表中的接收数据类型可能是问题,您需要检查它。但是,只要有可能,就使用NVARCHAR over VARCHAR,因为它更向前兼容。
2)第二个问题可能在代码中,取决于你使用什么来编码它,因为在构建String时你必须记住,String可能有一个你可能超过的最大缓冲容量,甚至在你之前超过您拥有的数据库字段。例如,在VBA中,字符串长度最大缓冲区容量为@ 255个字符,这意味着当您的注释字段长度增加时,您可以为其余的Insert变量增加可用空间,并且Insert命令本身会缩小。如果是这种情况,则先插入所有其他数据,然后单独更新记录及其后面的注释---假设注释最大长度仍然不会导致问题---在这种情况下计算出多少字符串空间假设1个字符的评论更新评论并查看剩余的空间,并且+1是评论的最大长度,而不进行特殊的重新评估。