| |

VerySource

 Forgot password?
 Register
Search
View: 643|Reply: 1

How to call a stored procedure with vb? Thank you all

[Copy link]

2

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-12 20:30:02
| Show all posts |Read mode
let him
Reply

Use magic Report

0

Threads

10

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-7-7 15:45:01
| Show all posts
This example uses the Append and CreateParameter methods to execute a stored procedure with input parameters.

Public Sub AppendX()

   Dim cnn1 As ADODB.Connection
   Dim cmdByRoyalty As ADODB.Command
   Dim prmByRoyalty As ADODB.Parameter
   Dim rstByRoyalty As ADODB.Recordset
   Dim rstAuthors As ADODB.Recordset
   Dim intRoyalty As Integer
   Dim strAuthorID As String
   Dim strCnn As String

   'Open the connection.
   Set cnn1 = New ADODB.Connection
   strCnn = "Provider=sqloledb;"&_
      "Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=;"
   cnn1.Open strCnn
   cnn1.CursorLocation = adUseClient
      
   'Open the command object with a single parameter.
   Set cmdByRoyalty = New ADODB.Command
   cmdByRoyalty.CommandText = "byroyalty"
   cmdByRoyalty.CommandType = adCmdStoredProc
   
   'Get parameter values ​​and append parameters.
   intRoyalty = Trim(InputBox("Enter royalty:"))
   Set prmByRoyalty = cmdByRoyalty.CreateParameter("percentage", _
      adInteger, adParamInput)
   cmdByRoyalty.Parameters.Append prmByRoyalty
   prmByRoyalty.Value = intRoyalty

   'Create a record set by executing the command.
   Set cmdByRoyalty.ActiveConnection = cnn1
   Set rstByRoyalty = cmdByRoyalty.Execute
   
   'Open the Authors table to get the author's name for display.
   Set rstAuthors = New ADODB.Recordset
   rstAuthors.Open "authors", cnn1,,, adCmdTable
   
   'Print the current data in the record set and add the author's name from the Authors table.
   Debug.Print "Authors with "&intRoyalty&" percent royalty"
   Do While Not rstByRoyalty.EOF
      strAuthorID = rstByRoyalty!au_id
      Debug.Print ""&rstByRoyalty!au_id&", ";
      rstAuthors.Filter = "au_id ='"&strAuthorID&"'"
      Debug.Print rstAuthors!au_fname&""&rstAuthors!au_lname
      rstByRoyalty.MoveNext
   Loop

   rstByRoyalty.Close
   rstAuthors.Close
   cnn1.Close

End Sub
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list