I try call this stored procedure in SQL Server that inserts data.
I have this:
CREATE PROCEDURE [dbo].[AddUser]
#user_id bigint,
#user_name varchar(20)
AS
BEGIN
INSERT INTO [users] ([id], [name])
VALUES (#user_id, #user_name)
END
and I have this ASP code:
<%
Const adCmdStoredProc = 4
Const adBigInt = 20
Const adParamInput = 1
Const adVarChar = 200
Set conn = Server.CreateObject("ADODB.Connection")
connectionstring = "Provider=SQLOLEDB.1;Password=***********;Persist Security Info=True;User ID=sa;Initial Catalog=Test;Data Source=AREAWEB2-PC"
conn.Open connectionstring
Set cmd = Server.CreateObject("ADODB.Command")
Set cmd.ActiveConnection = conn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "{call AddUser (?,?)}"
frm_id = 1
frm_name = "Carlos"
Set prmUserId = cmd.CreateParameter("#user_id", adBigInt, adParamInput, 0, frm_id)
Set prmUserName = cmd.CreateParameter("#user_name", adVarChar, adParamInput, 20, frm_name)
Cmd.Parameters.Append prmUserId
Cmd.Parameters.Append prmUserName
Set rs = cmd.Execute
%>
But I get this error message:
Microsoft OLE DB Provider for SQL Server error '80040e10'
What is wrong ?
If you Google the error code 80040e10 it becomes apparent quickly that the issue is one of the following to do with your parameter definitions;
From Why do I get 80040E10 errors?
Microsoft OLE DB Provider for SQL Server error '80040e10'
Procedure '' expects parameter '', which was not supplied.
Your issue is likely due to passing a 0 size in your #user_id parameter, as all parameters have to pass a maximum size either using .CreateParameter() or before appending a parameter to the collection using .Size property.
Update:
As the OP has pointed out the issue is actually due to the wrong CommandType property value being set.
Thought it would be worth expanding the answer to explain why. The reason is the CommandType in the OP's example is set to adCmdStoredProc but the .CommandText property is not set to a Stored Procedure name, the easiest way to correct this (instead of removing the .CommandType) is to change the .CommandText as follows;
cmd.CommandText = "AddUser"
The other option is to change the .CommandType to adCmdText which will accept the format {call AddUser (?,?)}.
cmd.CommandType = adCmdText
Internally ADO uses adCmdText for the adCmdStoredProc CommandTypeEnum as the .CommmandText will get changed to the format {call AddUser (?,?)}, the difference is you cannot use this form yourself unless you specify .CommandType = adCmdText. There is no additional benefits to using either one, but in my opinion adCmdStoredProc just simplifies the calls and hides the nuts and bolts from you.
The code fails because I have these incorrect:
cmd.CommandType = adCmdStoredProc
Removing it, works.
Related
I am trying to pass an "image type data" as a parameter (LongDescription(image,null) as declared in SQL) in my SQL stored procedure like this:
ALTER PROCEDURE [dbo].[spselConvertImageTEST]
#ReportedByCountry nvarchar(10) OUTPUT,
#ReportId nvarchar(10) OUTPUT,
#LongDescription image OUTPUT
And I am trying to receive it in access VBA like this:
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = CurrentProject.Connection
.CommandText = "dbo.spselConvertImageTEST"
.CommandType = adCmdStoredProc
.CommandTimeout = 300
.Parameters.Append .CreateParameter("ReportedByCountry", adVarChar, adParamOutput, 10)
.Parameters.Append .CreateParameter("ReportId", adVarChar, adParamOutput, 10)
.Parameters.Append .CreateParameter("LongDescription", adBinary, adParamOutput)
.Execute
End With
When trying to create the third parameter I receive this error:
Run-time error '3708'
Parameter object is improperly defined. Inconsistent or incomplete information was provided.
Can anybody be so kind as to help me out here, please?
Thanks for your answers I decided to do it this way:
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
rs.Open "dbo.spselConvertImageTEST", CurrentProject.Connection
With rs
.MoveFirst
Do While Not .EOF
Me.oleLongDescription = .Fields("LongDescription")
.MoveNext
Loop
End With
Gord Thompson thanks for your last suggestion too but unfortunately it did not work either I received the following error:
Run-time error '-2147024882(8007000e)
No enough storage is available to complete this operation.
I have an ADO command object in VBA running and returning values from a stored procedure (in SQL Server). To validate the SP and command lines in VBA, I've used the CopyFromRecordset method to view the data and everything seems fine.
Set ADOComm = New ADODB.Command
With ADOComm
.ActiveConnection = ADOConn
.CommandType = adCmdStoredProc
.CommandText = "GenerateMasterSumIfs"
.Parameters.Append .CreateParameter("ImportFilePath", adVarChar, adParamInput, 100, TextFileSavePath)
End With
Set ADORec = New ADODB.Recordset
Set ADORec = ADOComm.Execute
I'd now like to be able to navigate the returned records using FIND or GETROWS (for example) but the recordset appears to have no data (recordset.RecordCount returns -1). I've tried to research this online and have seen references to cursor types being restricted depending on the source (in my case, SQL Server) but haven't been able to find a solution that I can understand and use.
So, my question(s), specifically, are:
Can I continue to use the ADO Command/Recordset combination to collate my data then 'navigate' it? OR
Do I need to run the SP using a different method to enable the navigation I require?
I'm no expert in this field, so would appreciate your patience with my technical descriptions and any site etiquette faux pas.
The solution I needed was the CursorLocation property of the ADO Connection object. Changing it to adUseClient has allowed me to move the cursor and use methods such as FIND and GETROWS as I required.
Set ADOConn = New ADODB.Connection
ADOConn.CursorLocation = adUseClient
ADOConn.Open "Driver={SQL Server Native Client 11.0};Server=ServerName;Database=DBName;Trusted_Connection=yes;"
Set ADOComm = New ADODB.Command
With ADOComm
.ActiveConnection = ADOConn
.CommandType = adCmdStoredProc
.CommandText = "GenerateMasterSumIfs"
.Parameters.Append .CreateParameter("ImportFilePath", adVarChar, adParamInput, 100, TextFileSavePath)
.Parameters.Append .CreateParameter("MTFilePath", adVarChar, adParamInput, 100, PathToMT)
End With
Set ADORec = New ADODB.Recordset
Set ADORec = ADOComm.Execute
I am using Access 2010 and SQL server 2008. I have to capture an attachment in one of my forms and move the attachment to SQL server. Server is remote and I am not allowed to provide path of the file relative to server.
Here are things I tried without any success:
1.Tried using ADODB.Stream but I get error when i use convert function(to convert the data into varbinary) after getting file value using .Read function of the object
2. Tried creating an attachment type in my form and inserted the attachment in a local access DB table with data type as Attachment. I could not move the data from this table to SQL server table.
Any pointers or solution will be of great help.
Thank you
I got it figured out. Thank you for all the suggestion guys.
All I did was create a ADODB.Stream object, read the file and passed it to a proc in sql server which inserts the data.
The code for inserting the data looks something like this
Dim conn As ADODB.Connection
Dim cmd As ADODB.Command
Dim coll As Collection
Set conn = New ADODB.Connection
Set coll = New Collection
stConnect = GetADOConnectionString()
conn.ConnectionString = stConnect
conn.Open
If conn.State = 1 Then ' open
Set cmd = New ADODB.Command
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "proc_insert_review_doc"
cmd.ActiveConnection = conn
coll.Add cmd.CreateParameter("#LoanID", adVarChar, adParamInput, 10, LoanId)
coll.Add cmd.CreateParameter("#WorkStreamID", adVarChar, adParamInput, 10, WorkstreamID)
coll.Add cmd.CreateParameter("#QuestionSetID", adVarChar, adParamInput, 10, QuestionSetID)
coll.Add cmd.CreateParameter("#FileValue", adVarBinary, adParamInput, , FileValue)
For Each coll_i In coll
cmd.Parameters.Append (coll_i)
Next coll_i
cmd.Execute
Set cmd = Nothing
conn.Close
Else
MsgBox "Failed to open a connection to the database server!", vbCritical
End If
Set conn = Nothing
End Function
i'll do somthing like that, it is looking easyier to me to use a record set...
dim Conn as ADODB.Connection
dim RS as ADODB.RecordSet
dim binObj as ADODB.Stream
Conn.ConnectionString="Provider=SQLOLEDB;Persist Security Info=False;User ID=sa;Password=;Initial Catalog=khc405;Network Library=dbmssocn; Data Source=db1;"
Conn.Open
Set RS = New ADODB.Recordset
sql = "SELECT * FROM SOMETABLE WHERE FILENAME='HOWTO.PDF'
RS.Open sql, GLBcn, adOpenDynamic, adLockOptimistic
If Not (RS.BOF And RS.eof) Then
Set binObj = New ADODB.Stream
binObj.Type = adTypeBinary
binObj.Open
binObj.LoadFromFile (App.Path & "\SomeFolder\" & ''HOWTO.PDF''
RS!FILEDATA = binObj.Read
RS!FileName ='HOWTO.PDF'
RS.Update
binObj.Close
Set binObj = Nothing
End If
i get this from-
http://www.sqlservercentral.com/Forums/Topic243427-169-1.aspx
good luck
I have an Access DB that has a bunch of linked tables from a SQL Server database. The Access DB calls a stored procedure on the SQL Server database that updates data on a form.
Dim sql As String
Dim cnn As ADODB.Connection
Set cnn = New ADODB.Connection
cnn.ConnectionString = "DSN=Records"
cnn.CommandTimeout = 90
cnn.Open
sql = "exec myStoredProcedure #param1=" & Me.txtParam1Field & ", #param2=" & Me.txtParam2Field
cnn.Execute sql
Set cnn = Nothing
frmMyForm.Requery
When I run this it either times out, if the CommandTimeout value isn't long enough, or it executes, but doesn't actually execute myStoredProcedure for some reason. If I take the string sql and past it into Sql Server Manager, myStoredProcedure executes in less than a second and everything works great.
I've tried debugging over this code in Access, but I'm not getting any useful results when I step over cnn.Execute sql.
Depending on the values of txtParam1Field and txtParam2Field you probably want to enclose the values with single quote like so:
sql = "exec myStoredProcedure #param1='" & Me.txtParam1Field & "', #param2='" & Me.txtParam2Field & "'"
If we take your original code and assume that txtParam1Field is equal to 1 and txtParam2Field is equal to John then your generated sql will not execute because it will look like this:
exec myStoredProcedure #param1=1, #param2=John
Your best bet is to output the value of "sql" variable in debug window and run that exact statement in sql query manager. That will tell you exactly where the problem is if it's malformed SQL.
You could try setting the Prepared property to false on the command object. This causes a recompile of the procedure before execution, but could result in a better plan depending on the parameters that are sent.
Dim sql As String
Dim cnn As ADODB.Connection
Dim Cmd As ADODB.Command
Set cnn = New ADODB.Connection
cnn.ConnectionString = "DSN=Records"
cnn.CommandTimeout = 90
cnn.Open
sql = "exec myStoredProcedure #param1=" & Me.txtParam1Field & ", #param2=" & Me.txtParam2Field
Set Cmd = New ADODB.Command
Set Cmd.ActiveConnection = cnn
Cmd.CommandType = adCmdText
Cmd.CommandText = sql
Cmd.Prepared = False
Cmd.CommandTimeout = 300
Cmd.Execute
I have an MS Access application that contains all tables linked to SQL Server, so in MS Access VBA code or query I work with those tables very simple, I access them via name, like [Customers].
Also I have a stored procedure in SQL Server called sp_CopyData which I need to call from my VBA code. How can I do that without creating new connection to SQL Server (I already have it somewhere!? because I have access to tables)?
Or it's impossible? Appreciate any help. Thanks!
The right answer found out, it should be like:
Dim qdef As DAO.QueryDef
Set qdef = CurrentDb.CreateQueryDef("")
qdef.Connect = CurrentDb.TableDefs("[ANY LINKED TABLE TO MS SQL SERVER]").Connect
qdef.SQL = "EXEC sp_CopyData"
qdef.ReturnsRecords = False ''avoid 3065 error
qdef.Execute
Create a pass-through query, and you can then use this through the WHOLE application anytime you need to execute some T-SQL.
The code this becomes:
With CurrentDb.QueryDefs("qPass")
.SQL = "exec sp_copydata"
.ReturnsRecords = False ''avoid 3065 error
.Execute
End With
The code in MS Access works for me:
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
cmd.ActiveConnection = "Provider=SQLOLEDB.1;Persist Security Info=False;Initial Catalog=[DB];Data Source=[PC];Integrated Security=SSPI;"
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "sp_CopyData"
cmd.Parameters.Append cmd.CreateParameter("#param", adVarChar, adParamInput, 255, param)
cmd.Execute
Try:
CurrentProject.Connection.Execute "EXEC sp_CopyData"
References: http://msdn.microsoft.com/en-us/library/office/ff821478(v=office.14).aspx