Computer Learning Zone CLZ Access Excel Word Windows

The function of education is to teach one to think intensively and to think critically. Intelligence plus character - that is the goal of true education.

-Martin Luther King Jr.
 
Home   Courses   Seminars   Templates   Help   TechHelp   Forums   Contact   Join   Order   Logon  
 
Tips & Tricks

New Tips Added Weekly!
Click here to get on our Mailing List

   
 
ASP

Query String

How to handle a Query String used in parameterised Query

In this tutorial you will learn how to handle parameters passed from a Query String.

       
	'CORRECT WAY - Parameterized Query with dynamic sql
	'--------------------------------------------------
	strSQL = "SELECT * FROM users WHERE username=? AND password=?"
	Dim cmd1
	Set cmd1 = Server.CreateObject("ADODB.Command")    
	cmd1.ActiveConnection = cnnLogin
	cmd1.CommandText = strSQL
	cmd1.CommandType = adCmdText
	cmd1.Parameters(0) = Request.Form("login")
	cmd1.Parameters(1) = Request.Form("password")
	Set rstLogin = cmd1.Execute()
	
 
	'BAD WAY WITH CONCATENTATION DON'T DO IT!!!
	'------------------------------------------
	strSQL = "SELECT * FROM users WHERE username='" & Request.Form("login") & _
		"' AND password='" & Request.Form("password") & "';"
	Set rstLogin = cnnLogin.Execute(strSQL)
	

What if you want a LIKE clause in your Query?

	SELECT * FROM tblX WHERE Field LIKE '%' & Name & '%'
	

Tried this but it didn't work.

	SELECT * FROM tblX WHERE Field LIKE '%?%'
	

Include ADOVBS.inc in your page to use the Constants.

	'Set parameter = command.CreateParameter (Name, Type, Direction, Size, Value)
	'cmd1.Parameters.Append(cmd1.CreateParameter("Name", 200, 1, 512, name))
	cmd1.Parameters.Append(cmd1.CreateParameter("Name", 200, 1, 512, "%" & name & "%"))
	

Or you could wrap it before you assign:

	cmd1.Parameters(0) = name
	
	cmd1.Parameters(0) = "%" & name & "%"
	

 

Alex Hedley (Avatar) By: Alex Hedley


Click here to sign up for more FREE tips

 

 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Account Login
Online Theater
Downloads
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Email Richard
Mailing Address
Phone Number
Fax Number
Course Survey
Blog RSS Feed    Twitter

YouTube Channel    LinkedIn