Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / General 1 / January 2006

Tip: Looking for answers? Try searching our database.

How call procedure?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Marcin Wasilewski - 30 Jan 2006 08:52 GMT
Hi,
How to call procedure, which is on SQL Server from Access using visual
basic?

Thanks,
mw
Anthony England - 30 Jan 2006 09:33 GMT
> 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.
Marcin Wasilewski - 30 Jan 2006 10:44 GMT
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,ShippedDate) 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.CommandType =
objCom.CommandText =
objCom.Execute

Thanks for your help.
mw
Anthony England - 30 Jan 2006 11:35 GMT
> Of course you're right.
> Below there is code of procedure which a want to call:
[quoted text clipped - 28 lines]
> 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.Connection
   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=sqloledb;" & _
             "Data Source=MyServer;" & _
             "Initial Catalog=MyDatabase;" & _
             "Integrated Security=SSPI"

   Set cnn = New ADODB.Connection

   cnn.Open strConn

   Set cmd = New ADODB.Command

   cmd.ActiveConnection = cnn

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

   cmd.CommandType = adCmdStoredProc

   Set prm = cmd.CreateParameter("@BeginningDate", adDate, adParamInput, ,
dteStart)
   cmd.Parameters.Append prm

   Set prm = cmd.CreateParameter("@EndingDate", adDate, adParamInput, ,
dteEnd)
   cmd.Parameters.Append prm

   Set rst = cmd.Execute

   While Not rst.EOF
       Debug.Print rst.Fields("OrderID")
       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
Marcin Wasilewski - 30 Jan 2006 13:19 GMT
Thank you very much.
Of course everything is working.

mw
Marcin Wasilewski - 30 Jan 2006 13:20 GMT
Thank you very much.
It's exactly what I need.
Of course everything is working.

mw
Lyle Fairfield - 30 Jan 2006 13:26 GMT
>> Of course you're right.
>> Below there is code of procedure which a want to call:
[quoted text clipped - 125 lines]
>
> 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.

Signature

Lyle Fairfield

Anthony England - 30 Jan 2006 13:44 GMT
>>> Of course you're right.
>>> Below there is code of procedure which a want to call:
[quoted text clipped - 129 lines]
> 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.

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.
Marcin Wasilewski - 30 Jan 2006 15:40 GMT
One more question to
   strConn = "Provider=sqloledb;" & _
             "Data Source=C-MWA;" & _
             "Initial Catalog=Northwind;" & _
             "Integrated Security=SSPI"

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

mw
Anthony England - 30 Jan 2006 15:50 GMT
> One more question to
>    strConn = "Provider=sqloledb;" & _
[quoted text clipped - 5 lines]
>
> mw

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

http://www.carlprothman.net/Technology/ConnectionStrings/OLEDBProviders/tabid/87
/Default.aspx

Marcin Wasilewski - 31 Jan 2006 08:08 GMT
Thank you for responce.
I must get to oracle form access so a must use

* OLE DB Provider for Oracle (from Microsoft)
oConn.Open "Provider=msdaora;" & _
          "Data Source=MyOracleDB;" & _
          "User Id=myUsername;" & _
          "Password=myPassword"

But when I try to open a cennection I have this error:
"ORA-12538: TNS: there is not protocol adapter" ???
What does it mean?
have you any idea?

mw
Anthony England - 31 Jan 2006 09:55 GMT
> Thank you for responce.
> I must get to oracle form access so a must use
[quoted text clipped - 11 lines]
>
> mw

Sorry Marcin, I have no experience in configuring Oracle.
Do you have any way of checking that the database is working, and that your
login details are correct?  Is there some form of Oracle software installed
on your client machine?  Do you know which files need to be configured?
If you still need an answer, post a new question:  "ADO Connection to
Oracle" there may be someone who can help.
Anthony England - 31 Jan 2006 10:11 GMT
> Thank you for responce.
> I must get to oracle form access so a must use
[quoted text clipped - 11 lines]
>
> mw

Sorry Marcin, I have no experience in configuring Oracle.
Do you have any way of checking that the database is working, and that your
login details are correct?  Is there some form of Oracle software installed
on your client machine?  Do you know which files need to be configured?
If you still need an answer, post a new question:  "ADO Connection to
Oracle" there may be someone who can help.
Marcin Wasilewski - 31 Jan 2006 14:02 GMT
I can handel it.
Of course there was some problems with access (permissions) on server.

It's typical problem when yor're working on server which is
administrated be some else.

Thanks for response.

mw
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.