Important alert: (current site time 7/15/2013 10:35:29 PM EDT)
 

VB icon

Access' queries like stored procedures

Email
Submitted on: 4/13/2000 11:21:57 PM
By: ATM  
Level: Intermediate
User Rating: By 13 Users
Compatibility: ASP (Active Server Pages)
Views: 61951
 
     Here is HOWTO call access' select,insert,update and delete queries from asp.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Access' queries like stored procedures
' Description:Here is HOWTO call access' select,insert,update and delete queries from asp.
' By: ATM
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=6127&lngWId=4'for details.'**************************************

'Create new Access Database, C:\test.mdb 
'
'Create table: 
'TestTable 
'-------------------- 
'id autonumber 
'field1 textlen=50 
'field2 number 
'-------------------- 
'
'CreateFour queries: 
'Query1: DeleteQuery, Type:Delete 
'-------------------------------- 
'DELETE [testTable].[field1], [testTable].[field2] 
'FROM testTable 
'WHERE id=[:param1]; 
'-------------------------------- 
'Query2: InsertQuery, Type:Insert 
'-------------------------------- 
'INSERT INTO testTable ( field1, field2 ) 
'VALUES ([:param1], [:param2]); 
'-------------------------------- 
'Query3: SelectByid, Type:Normal 
'-------------------------------- 
'SELECT [testTable].[field1], [testTable].[field2], [testTable].[id] 
'FROM testTable 
'WHERE ((([testTable].[id])>=[:param1] And ([testTable].[id])<=[:param2])); 
'------------------------------- 
'Query4: UpdateByid, Type:Update 
'-------------------------------- 
'UPDATE testTable SET field1 = [:param1], field2 = [:param2] 
'WHERE id=[:param3]; 
'Calls: 
'accessTest.asp?defMode=0 - for test select 
'
'accessTest.asp?defMode=1 - for test insert 
'
'accessTest.asp?defMode=2 - for test delete 
'
'accessTest.asp?defMode=3 - for test update 
'
'
'Here is accessTest.asp. 
'--------------------------------------- 
<!--#include file="adovbs.inc"--> 
<% 
'for one query we give max 60sec 
execTimeout=60 
'we have 1 query will be executed 
execCount=1 
'max +5 sec for script to execute 
Server.ScriptTimeout=execCount*execTimeout+5 
'connection parameters 
dbPath="DBQ=C:\Test.mdb;" 
userData="UID=;PWD=" 
conn_string="PROVIDER=MSDASQL;" & _ 
 "DRIVER={Microsoft Access Driver (*.mdb)};" &_ 
 dbPath &_ 
 userData 
'connection object
Set connObj=Server.CreateObject("ADODB.Connection") 
'make connection
connObj.Open conn_string 
'command object
Set commandObj=Server.CreateObject("ADODB.Command") 
commandObj.ActiveConnection=connObj 
commandObj.CommandTimeout=execTimeout 
commandObj.CommandType=adCmdStoredProc 
Select Case request("defMode") 
'test Select 
case "0" 
'------------------------------------------------------------------------ 
'our select query is 
'Name: SelectByid 
commandObj.CommandText="SelectByid" 
' 
'SELECT testTable.field1, testTable.field2, testTable.id 
'FROM testTable 
'WHERE (((testTable.id)=>:param1)) and (((testTable.id)<=:param2)); 
'create parameters for query 
commandObj.Parameters.Append commandObj.CreateParameter("param1", _ 
adInteger, _ 
adParamInput, _ 
10, _ 
1) 
commandObj.Parameters.Append commandObj.CreateParameter("param2", _ 
adInteger, _ 
adParamInput, _ 
10, _ 
100) 
'create recordset object 
Set rsObj=Server.CreateObject("ADODB.Recordset") 
rsObj.CursorType=1 'forwardonly 
'run query 
rsObj.Open commandObj 
response.write("<TABLE>") 
response.write("<TR><TD>ID</TD><TD>Field1</TD><TD>Field2</TD></TR>") 
If not(rsObj.EOF) then 
 Do While Not(rsObj.EOF) 
response.write("<TR><TD>") 
response.write(rsObj("id")) 
response.write("<TD>") 
response.write(rsObj("field1")) 
response.write("</TD><TD>") 
response.write(rsObj("field2")) 
response.write("</TD></TR>") 
rsObj.MoveNext 
 Loop 
 'close recordset 
 rsObj.Close 
End if 
response.write("</TABLE>") 
'deallocate rs object 
Set rsObj=Nothing 
'delete allocated parameters 
commandObj.Parameters.Delete "param2" 
commandObj.Parameters.Delete "param1" 
'------------------------------------------------------------------------ 
'test Insert 
case "1" 
'------------------------------------------------------------------------ 
'Name: Insert 
commandObj.CommandText="InsertQuery" 
'INSERT INTO testTable ( field1, field2 ) 
'VALUES ([:param1], [:param2]); 
commandObj.Parameters.Append commandObj.CreateParameter(":param1", _ 
adVarchar, _ 
adParamInput, _ 
50, _ 
"1") 
commandObj.Parameters.Append commandObj.CreateParameter(":param2", _ 
adInteger, _ 
adParamInput, _ 
10, _ 
2) 
i=0 
Do While i<5 
 commandObj(":param1")=chr(65+i) 
 commandObj(":param2")=(65+i) 
 commandObj.Execute 
 i=i+1 
Loop 
'delete allocated parameters 
commandObj.Parameters.Delete ":param2" 
commandObj.Parameters.Delete ":param1" 
'------------------------------------------------------------------------ 
'test Delete 
case "2" 
'------------------------------------------------------------------------ 
'Name: DeleteQuery 
commandObj.CommandText="DeleteQuery" 
'DELETE FROM testTable 
'WHERE id=[:param1]; 
commandObj.Parameters.Append commandObj.CreateParameter(":param1", _ 
adInteger, _ 
adParamInput, _ 
10, _ 
"1") 
i=0 
Do While i<5 
 '!!!id can be different!!!
 commandObj(":param1")=14 
 commandObj.Execute 
 i=i+1 
Loop 
'delete allocated parameters 
commandObj.Parameters.Delete ":param1" 
'------------------------------------------------------------------------ 
'test Update 
case "3" 
'------------------------------------------------------------------------ 
'Name: UpdateByid 
commandObj.CommandText="UpdateByid" 
'UPDATE testTable SET field1 = [:param1], field2 = [:param2] 
'WHERE id=[:param3]; 
commandObj.Parameters.Append commandObj.CreateParameter(":param1", _ 
adVarchar, _ 
adParamInput, _ 
50, _ 
"Z") 
commandObj.Parameters.Append commandObj.CreateParameter(":param2", _ 
adInteger, _ 
adParamInput, _ 
10, _ 
0) 
commandObj.Parameters.Append commandObj.CreateParameter(":param3", _ 
adInteger, _ 
adParamInput, _ 
10, _ 
0) 
i=0 
Do While i<5 
 commandObj(":param1")="14" 
 commandObj(":param2")=14 
 '!!!id can be different!!!
 commandObj(":param3")=15 
 commandObj.Execute 
 i=i+1 
Loop 
'delete allocated parameters 
commandObj.Parameters.Delete ":param3" 
commandObj.Parameters.Delete ":param2" 
commandObj.Parameters.Delete ":param1" 
'------------------------------------------------------------------------ 
End Select 
'deallocate commandObj 
Set commandObj=Nothing 
'close connection 
connObj.Close 
'deallocate connection object 
Set connObj=Nothing 
response.end
%> 
'--------------------------------------


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

4/20/2000 9:19:21 AMChris

I've been looking for an example of how to do this. Thanks for taking the time to post your code.
(If this comment was disrespectful, please report it.)

 
7/28/2000 9:29:47 AMBrian

Good code. LockwoodTech Query-Blaster will generate both the MSAccess stored queries and the ADO code to call it (in this format). See http://www.lockwoodtech.com/index_qb.htm
(If this comment was disrespectful, please report it.)

 
2/19/2001 7:02:36 PMMark

Thanks for the code, I have learnt alot reading through it, but I cannot get it to work. Can you tell me what the 'adovbs.inc' file is / does. Thanks again.
(If this comment was disrespectful, please report it.)

 
4/24/2001 8:03:46 AMStewart MacFarlane

Ok this code looks like it does everything i need, tho i havent tried it yet, is there any chance you could make a new version with more comments? include the database file too would be excellent. Anyway im going to have a go at this code even tho i dont fully understand it all. Thanx for your hard work. If you can manage do what i ask i would be grateful if you could email me at: Highlander@cableinet.co.uk
(If this comment was disrespectful, please report it.)

 
6/12/2001 7:58:58 AMnarsi_k@yahoo.com

sir,
iam working as a software engg,recently we did a portal called www.boozeguru.com.........now we have to use stored procedures in that portal
i have a little bit idea on stored procedures........ple mail me in detail about stored procdures with asp back end sql server
(If this comment was disrespectful, please report it.)

 
6/12/2001 7:59:08 AMnarsi_k@yahoo.com

sir,
(If this comment was disrespectful, please report it.)

 
4/18/2002 3:35:08 AMAbhi Malani

works good - i give it my rating
(If this comment was disrespectful, please report it.)

 
6/24/2002 11:02:11 PMina

Thanks for the code, I have learnt alot
reading through it, but I cannot get it
to work. Can you tell me what the
'adovbs.inc' file is / does. Thanks
again.

(If this comment was disrespectful, please report it.)

 
9/19/2002 3:46:41 AM

Error Type:
ADODB.Command (0x800A0BB9)
Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
accessTest.asp, line 23
'commandObj.CommandType=adCmdStoredProc
(If this comment was disrespectful, please report it.)

 
12/11/2002 9:23:14 AM

I have used this code twice. Great post.
(If this comment was disrespectful, please report it.)

 
5/2/2003 2:05:40 PM

Excelent code. It worked the first time I tried and saved me many hours work. Is there any example that will create
(If this comment was disrespectful, please report it.)

 
6/1/2003 9:45:11 PM

thank for the code that i get came from you..

me i have also the codes using SQL 7
(If this comment was disrespectful, please report it.)

 
8/3/2003 1:03:08 PM

Thanks for the code !
(If this comment was disrespectful, please report it.)

 
7/1/2006 8:19:12 AMpawan

very informative site for all the Developer
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.