Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old August 7th, 2003, 07:11 AM
Friend of Wrox
Join Date: Aug 2003
Posts: 166
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using an insert stored procedure

If I have an insert stored proc. already writen. How do I use it in VB, asuming the DB is already connected to. I have text boxes full of info and I want to click an insert command button and insert a new record in the DB. I know there is a way by using the execute SQL command but I am not sure of the syntax. I don't want to set up a parameters storedproc I just want to use the storedproc I already have. I hope I explained it right.

Thanks for you help


\"Don\'t follow someone who\'s not going anywhere\" John Mason
Old August 7th, 2003, 08:19 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts

Didn't just post this message yesterday?

Anyway, If you are going to use a stored procedure or any other method to update\insert data into a data table then you are going to have to use parameters. Think about it.

You have a text box that may contain the name of a new user. You want to add the users name to the user name table in your database. You also have this nice stored procedure that will handle the insert. If you do not pass the stored procedure any parameters, like the users name, what will it insert into the database. Somehow you have to pass the user name to the stored procedure. That would be a neat trick if you could do it without giving it the user name.

You can also use the execute method provided by your connection object but then you have to re-write your sql statement and your stored procedure will never run. But, again you still need to provide the user name, simply put this is a parameter.

So I am going to provide the same reply and see if it makes more sense.

' Create a command object
Dim Cmd as New Adodb.Command
' Set the command object equal to the connection object
Set Cmd.ActiveConnection = Cn
' Define the Store procedure
With Cmd
     .CommandText = "sp_YourSprocsName"
     .CommandType = adCmdStoredProc
     ' Set parameters
     ' Substitute "@MyParam1" with the name of any parameters
     ' required by the sproc
     ' Substitute Value1 with the parameter value
     .Parameter("@MyParam1") = Value1
     ' Repeat this for each parameter
     .Parameter("@MyParamX") = ValueX
     ' Execute the stored procedure
     ' If you are not returning records then
     ' If you are returning records and assuming
     ' your recordset object is named rs
     Set rs = .Execute()
     ' You can now reference your recordset object as usual
End With

Please ask me if you do not understand this and I will try to answer your question and explain it better. If you need further help try posting your stored procedure then maybe we can give you a more explicit answer.

Larry Asher
Old August 7th, 2003, 08:38 AM
Friend of Wrox
Join Date: Aug 2003
Posts: 166
Thanks: 0
Thanked 0 Times in 0 Posts

I used your post from yesterday and with today's info it really helped. Thanks for your help I wasn't understanding it completly. I have the paramters all set up and it seems to be working right now.
Thanks again for your help.

Old August 7th, 2003, 08:42 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts

Glad I could help. Hope your project is going well as a whole.

Larry Asher

Similar Threads
Thread Thread Starter Forum Replies Last Post
Check value while insert in stored procedure Andraw SQL Server 2005 2 August 12th, 2008 09:08 AM
insert using stored procedure & sqldatasource ctrl mahajanvit ASP.NET 2.0 Basics 10 September 13th, 2006 09:03 AM
Stored Procedure Insert Null Value harpua SQL Server ASP 2 March 5th, 2005 10:19 AM
stored procedure dynamic insert harpua Classic ASP Databases 3 January 21st, 2005 12:50 AM
How do I insert stored procedure through C# nishim.attreja VB How-To 1 September 18th, 2004 01:24 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.