ADO stream concert to binary
up vote
1
down vote
favorite
This is the code that I use to upload file into database using VBA, but this loop can only run once. During the second loop it will give error, the error is
Operation is not allowed in this context
Can anybody suggest me what to do?
The error is in the line adoStream.Type = adTypeBinary
Dim stCon As String 'SQL Connection string
Dim stProcName As String 'Stored Procedure name
Dim strCmd As String
Dim adoStream As Object
Dim adocmd As Object
Dim strFilePath As String
Dim adoCon As Object
Set adoCon = CreateObject("ADODB.Connection")
Set adoStream = CreateObject("ADODB.Stream")
Set adocmd = CreateObject("ADODB.Command")
adoCon.CursorLocation = adUseClient
adoCon.Open "Provider=SQLOLEDB; " & _
"Data Source=#########; " & _
"Initial Catalog=#######;" & _
"User ID=#######; Password=########;"
For Each fl In fld.Files
If fl.name Like Mask Then
strInput = fl.name
strFilePath = fld.path & "" & fl.name
MsgBox (strFilePath)
adoStream.Type = adTypeBinary
adoStream.Open
adoStream.LoadFromFile strFilePath 'It fails if file is open
With adocmd
.CommandText = "INSERT INTO dbo.coc_upload(Data) " & _
"VALUES (?)"
.CommandType = adCmdText
'---adding parameters
.Parameters.Append .CreateParameter("@Data", adVarBinary, adParamInput, adoStream.Size, adoStream.Read)
'---
End With
adocmd.ActiveConnection = adoCon
adocmd.Execute
adoCon.Close
MsgBox ("Done")
End If
Next
excel vba excel-vba
|
show 6 more comments
up vote
1
down vote
favorite
This is the code that I use to upload file into database using VBA, but this loop can only run once. During the second loop it will give error, the error is
Operation is not allowed in this context
Can anybody suggest me what to do?
The error is in the line adoStream.Type = adTypeBinary
Dim stCon As String 'SQL Connection string
Dim stProcName As String 'Stored Procedure name
Dim strCmd As String
Dim adoStream As Object
Dim adocmd As Object
Dim strFilePath As String
Dim adoCon As Object
Set adoCon = CreateObject("ADODB.Connection")
Set adoStream = CreateObject("ADODB.Stream")
Set adocmd = CreateObject("ADODB.Command")
adoCon.CursorLocation = adUseClient
adoCon.Open "Provider=SQLOLEDB; " & _
"Data Source=#########; " & _
"Initial Catalog=#######;" & _
"User ID=#######; Password=########;"
For Each fl In fld.Files
If fl.name Like Mask Then
strInput = fl.name
strFilePath = fld.path & "" & fl.name
MsgBox (strFilePath)
adoStream.Type = adTypeBinary
adoStream.Open
adoStream.LoadFromFile strFilePath 'It fails if file is open
With adocmd
.CommandText = "INSERT INTO dbo.coc_upload(Data) " & _
"VALUES (?)"
.CommandType = adCmdText
'---adding parameters
.Parameters.Append .CreateParameter("@Data", adVarBinary, adParamInput, adoStream.Size, adoStream.Read)
'---
End With
adocmd.ActiveConnection = adoCon
adocmd.Execute
adoCon.Close
MsgBox ("Done")
End If
Next
excel vba excel-vba
I suspect the error is actually happening on the next line. You haven't closed the adoStream.
– Jon Vote
Nov 22 at 0:25
When I close the adoStream it gives error, Operation is not allowed when the object is close
– syah
Nov 22 at 0:31
Hmmm - ok was hoping that would help...have you tried moving the open statement above the loop?
– Jon Vote
Nov 22 at 0:36
I finally can get the code run, just have to modfiy this
– syah
Nov 22 at 0:37
Have to declare this code inside the loop
– syah
Nov 22 at 0:37
|
show 6 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
This is the code that I use to upload file into database using VBA, but this loop can only run once. During the second loop it will give error, the error is
Operation is not allowed in this context
Can anybody suggest me what to do?
The error is in the line adoStream.Type = adTypeBinary
Dim stCon As String 'SQL Connection string
Dim stProcName As String 'Stored Procedure name
Dim strCmd As String
Dim adoStream As Object
Dim adocmd As Object
Dim strFilePath As String
Dim adoCon As Object
Set adoCon = CreateObject("ADODB.Connection")
Set adoStream = CreateObject("ADODB.Stream")
Set adocmd = CreateObject("ADODB.Command")
adoCon.CursorLocation = adUseClient
adoCon.Open "Provider=SQLOLEDB; " & _
"Data Source=#########; " & _
"Initial Catalog=#######;" & _
"User ID=#######; Password=########;"
For Each fl In fld.Files
If fl.name Like Mask Then
strInput = fl.name
strFilePath = fld.path & "" & fl.name
MsgBox (strFilePath)
adoStream.Type = adTypeBinary
adoStream.Open
adoStream.LoadFromFile strFilePath 'It fails if file is open
With adocmd
.CommandText = "INSERT INTO dbo.coc_upload(Data) " & _
"VALUES (?)"
.CommandType = adCmdText
'---adding parameters
.Parameters.Append .CreateParameter("@Data", adVarBinary, adParamInput, adoStream.Size, adoStream.Read)
'---
End With
adocmd.ActiveConnection = adoCon
adocmd.Execute
adoCon.Close
MsgBox ("Done")
End If
Next
excel vba excel-vba
This is the code that I use to upload file into database using VBA, but this loop can only run once. During the second loop it will give error, the error is
Operation is not allowed in this context
Can anybody suggest me what to do?
The error is in the line adoStream.Type = adTypeBinary
Dim stCon As String 'SQL Connection string
Dim stProcName As String 'Stored Procedure name
Dim strCmd As String
Dim adoStream As Object
Dim adocmd As Object
Dim strFilePath As String
Dim adoCon As Object
Set adoCon = CreateObject("ADODB.Connection")
Set adoStream = CreateObject("ADODB.Stream")
Set adocmd = CreateObject("ADODB.Command")
adoCon.CursorLocation = adUseClient
adoCon.Open "Provider=SQLOLEDB; " & _
"Data Source=#########; " & _
"Initial Catalog=#######;" & _
"User ID=#######; Password=########;"
For Each fl In fld.Files
If fl.name Like Mask Then
strInput = fl.name
strFilePath = fld.path & "" & fl.name
MsgBox (strFilePath)
adoStream.Type = adTypeBinary
adoStream.Open
adoStream.LoadFromFile strFilePath 'It fails if file is open
With adocmd
.CommandText = "INSERT INTO dbo.coc_upload(Data) " & _
"VALUES (?)"
.CommandType = adCmdText
'---adding parameters
.Parameters.Append .CreateParameter("@Data", adVarBinary, adParamInput, adoStream.Size, adoStream.Read)
'---
End With
adocmd.ActiveConnection = adoCon
adocmd.Execute
adoCon.Close
MsgBox ("Done")
End If
Next
excel vba excel-vba
excel vba excel-vba
edited Nov 22 at 7:18
Pᴇʜ
19.9k42650
19.9k42650
asked Nov 22 at 0:20
syah
63
63
I suspect the error is actually happening on the next line. You haven't closed the adoStream.
– Jon Vote
Nov 22 at 0:25
When I close the adoStream it gives error, Operation is not allowed when the object is close
– syah
Nov 22 at 0:31
Hmmm - ok was hoping that would help...have you tried moving the open statement above the loop?
– Jon Vote
Nov 22 at 0:36
I finally can get the code run, just have to modfiy this
– syah
Nov 22 at 0:37
Have to declare this code inside the loop
– syah
Nov 22 at 0:37
|
show 6 more comments
I suspect the error is actually happening on the next line. You haven't closed the adoStream.
– Jon Vote
Nov 22 at 0:25
When I close the adoStream it gives error, Operation is not allowed when the object is close
– syah
Nov 22 at 0:31
Hmmm - ok was hoping that would help...have you tried moving the open statement above the loop?
– Jon Vote
Nov 22 at 0:36
I finally can get the code run, just have to modfiy this
– syah
Nov 22 at 0:37
Have to declare this code inside the loop
– syah
Nov 22 at 0:37
I suspect the error is actually happening on the next line. You haven't closed the adoStream.
– Jon Vote
Nov 22 at 0:25
I suspect the error is actually happening on the next line. You haven't closed the adoStream.
– Jon Vote
Nov 22 at 0:25
When I close the adoStream it gives error, Operation is not allowed when the object is close
– syah
Nov 22 at 0:31
When I close the adoStream it gives error, Operation is not allowed when the object is close
– syah
Nov 22 at 0:31
Hmmm - ok was hoping that would help...have you tried moving the open statement above the loop?
– Jon Vote
Nov 22 at 0:36
Hmmm - ok was hoping that would help...have you tried moving the open statement above the loop?
– Jon Vote
Nov 22 at 0:36
I finally can get the code run, just have to modfiy this
– syah
Nov 22 at 0:37
I finally can get the code run, just have to modfiy this
– syah
Nov 22 at 0:37
Have to declare this code inside the loop
– syah
Nov 22 at 0:37
Have to declare this code inside the loop
– syah
Nov 22 at 0:37
|
show 6 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422246%2fado-stream-concert-to-binary%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I suspect the error is actually happening on the next line. You haven't closed the adoStream.
– Jon Vote
Nov 22 at 0:25
When I close the adoStream it gives error, Operation is not allowed when the object is close
– syah
Nov 22 at 0:31
Hmmm - ok was hoping that would help...have you tried moving the open statement above the loop?
– Jon Vote
Nov 22 at 0:36
I finally can get the code run, just have to modfiy this
– syah
Nov 22 at 0:37
Have to declare this code inside the loop
– syah
Nov 22 at 0:37