我正在构建一个excel应用程序,其中审核表有表单(用户将数据填入其中)。我有另一个名为Data_upload的工作表,它保存在审核工作表中填写的信息。
当有超过100个字符时,数据无法从Data_upload上传到我的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
发布于 2016-07-14 11:57:16
你说的是多长时间?如果需要255个字符,则可以在Access中使用字段的文本类型。否则你就得用备忘录。限制不是在您的VBA代码中,而是在数据库中。检查这个用于访问中数据类型的数据限制。和附加信息如何更改字段类型在这里。
如果您使用的是SQL服务器,则应用类似的方法。如果不限于100个字符,则必须检查要将长文本存储到的列的数据类型。将VARCHAR(MAX)类型更改为非常长的文本。
发布于 2016-07-14 15:49:12
实际上这里有两个潜在的问题。
1)正如Ondrej Holman指出的那样,SQL表中的接收数据类型可能是问题所在,您需要检查它。然而,只要有可能,就使用NVARCHAR而不是VARCHAR,因为这是更向前兼容的。
2)第二个问题可能是代码中的问题,这取决于您使用什么来对其进行编码,因为在构建字符串时,您必须记住,该字符串的最大缓冲区容量甚至可能超过您所拥有的数据库字段。例如,在VBA中,字符串长度最大缓冲区容量为@255个字符,这意味着随着您的注释字段长度的增加,您对其余插入变量和Insert命令本身的可用空间会缩小。如果是这样的话,那么先插入所有其他数据,然后用注释本身更新记录--假设注释的最大长度仍然不会引起问题--在这种情况下,计算假设一个字符注释的注释更新的字符串空间,看看还有多少空格,而+1是您的注释的最大长度,而不做一些特殊的修饰。
https://stackoverflow.com/questions/38373460
复制相似问题