I need to upload a file (<10 MB) around once a week to a SQL Server 2016 database on a remote server in the same network. Until now it was all within a Access FE/BE but I want to migrate to SQL Server as backend.
The attachments I had in MS Access so need to be handled now on the SQL database as I do not want to do this on a fileshare.
I found many threads about using something like this from SQLShack
DECLARE @File varbinary(MAX);
SELECT
@File = CAST(bulkcolumn AS varbinary(max))
FROM
OPENROWSET(BULK 'C:\sqlshack\akshita.png', SINGLE_BLOB) as MyData;
INSERT INTO DemoFileStreamTable_1
VALUES (NEWID(), 'Sample Picture', @File)
This works when I start the query within SSMS on the SQL Server itself and the file is already accessible by the server on its local drive.
But when I try to put this in my VBA code on my Access frontend computer:
Sub DaoOdbcExample()
Dim cdb As DAO.Database, qdf As DAO.QueryDef
Set cdb = CurrentDb
Set qdf = cdb.CreateQueryDef("")
qdf.Connect = "ODBC;" & _
"Driver={SQL Server};" & _
"Server=MyServer;" & _
"Database=MyDatabase;" & _
"Trusted_Connection=yes;"
qdf.SQL = "DECLARE @File varbinary(MAX); SELECT @File = CAST(bulkcolumn as varbinary(max)) FROM OPENROWSET(BULK 'D:\SomeFile.pdf', SINGLE_BLOB) as MyData; INSERT INTO DemoFileStreamTable_1 VALUES ( NEWID(), 'Test PDF', @File)"
qdf.ReturnsRecords = False
qdf.Execute dbFailOnError
Set qdf = Nothing
Set cdb = Nothing
End Sub
I just get an error
ODBC--call failed
Other simple "Select" statements seem to work, so the connection itself seems okay.
So my questions are:
How can I perform such an upload from a local file on computer A to the remote SQL server on computer B (which cannot directly access this file) using MS Access as my frontend?
Is there a different way not using the "BULK" statement as I need "bulkadmin" rights for all users then?
I may have found a solution using the links from @AlwaysLearning. The first sub actually answers my question to upload a file to a remote FILESTREAM SQL Server. The second sub downloads all uploaded files into a given directory.