upload Attachments to SQL server 2014 (1 Viewer)

DevAccess

Registered User.
Local time
Today, 05:45
Joined
Jun 27, 2016
Messages
321
Hi

I have few files on my local folders, now i would like to move these files to sql server 2014 tables ( data type of destination table could be blob, nvarchar(max), varibinary(max), I am not sure what shall i opt in this case ).

is there anyway that VBA from Access or SQL could do this ?

If so any code snippet would be handy.

Thanks

Man
 

DevAccess

Registered User.
Local time
Today, 05:45
Joined
Jun 27, 2016
Messages
321
I am getting below error 3001
"Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another"
on line number
adoCon.CursorLocation = adUseClient

I am using excel vba.

Sub SaveAsBinary()

Dim adoStream As Object
Dim adoCmd As Object
Dim strFilePath As String
Dim adoCon As Object
Const strDB As String = "" 'Database name
Const strServerName As String = "" 'Server Name

Set adoCon = CreateObject("ADODB.Connection")
Set adoStream = CreateObject("ADODB.Stream")
Set adoCmd = CreateObject("ADODB.Command")

'--Open Connection to SQL server
adoCon.CursorLocation = adUseClient
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:45
Joined
May 7, 2009
Messages
19,237
you are using late bound for your objects. Subst 3 to adUseClient or better set Reference to Microsoft ActiveX Data Object 2.8 or 6.1
 

DevAccess

Registered User.
Local time
Today, 05:45
Joined
Jun 27, 2016
Messages
321
you are using late bound for your objects. Subst 3 to adUseClient or better set Reference to Microsoft ActiveX Data Object 2.8 or 6.1
thanks but now it gives same error on
.CommandType = adCmdText
Code:
 Dim adoStream               As Object
    Dim adoCmd                  As Object
    Dim strFilePath             As String
    Dim adoCon                  As Object
    Const strDB                 As String = "CSIData8thJune2018&1934PM"  'Database name
    Const strServerName         As String = "DESKTOP-LEJN6P1"  'Server Name
    
    Set adoCon = CreateObject("ADODB.Connection")
    Set adoStream = CreateObject("ADODB.Stream")
    Set adoCmd = CreateObject("ADODB.Command")
    
    '--Open Connection to SQL server
  adoCon.CursorLocation = adUseClient
    adoCon.Open "Provider=SQLOLEDB;Data Source=" & strServerName & ";Initial Catalog = " & strDB & ";Integrated Security=SSPI;"
    '----
    
    strFilePath = "C:\Lotus Notes work\AttachmentExtracted\6CNLY1_1.PNG" ' File to upload
    
    adoStream.Type = adTypeBinary
    adoStream.Open
    adoStream.LoadFromFile strFilePath 'It fails if file is open
        
    With adoCmd
        .CommandText = "INSERT INTO tblFiles VALUES (?,?)" ' Query
        .CommandType = adCmdText
        
        '---adding parameters
       ' .Parameters.Append .CreateParameter("@Id", adInteger, adParamInput, 0, 1)
        .Parameters.Append .CreateParameter("@FileNameAtt", adVarBinary, adParamInput, adoStream.Size, adoStream.Read)
        '---
    End With
    
    adoCmd.ActiveConnection = adoCon
    adoCmd.Execute
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:45
Joined
May 7, 2009
Messages
19,237
Just add the Reference on vbe. Tools->Reference, scroll until you find it.
 

DevAccess

Registered User.
Local time
Today, 05:45
Joined
Jun 27, 2016
Messages
321
Just add the Reference on vbe. Tools->Reference, scroll until you find it.
it is already there, please see attached file.
 

Attachments

  • Capture.PNG
    Capture.PNG
    8.1 KB · Views: 50

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:45
Joined
May 7, 2009
Messages
19,237
Subst 1 to it
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:45
Joined
May 7, 2009
Messages
19,237
You have the wring ref. select the other one withou "Recordset".
 

jleach

Registered User.
Local time
Today, 08:45
Joined
Jan 4, 2012
Messages
308
BTW, your adVarBinary will need to be adLongVarBinary otherwise you'll get a truncation error from the server.

(I just did this yesterday, borrowed the same bit of code)
 

jleach

Registered User.
Local time
Today, 08:45
Joined
Jan 4, 2012
Messages
308
Try opening the db with the shift key held so you can remove the 6.0 reference and replace with 6.1. Sounds like something is using it, but that shouldn't be the case unless you have active code running somewhere. (not sure what breaking changes may have been between the two versions, so be careful of that).

And you'll need a field list on your insert statement as well.
 

Users who are viewing this thread

Top Bottom