0

I am building an application that uses SQL Server Express as backend and a frontend of MS Access. I am trying to store small pdf files in the database using the varbinary(max) column type. I am connecting to the database using VBA.

Here is my code:

Dim cn, rs  As Object
Dim sql, strCnxn, FileToUpload, FileName As String

Dim fso As Object
Set fso = VBA.CreateObject("Scripting.FileSystemObject")

FileToUpload = CustOpenFileDialog
if FileToUpload <> "" Then
    FileName = fso.GetFileName(FileToUpload) 'get only filename + extension
         
    'SQL Connection
     strCnxn = ""
         
     Set cn = CreateObject("ADODB.Connection")
     cn.Open strCnxn
         
     'Recordset
     sql = "InvoiceFiles" 'Table to add file
     Set rs = CreateObject("ADODB.Recordset")
    
     rs.Open sql, strCnxn, 1, 3  '1 - adOpenKeyset, 3 - adLockOptimistic"
         
     'Create Stream to upload File as BLOB data
     Dim strm As Object
     Set strm = CreateObject("ADODB.Stream")
     strm.Type = 1
     strm.Open
     strm.LoadFromFile FileToUpload
        
     rs.AddNew
     rs!InvoiceID = CInt(Me.InvoiceID.Value)
     rs.Fields("FileData").Value = strm.Read
     rs!FileName = FileName
     strm.Close
     rs.Update

I am getting this error:

Error

I think that the error is from conflicting datatypes but I am not sure.

I had found this question which my code is based off of. https://stackoverflow.com/questions/71448275/ms-access-sql-server-vba-upload-local-file-to-filestream-on-remote-sql-serv

Any help is appreciated.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
DoShawhan
  • 1
  • 1

0 Answers0