简体   繁体   中英

Store large data to SQL database

I am building an excel application where the Audit sheet has the form (that the users fills the data into it). I have another sheet called Data_upload which saves the information that is filled in the Audit worksheet.

I'm stuck in situation where data doesn't gets uploaded from Data_upload to my SQL table when there are more than 100 characters.

What modifications can I do to save the data irrespective of the data length?

'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

About what length are you talking about? If you need to have 255 characters, then you can use Text type of field in Access. Otherwise you have to use Memo. The limitation is not in your VBA code, but in the database. Check this for data limits of data types in Access and additional info how to change the type of field is here.

If you are using SQL server, then similar applies. You have to check the data type of column you are trying to store long text to, if it is not limited to, let's say, 100 characters. Change the type VARCHAR(MAX) for really long texts.

There are actually potentially 2 issues here.

1) As Ondrej Holman pointed out the receiving data type in the SQL table may be the issue and you need to check that. However whenever possible use NVARCHAR over VARCHAR as that is more forwardly compatible.

2) The second issue could be in the code depending on what your using to code this in because as you build the String you have to keep in mind that the String may have a maximum buffer capacity that you might exceed even before you exceed the database field that you have. For instance in VBA the String length maximum buffer capacity is @255 characters which means as your comment field grows in length the available space you have for the rest of the Insert variables and the Insert command itself shrinks. If this is the case then Insert all the other data first then Update the record with the comment later by itself --- assuming the comments maximum length is still not going to cause issues --- in which case figure out how much String space for the Update of the comment assuming a 1 character comment and see how much space remains and that +1 is the maximum length of your comment without doing some special finagling.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM