473,804 Members | 3,462 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ADO, adCmdText, parameters

Hello.

I know how to call a parameterized stored procedure by using ADODB command
object and parameters,
but how can I execute the same query using adCmdText instead of
adCmdStoredProc ?
Namely I created the CommandText string with declare statement for
parameters before the select statement. No error is displayed, but also, no
records are shown iether. It seems that the server can't recognize
parameters. The select statement is exactly the same as it was in the stored
procedure...
The code is somethingh like this:

Option Compare Database
Private Sub BLABLA()

Dim cmd As ADODB.Command
Dim par As ADODB.Parameter
Dim fld As ADODB.Field
Dim rs As ADODB.Recordset
Dim str As String

Dim PARAMETER1 As New ADODB.Parameter
Dim PARAMETER2 As New ADODB.Parameter
Dim PARAMETER3 As New ADODB.Parameter

Dim rst As ADODB.Recordset

Set cmd = New ADODB.Command

cmd.ActiveConne ction = "Provider=SQLOL EDB.1;Data Source=(local); " &
"Integrated Security=SSPI;I nitial Catalog=Databas eName"

cmd.CommandText = "DECLARE @PARAMETER1 datetime, @PARAMETER2 datetime,
@PARAMETER3 bit;" & _
"SELECT * FROM blah, blah.... " & _
"WHERE something>= @PARAMETER3 AND " & _
"something BETWEEN @PARAMETER1 AND @PARAMETER2"

cmd.CommandType = adCmdText

Set PARAMETER1 = cmd.CreateParam eter("@PARAMETE R1", adDate, adParamInput)
cmd.Parameters. Append PARAMETER1
PARAMETER1.Valu e = Format("01.01.2 000", "yyyy-mm-dd")

Set PARAMETER2 = cmd.CreateParam eter("@PARAMETE R2", adDate, adParamInput)
cmd.Parameters. Append PARAMETER2
PARAMETER2.Valu e = Format("01.01.2 007", "yyyy-mm-dd")

Set PARAMETER3 = cmd.CreateParam eter("@PARAMETE R3", adInteger, adParamInput)
cmd.Parameters. Append PARAMETER3
PARAMETER3.Valu e = 0

Set rst = New ADODB.Recordset
Set rst = cmd.Execute()

Do Until rst.EOF
str = ""
For Each fld In rst.Fields
str = str & fld.Value & Chr(9)

Next fld
Debug.Print str
rst.MoveNext
Loop

Set rst = Nothing
Set par = Nothing
Set cmd = Nothing
Set PARAMETER1 = Nothing
Set PARAMETER2 = Nothing
Set PARAMETER3 = Nothing
Exit Sub

End Sub

Nov 13 '05 #1
3 23744
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I believe ADO parameters are ONLY for stored procedures. If you're
creating the SQL statement in VBA you can insert the parameter values in
the SQL string w/ concatenation:

param1 = "'2000-01-01'"
param2 = "'2007-01-01'"
param3 = 0
cmd.CommandText = "SELECT * FROM blah, blah.... " & _
"WHERE something>= " & param3 & " AND " & _
"something BETWEEN " & param1 & " AND " & param2

When I pass datetime values to SQL I put them in single quotes; SQL
automatically converts them to datetime data types. Also, I use the ISO
format, which is CCYY-MM-DD; where CC is the century number, YY the year
of the century, MM the month number, and DD the day of the month.
--
MGFoster:::mgf0 0 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQl68aIechKq OuFEgEQLEfACgp/Rs2dJpB4QP86dLq TTTt68ghDgAniPI
nrj2pBygatiDkYl bMK/aKzSb
=cKPN
-----END PGP SIGNATURE-----

Zlatko Matić wrote:
Hello.

I know how to call a parameterized stored procedure by using ADODB command
object and parameters,
but how can I execute the same query using adCmdText instead of
adCmdStoredProc ?
Namely I created the CommandText string with declare statement for
parameters before the select statement. No error is displayed, but also, no
records are shown iether. It seems that the server can't recognize
parameters. The select statement is exactly the same as it was in the stored
procedure...
The code is somethingh like this:

Option Compare Database
Private Sub BLABLA()

Dim cmd As ADODB.Command
Dim par As ADODB.Parameter
Dim fld As ADODB.Field
Dim rs As ADODB.Recordset
Dim str As String

Dim PARAMETER1 As New ADODB.Parameter
Dim PARAMETER2 As New ADODB.Parameter
Dim PARAMETER3 As New ADODB.Parameter

Dim rst As ADODB.Recordset

Set cmd = New ADODB.Command

cmd.ActiveConne ction = "Provider=SQLOL EDB.1;Data Source=(local); " &
"Integrated Security=SSPI;I nitial Catalog=Databas eName"

cmd.CommandText = "DECLARE @PARAMETER1 datetime, @PARAMETER2 datetime,
@PARAMETER3 bit;" & _
"SELECT * FROM blah, blah.... " & _
"WHERE something>= @PARAMETER3 AND " & _
"something BETWEEN @PARAMETER1 AND @PARAMETER2"

cmd.CommandType = adCmdText

Set PARAMETER1 = cmd.CreateParam eter("@PARAMETE R1", adDate, adParamInput)
cmd.Parameters. Append PARAMETER1
PARAMETER1.Valu e = Format("01.01.2 000", "yyyy-mm-dd")

Set PARAMETER2 = cmd.CreateParam eter("@PARAMETE R2", adDate, adParamInput)
cmd.Parameters. Append PARAMETER2
PARAMETER2.Valu e = Format("01.01.2 007", "yyyy-mm-dd")

Set PARAMETER3 = cmd.CreateParam eter("@PARAMETE R3", adInteger, adParamInput)
cmd.Parameters. Append PARAMETER3
PARAMETER3.Valu e = 0

Set rst = New ADODB.Recordset
Set rst = cmd.Execute()

Do Until rst.EOF
str = ""
For Each fld In rst.Fields
str = str & fld.Value & Chr(9)

Next fld
Debug.Print str
rst.MoveNext
Loop

Set rst = Nothing
Set par = Nothing
Set cmd = Nothing
Set PARAMETER1 = Nothing
Set PARAMETER2 = Nothing
Set PARAMETER3 = Nothing
Exit Sub

End Sub

Nov 13 '05 #2
Hi!

I think you ou should be able to do something like this:

cmd.CommandText "SELECT * FROM blah, blah.... " & _
"WHERE something>= ? AND " & _
"something BETWEEN ? AND ?"

cmd.CommandType = adCmdText

Then supply the parameters in the same order

Set PARAMETER1 = cmd.CreateParam eter("something ", adInteger,
adParamInput)
cmd.Parameters. Append PARAMETER1
PARAMETER1.Valu e = 0

Set PARAMETER2 = cmd.CreateParam eter("someother thing", adDate,
adParamInput)
cmd.Parameters. Append PARAMETER2
PARAMETER2.Valu e = <datevariable >

Set PARAMETER3 = cmd.CreateParam eter("someother thing", adDate,
adParamInput)
cmd.Parameters. Append PARAMETER3
PARAMETER3.Valu e = <someotherdatav ariable>
Zlatko Matić wrote:
Hello.

I know how to call a parameterized stored procedure by using ADODB command object and parameters,
but how can I execute the same query using adCmdText instead of
adCmdStoredProc ?
Namely I created the CommandText string with declare statement for
parameters before the select statement. No error is displayed, but also, no records are shown iether. It seems that the server can't recognize
parameters. The select statement is exactly the same as it was in the stored procedure...
The code is somethingh like this:

Option Compare Database
Private Sub BLABLA()

Dim cmd As ADODB.Command
Dim par As ADODB.Parameter
Dim fld As ADODB.Field
Dim rs As ADODB.Recordset
Dim str As String

Dim PARAMETER1 As New ADODB.Parameter
Dim PARAMETER2 As New ADODB.Parameter
Dim PARAMETER3 As New ADODB.Parameter

Dim rst As ADODB.Recordset

Set cmd = New ADODB.Command

cmd.ActiveConne ction = "Provider=SQLOL EDB.1;Data Source=(local); " &
"Integrated Security=SSPI;I nitial Catalog=Databas eName"

cmd.CommandText = "DECLARE @PARAMETER1 datetime, @PARAMETER2 datetime, @PARAMETER3 bit;" & _
"SELECT * FROM blah, blah.... " & _
"WHERE something>= @PARAMETER3 AND " & _
"something BETWEEN @PARAMETER1 AND @PARAMETER2"

cmd.CommandType = adCmdText

Set PARAMETER1 = cmd.CreateParam eter("@PARAMETE R1", adDate, adParamInput) cmd.Parameters. Append PARAMETER1
PARAMETER1.Valu e = Format("01.01.2 000", "yyyy-mm-dd")

Set PARAMETER2 = cmd.CreateParam eter("@PARAMETE R2", adDate, adParamInput) cmd.Parameters. Append PARAMETER2
PARAMETER2.Valu e = Format("01.01.2 007", "yyyy-mm-dd")

Set PARAMETER3 = cmd.CreateParam eter("@PARAMETE R3", adInteger, adParamInput) cmd.Parameters. Append PARAMETER3
PARAMETER3.Valu e = 0

Set rst = New ADODB.Recordset
Set rst = cmd.Execute()

Do Until rst.EOF
str = ""
For Each fld In rst.Fields
str = str & fld.Value & Chr(9)

Next fld
Debug.Print str
rst.MoveNext
Loop

Set rst = Nothing
Set par = Nothing
Set cmd = Nothing
Set PARAMETER1 = Nothing
Set PARAMETER2 = Nothing
Set PARAMETER3 = Nothing


Exit Sub

End Sub


Nov 13 '05 #3
An easier way to use the parameters is
cmd.Parameters( "@something ") = thevalue

RoyVidar wrote:
Hi!

I think you ou should be able to do something like this:

cmd.CommandText "SELECT * FROM blah, blah.... " & _
"WHERE something>= ? AND " & _
"something BETWEEN ? AND ?"

cmd.CommandType = adCmdText

Then supply the parameters in the same order

Set PARAMETER1 = cmd.CreateParam eter("something ", adInteger,
adParamInput)
cmd.Parameters. Append PARAMETER1
PARAMETER1.Valu e = 0

Set PARAMETER2 = cmd.CreateParam eter("someother thing", adDate,
adParamInput)
cmd.Parameters. Append PARAMETER2
PARAMETER2.Valu e = <datevariable >

Set PARAMETER3 = cmd.CreateParam eter("someother thing", adDate,
adParamInput)
cmd.Parameters. Append PARAMETER3
PARAMETER3.Valu e = <someotherdatav ariable>
Zlatko Matić wrote:
Hello.

I know how to call a parameterized stored procedure by using ADODB


command
object and parameters,
but how can I execute the same query using adCmdText instead of
adCmdStoredPr oc?
Namely I created the CommandText string with declare statement for
parameters before the select statement. No error is displayed, but


also, no
records are shown iether. It seems that the server can't recognize
parameters. The select statement is exactly the same as it was in the


stored
procedure.. .
The code is somethingh like this:

Option Compare Database
Private Sub BLABLA()

Dim cmd As ADODB.Command
Dim par As ADODB.Parameter
Dim fld As ADODB.Field
Dim rs As ADODB.Recordset
Dim str As String

Dim PARAMETER1 As New ADODB.Parameter
Dim PARAMETER2 As New ADODB.Parameter
Dim PARAMETER3 As New ADODB.Parameter

Dim rst As ADODB.Recordset

Set cmd = New ADODB.Command

cmd.ActiveCon nection = "Provider=SQLOL EDB.1;Data Source=(local); " &
"Integrated Security=SSPI;I nitial Catalog=Databas eName"

cmd.CommandTe xt = "DECLARE @PARAMETER1 datetime, @PARAMETER2


datetime,
@PARAMETER3 bit;" & _
"SELECT * FROM blah, blah.... " & _
"WHERE something>= @PARAMETER3 AND " & _
"something BETWEEN @PARAMETER1 AND @PARAMETER2"

cmd.CommandTy pe = adCmdText

Set PARAMETER1 = cmd.CreateParam eter("@PARAMETE R1", adDate,


adParamInput)
cmd.Parameter s.Append PARAMETER1
PARAMETER1.Va lue = Format("01.01.2 000", "yyyy-mm-dd")

Set PARAMETER2 = cmd.CreateParam eter("@PARAMETE R2", adDate,


adParamInput)
cmd.Parameter s.Append PARAMETER2
PARAMETER2.Va lue = Format("01.01.2 007", "yyyy-mm-dd")

Set PARAMETER3 = cmd.CreateParam eter("@PARAMETE R3", adInteger,


adParamInput)
cmd.Parameter s.Append PARAMETER3
PARAMETER3.Va lue = 0

Set rst = New ADODB.Recordset
Set rst = cmd.Execute()

Do Until rst.EOF
str = ""
For Each fld In rst.Fields
str = str & fld.Value & Chr(9)

Next fld
Debug.Print str
rst.MoveNext
Loop

Set rst = Nothing
Set par = Nothing
Set cmd = Nothing
Set PARAMETER1 = Nothing
Set PARAMETER2 = Nothing
Set PARAMETER3 = Nothing
Exit Sub

End Sub


Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2201
by: Trey Guy | last post by:
I have an ASP page I use as a front end to an Access db. I am running a query from that page that is returning a "Too few parameters..." error. The query works fine in Access. I have checked all field names and they are correct. The SQL is below. Any ideas as to the cause of this error? Thanks, Trey SELECT qryParticipantOutcomesAggregate.IndID, Indicators.IndName, Sum(qryParticipantOutcomesAggregate.NEligible) AS SumOfNEligible,
18
2854
by: Johnd | last post by:
I am running into an issue when I try to write more than 1024 characters to a memo field. Apparantly the odbc connection I am using does not permit literals to be larger that 1024 characters. This Memo filed can take virtually infinate data, so the solution seems to be use parameters. My problem is I have never heard of this and need this to work ASAP. Does anyone have a simple example of how Parameters work using ASP?
5
4381
by: Bruno Alexandre | last post by:
Hi guys, withou using SP, I want to be able to add a Parameter to the SQL Query and retrive the Recordset so I can use the Paging property under the recorset object.... how can I do this? I'm stuck here.
3
4756
by: Jenni | last post by:
Hi, my simple solution is not so simple. I have done a bit of VB programming, and am now tasked with trying to make access work using VBA, and battling my butt off. This probably has a really simple solution, which right now eludes me. The code is below, and the error message I get is this "No Value given for one or more required parameters". sClinic is picking up the string from a combo box, and this does work cos the message box tosses...
8
2093
by: CSDunn | last post by:
Hello, I have a situation in which I need to address three SQL Server 2000 Stored Procedure parameters in the OnClick event of an Option Group. The Option Group lives on an Access 2000 ADP form. In another situation where I had to set the RowSource of a combo box based on a parameter value that was delivered to a proc from another combo box, I did this: Private Sub FirstCombo_AfterUpdate()
7
21643
by: Zlatko Matić | last post by:
Let's assume that we have a database on some SQL server (let it be MS SQL Server) and that we want to execute some parameterized query as a pass.through query. How can we pass parameters to the server ? Is it possible to use parameters in pass-through queries ? An additional question: Is it possible to connect to a database on MySQL or PostgreSQL using ADO ? Is it possible to execute pass-through queries with parameters, using ADO...
1
1843
by: javediq143 | last post by:
Hi all, I got one more prob in another ASP page linked to the earlier one. I've created an Access DB and created a query using Access Query wizard. The query is running fine in Access and it is showing me the right Record each time. But when I'm taking this query in ASP page it is giving me this Err: Error Type: Microsoft OLE DB Provider for ODBC Drivers (0x80040E10) Too few parameters. Expected 1. /imagine/ShowJobDetailTest.asp, line 36...
15
16079
by: Dave | last post by:
I am getting the error above intermittantly with an ASP 3.0 page using an MS Access 2003 database. I have searched Google extensively and found the following possible causes for this error: A field name was spelled incorrectly. One or more of the values was blank. You tried to insert the wrong datatype (e.g. surrounded a numeric value with quotes, or forgot to put quotes around a string).
1
1944
by: ravysters | last post by:
hi... i am using VC++ to connect to my database and i am trying to insert a new row into a table. I am also using the returning statement available in postgres to return a column of the currently inserted record.. i have to output this.. can i do this.. here is my code.. i am getting an exception when i run this code.. there is no probs when i only use the insert statement.. but when i use the insert statement with the returning clause i get the...
0
9708
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9588
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10589
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10327
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7625
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2999
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.