473,661 Members | 2,448 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How call procedure?

Hi,
How to call procedure, which is on SQL Server from Access using visual
basic?

Thanks,
mw

Jan 30 '06 #1
13 2310
"Marcin Wasilewski" <ma************ ********@gmail. com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
Hi,
How to call procedure, which is on SQL Server from Access using visual
basic?

Thanks,
mw


We can't see the stored procedure, so we don't know what you are trying to
do. Stored procedures can be used to do many things - sometimes they return
a recordset, sometimes multiple recordsets, sometimes none. Sometimes they
have parameters (input and output) and sometimes none. So really you should
post your stored procedure and say what you need to do with it.
But you could try doing a search before you ask - there must be plenty of
examples.
Jan 30 '06 #2
Of course you're right.
Below there is code of procedure which a want to call:

CREATE PROCEDURE dbo.[Year to Year Sales]
@BeginningDate DateTime, @EndingDate DateTime
AS
IF @BeginningDate IS NULL OR @EndingDate IS NULL
BEGIN
RAISERROR('NULL values are not allowed', 14, 1)
RETURN
END
SELECT O.ShippedDate,
O.OrderID,
OS.Subtotal,
DATENAME(yy,Shi ppedDate) AS Year
FROM ORDERS O INNER JOIN [Order Subtotals] OS
ON O.OrderID = OS.OrderID
WHERE O.ShippedDate BETWEEN @BeginningDate AND @EndingDate
GO

As you see, I need to give two parametrs and I give back a some data.

Of course I search befor asking, but it is my first time ;) so a realy
don't know, how build this part of code.
So I need call the procedure with 'Year' and 'Year Sales' parametrs.
I've founded somting like that:
objCom.CommandT ype =
objCom.CommandT ext =
objCom.Execute

Thanks for your help.
mw

Jan 30 '06 #3

"Marcin Wasilewski" <ma************ ********@gmail. com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
Of course you're right.
Below there is code of procedure which a want to call:

CREATE PROCEDURE dbo.[Year to Year Sales]
@BeginningDate DateTime, @EndingDate DateTime
AS
IF @BeginningDate IS NULL OR @EndingDate IS NULL
BEGIN
RAISERROR('NULL values are not allowed', 14, 1)
RETURN
END
SELECT O.ShippedDate,
O.OrderID,
OS.Subtotal,
DATENAME(yy,Shi ppedDate) AS Year
FROM ORDERS O INNER JOIN [Order Subtotals] OS
ON O.OrderID = OS.OrderID
WHERE O.ShippedDate BETWEEN @BeginningDate AND @EndingDate
GO

As you see, I need to give two parametrs and I give back a some data.

Of course I search befor asking, but it is my first time ;) so a realy
don't know, how build this part of code.
So I need call the procedure with 'Year' and 'Year Sales' parametrs.
I've founded somting like that:
objCom.CommandT ype =
objCom.CommandT ext =
objCom.Execute

Thanks for your help.
mw


I'm not sure whether the name of your procedure "Year to Year Sales" matches
what it does. If the idea is to create a recordset based on a single year,
then perhaps you only need one input parameter - the year. You also need to
be careful with "between" when using dates as the order date may also
contain a time portion. I would also avoid putting spaces in the names of
stored procedures as it means you have to use the silly brackets.

I don't know whether I would bother to raise an error in the stored
procedure if you are going to wrap it in a vba function. You could use the
vba coding to check whether you have a start and end date. If you are going
to start raising errors, then perhaps you should think about getting your
stored procedure to provide a return value to show if it was successful.
You then have to get your vba coding to get the value of this output
parameter. I have not done this here, but this gives you the general idea:
Private Sub cmdTest_Click()

On Error GoTo Err_Handler

Dim cnn As ADODB.Connectio n
Dim cmd As ADODB.Command
Dim prm As ADODB.Parameter
Dim rst As ADODB.Recordset
Dim strConn As String
Dim dteStart As Date
Dim dteEnd As Date

dteStart = DateSerial(2006 , 1, 1)

dteEnd = DateSerial(2006 , 2, 1)

strConn = "Provider=sqlol edb;" & _
"Data Source=MyServer ;" & _
"Initial Catalog=MyDatab ase;" & _
"Integrated Security=SSPI"

Set cnn = New ADODB.Connectio n

cnn.Open strConn

Set cmd = New ADODB.Command

cmd.ActiveConne ction = cnn

cmd.CommandText = "[Year to Year Sales]"

cmd.CommandType = adCmdStoredProc

Set prm = cmd.CreateParam eter("@Beginnin gDate", adDate, adParamInput, ,
dteStart)
cmd.Parameters. Append prm

Set prm = cmd.CreateParam eter("@EndingDa te", adDate, adParamInput, ,
dteEnd)
cmd.Parameters. Append prm

Set rst = cmd.Execute

While Not rst.EOF
Debug.Print rst.Fields("Ord erID")
rst.MoveNext
Wend

MsgBox "Done", vbInformation

Exit_Handler:

If Not rst Is Nothing Then
If rst.State <> adStateClosed Then
rst.Close
End If
Set rst = Nothing
End If

Set prm = Nothing

Set cmd = Nothing

If Not cnn Is Nothing Then
If cnn.State <> adStateClosed Then
cnn.Close
End If
Set cnn = Nothing
End If

Exit Sub

Err_Handler:
MsgBox Err.Description , vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Sub


Jan 30 '06 #4
Thank you very much.
Of course everything is working.

mw

Jan 30 '06 #5
Thank you very much.
It's exactly what I need.
Of course everything is working.

mw

Jan 30 '06 #6
"Anthony England" <ae******@oops. co.uk> wrote in
news:dr******** **@nwrdmz01.dmz .ncs.ea.ibs-infra.bt.com:

"Marcin Wasilewski" <ma************ ********@gmail. com> wrote in
message news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
Of course you're right.
Below there is code of procedure which a want to call:

CREATE PROCEDURE dbo.[Year to Year Sales]
@BeginningDate DateTime, @EndingDate DateTime
AS
IF @BeginningDate IS NULL OR @EndingDate IS NULL
BEGIN
RAISERROR('NULL values are not allowed', 14, 1)
RETURN
END
SELECT O.ShippedDate,
O.OrderID,
OS.Subtotal,
DATENAME(yy,Shi ppedDate) AS Year
FROM ORDERS O INNER JOIN [Order Subtotals] OS
ON O.OrderID = OS.OrderID
WHERE O.ShippedDate BETWEEN @BeginningDate AND @EndingDate
GO

As you see, I need to give two parametrs and I give back a some data.

Of course I search befor asking, but it is my first time ;) so a
realy don't know, how build this part of code.
So I need call the procedure with 'Year' and 'Year Sales' parametrs.
I've founded somting like that:
objCom.CommandT ype =
objCom.CommandT ext =
objCom.Execute

Thanks for your help.
mw


I'm not sure whether the name of your procedure "Year to Year Sales"
matches what it does. If the idea is to create a recordset based on a
single year, then perhaps you only need one input parameter - the
year. You also need to be careful with "between" when using dates as
the order date may also contain a time portion. I would also avoid
putting spaces in the names of stored procedures as it means you have
to use the silly brackets.

I don't know whether I would bother to raise an error in the stored
procedure if you are going to wrap it in a vba function. You could
use the vba coding to check whether you have a start and end date. If
you are going to start raising errors, then perhaps you should think
about getting your stored procedure to provide a return value to show
if it was successful. You then have to get your vba coding to get the
value of this output parameter. I have not done this here, but this
gives you the general idea:
Private Sub cmdTest_Click()

On Error GoTo Err_Handler

Dim cnn As ADODB.Connectio n
Dim cmd As ADODB.Command
Dim prm As ADODB.Parameter
Dim rst As ADODB.Recordset
Dim strConn As String
Dim dteStart As Date
Dim dteEnd As Date

dteStart = DateSerial(2006 , 1, 1)

dteEnd = DateSerial(2006 , 2, 1)

strConn = "Provider=sqlol edb;" & _
"Data Source=MyServer ;" & _
"Initial Catalog=MyDatab ase;" & _
"Integrated Security=SSPI"

Set cnn = New ADODB.Connectio n

cnn.Open strConn

Set cmd = New ADODB.Command

cmd.ActiveConne ction = cnn

cmd.CommandText = "[Year to Year Sales]"

cmd.CommandType = adCmdStoredProc

Set prm = cmd.CreateParam eter("@Beginnin gDate", adDate,
adParamInput, ,
dteStart)
cmd.Parameters. Append prm

Set prm = cmd.CreateParam eter("@EndingDa te", adDate, adParamInput,
,
dteEnd)
cmd.Parameters. Append prm

Set rst = cmd.Execute

While Not rst.EOF
Debug.Print rst.Fields("Ord erID")
rst.MoveNext
Wend

MsgBox "Done", vbInformation

Exit_Handler:

If Not rst Is Nothing Then
If rst.State <> adStateClosed Then
rst.Close
End If
Set rst = Nothing
End If

Set prm = Nothing

Set cmd = Nothing

If Not cnn Is Nothing Then
If cnn.State <> adStateClosed Then
cnn.Close
End If
Set cnn = Nothing
End If

Exit Sub

Err_Handler:
MsgBox Err.Description , vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Sub


Probably, we should all be so thorough and careful. ADO has many
shortcuts and allows us to be less rigorous than this example is. But
doing it "right" as you have shown may result in less grief in the end.

--
Lyle Fairfield
Jan 30 '06 #7

"Lyle Fairfield" <ly***********@ aim.com> wrote in message
news:Xn******** *************** **********@216. 221.81.119...
"Anthony England" <ae******@oops. co.uk> wrote in
news:dr******** **@nwrdmz01.dmz .ncs.ea.ibs-infra.bt.com:

"Marcin Wasilewski" <ma************ ********@gmail. com> wrote in
message news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
Of course you're right.
Below there is code of procedure which a want to call:

CREATE PROCEDURE dbo.[Year to Year Sales]
@BeginningDate DateTime, @EndingDate DateTime
AS
IF @BeginningDate IS NULL OR @EndingDate IS NULL
BEGIN
RAISERROR('NULL values are not allowed', 14, 1)
RETURN
END
SELECT O.ShippedDate,
O.OrderID,
OS.Subtotal,
DATENAME(yy,Shi ppedDate) AS Year
FROM ORDERS O INNER JOIN [Order Subtotals] OS
ON O.OrderID = OS.OrderID
WHERE O.ShippedDate BETWEEN @BeginningDate AND @EndingDate
GO

As you see, I need to give two parametrs and I give back a some data.

Of course I search befor asking, but it is my first time ;) so a
realy don't know, how build this part of code.
So I need call the procedure with 'Year' and 'Year Sales' parametrs.
I've founded somting like that:
objCom.CommandT ype =
objCom.CommandT ext =
objCom.Execute

Thanks for your help.
mw


I'm not sure whether the name of your procedure "Year to Year Sales"
matches what it does. If the idea is to create a recordset based on a
single year, then perhaps you only need one input parameter - the
year. You also need to be careful with "between" when using dates as
the order date may also contain a time portion. I would also avoid
putting spaces in the names of stored procedures as it means you have
to use the silly brackets.

I don't know whether I would bother to raise an error in the stored
procedure if you are going to wrap it in a vba function. You could
use the vba coding to check whether you have a start and end date. If
you are going to start raising errors, then perhaps you should think
about getting your stored procedure to provide a return value to show
if it was successful. You then have to get your vba coding to get the
value of this output parameter. I have not done this here, but this
gives you the general idea:
Private Sub cmdTest_Click()

On Error GoTo Err_Handler

Dim cnn As ADODB.Connectio n
Dim cmd As ADODB.Command
Dim prm As ADODB.Parameter
Dim rst As ADODB.Recordset
Dim strConn As String
Dim dteStart As Date
Dim dteEnd As Date

dteStart = DateSerial(2006 , 1, 1)

dteEnd = DateSerial(2006 , 2, 1)

strConn = "Provider=sqlol edb;" & _
"Data Source=MyServer ;" & _
"Initial Catalog=MyDatab ase;" & _
"Integrated Security=SSPI"

Set cnn = New ADODB.Connectio n

cnn.Open strConn

Set cmd = New ADODB.Command

cmd.ActiveConne ction = cnn

cmd.CommandText = "[Year to Year Sales]"

cmd.CommandType = adCmdStoredProc

Set prm = cmd.CreateParam eter("@Beginnin gDate", adDate,
adParamInput, ,
dteStart)
cmd.Parameters. Append prm

Set prm = cmd.CreateParam eter("@EndingDa te", adDate, adParamInput,
,
dteEnd)
cmd.Parameters. Append prm

Set rst = cmd.Execute

While Not rst.EOF
Debug.Print rst.Fields("Ord erID")
rst.MoveNext
Wend

MsgBox "Done", vbInformation

Exit_Handler:

If Not rst Is Nothing Then
If rst.State <> adStateClosed Then
rst.Close
End If
Set rst = Nothing
End If

Set prm = Nothing

Set cmd = Nothing

If Not cnn Is Nothing Then
If cnn.State <> adStateClosed Then
cnn.Close
End If
Set cnn = Nothing
End If

Exit Sub

Err_Handler:
MsgBox Err.Description , vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Sub


Probably, we should all be so thorough and careful. ADO has many
shortcuts and allows us to be less rigorous than this example is. But
doing it "right" as you have shown may result in less grief in the end.

--
Lyle Fairfield

Thank you, Lyle. That is the first positive comment I have had about my
coding for a while. It's a pity it's not for the project I'm supposed to
working on.
Jan 30 '06 #8
One more question to
strConn = "Provider=sqlol edb;" & _
"Data Source=C-MWA;" & _
"Initial Catalog=Northwi nd;" & _
"Integrated Security=SSPI"

If my data base is on oracle server, how this code will be look like?

mw

Jan 30 '06 #9
"Marcin Wasilewski" <ma************ ********@gmail. com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
One more question to
strConn = "Provider=sqlol edb;" & _
"Data Source=C-MWA;" & _
"Initial Catalog=Northwi nd;" & _
"Integrated Security=SSPI"

If my data base is on oracle server, how this code will be look like?

mw


Check out Carl Prothman's site for all sorts of connection strings:

http://www.carlprothman.net/Technolo...7/Default.aspx
Jan 30 '06 #10

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

Similar topics

0
6695
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft Visual Basic .NET version of this article, see 308049. For a Microsoft Visual C++ .NET version of this article, see 310071. For a Microsoft Visual J# .NET version of this article, see 320627. This article refers to the following Microsoft .NET...
3
23999
by: Mariusz | last post by:
I want to write function to call another function which name is parameter to first function. Other parameters should be passed to called function. If I call it function('f1',10) it should call f1(10). If I call it function('f2',5) it should call f2(5). So far i tried something like CREATE FUNCTION . (@f varchar(50),@m money) RETURNS varchar(50) AS
4
3242
by: Jean-Marc Blaise | last post by:
Dear all, I have simulated the windows MULTI application with a java program calling the SQLTP1DL proc referenced as DB2DARI application, on Linux Intel or ZLinux. If the proc is NOT FENCED, there is no pb and the program works fine. If either the proc is FENCED, or FENCED THREADSAFE, I get a SQL1042C, or the instance crashes on ZLinux (V8.1 FP4). The proc does a return(SQLZ_HOLD_PROC): $ /opt/IBMJava2-131/bin/java sqltp1ri 1
1
1697
by: news.onet.pl | last post by:
Hello! I have a small question concerning to the procedure call. I have the following procedure: private sub procedure_name (ByVal name1 as string) .... end sub When I call it I just write:
0
19272
by: IamtheEvster | last post by:
Hi All, I am currently using PHP 5 and MySQL 5, both on Fedora Core 5. I am unable to call a MySQL stored procedure that returns output parameters using mysql, mysqli, or PDO. I'm having a hell of a time with it... The following comes from phpinfo(): PHP Version: 5.1.2 mysql Client API version: 5.0.18 mysqli Client API version: 5.0.18
3
4316
by: harborboy76 | last post by:
I am calling the exact same stored procedure called myprocedure from 2 different boxes from the CLP, but I'm experiencing different behaviors between them. After I was unable to get any support from IBM due to V7.1 being no longer supported, I figure someone here might be able to help. Is DB2 V7 more forgiving in the way I can call my stored procedure ? If it's defined with CHARACTER as incoming parameter, am I not required to put any...
4
4993
by: Pakna | last post by:
Hi, is there any way to call a JAVA stored procedure from a SQL Trigger? We are having difficulties with this and cannot verify whether DB2 even *has* this capability? Thank you very much....
2
10558
by: savio XCIX | last post by:
I created the following stored procedure: ======= CREATE PROCEDURE TBLNAME.proc_test (IN p_custnum VARCHAR(8), IN p_zipcode CHAR(5), OUT r_valid CHAR(1), OUT r_bal DECIMAL(9,2)) LANGUAGE SQL BEGIN
275
12247
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
12
30931
by: barmatt80 | last post by:
I don't know if this is the right part of the forum. But.... I have been working all night trying to create a web service to call a stored procedure in sql server 2008. The stored procedure calls a linked server to a db2 database that accepts 1 integer and returns 6 variables. The end result would be publish the web service to our sharepoint servers. This is not a problem. I am just trying to wrap my head around it. I know how to call...
0
8428
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
8341
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
8851
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
8754
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
8542
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
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4343
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2760
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.