A parameter query asks the user to enter a value when the query runs. Microsoft Access uses the entered value to filter records. Parameter queries are useful when you want to search for different students, courses, fees, or dates without creating a separate query for every search.
A parameter query is a query that displays an input prompt and uses the value entered by the user as a criterion.
| StudentID | StudentName | Course | Fees |
|---|---|---|---|
| 101 | Amit | ADCA | 10000 |
| 102 | Priya | Tally Prime | 4000 |
| 103 | Rahul | ADCA | 8000 |
| 104 | Neha | Web Development | 12000 |
Suppose you want to find students by course. Instead of writing a separate query for ADCA, Tally Prime, and Web Development, you can create one parameter query.
Follow these steps to create a parameter query:
[Enter Course Name:].qryStudentsByCourse.[Enter Course Name:]
When the query runs, Access asks the user to enter a course name. If the user enters ADCA, records for that course are displayed.
You can create a parameter query directly in SQL View.
PARAMETERS [Enter Course Name:] Text (255);
SELECT *
FROM Students
WHERE Course = [Enter Course Name:];
The PARAMETERS declaration identifies the input parameter as text. Access prompts for its value when the query runs.
PARAMETERS [Enter Student Name:] Text (255);
SELECT *
FROM Students
WHERE StudentName = [Enter Student Name:];
Enter a student's name to display the matching record.
You can also prompt the user to enter a number, such as a minimum fee amount.
PARAMETERS [Enter Minimum Fees:] Currency;
SELECT StudentName, Fees
FROM Students
WHERE Fees > [Enter Minimum Fees:];
If the user enters 5000, the query returns students whose fees are greater than 5000.
PARAMETERS [Enter Minimum Fees:] Currency;
SELECT StudentName, Fees
FROM Students
WHERE Fees >= [Enter Minimum Fees:];
This version also includes students whose fees are exactly equal to the entered amount.
You can use more than one parameter in a single query.
PARAMETERS
[Enter Course Name:] Text (255),
[Enter Minimum Fees:] Currency;
SELECT StudentName, Course, Fees
FROM Students
WHERE Course = [Enter Course Name:]
AND Fees >= [Enter Minimum Fees:];
When this query runs, Access prompts for:
If the user enters ADCA and 5000, the query returns ADCA students whose fees are at least 5000.
Parameter queries can ask the user to enter a date. This is useful for searching admission records or transactions within a date range.
PARAMETERS [Enter Admission Date:] DateTime;
SELECT StudentName, AdmissionDate
FROM Students
WHERE AdmissionDate = [Enter Admission Date:];
The user enters a date and Access displays records matching that date. This exact comparison is most suitable when the field contains dates without different time components.
PARAMETERS
[Enter Start Date:] DateTime,
[Enter End Date:] DateTime;
SELECT StudentName, AdmissionDate
FROM Students
WHERE AdmissionDate >= [Enter Start Date:]
AND AdmissionDate < [Enter End Date:] + 1;
If the user enters a start date and an end date, this pattern includes the full end date when the field contains times as well as dates.
You can use a parameter with LIKE to search for names that contain or begin with user-entered text.
PARAMETERS [Enter Starting Letters:] Text (255);
SELECT StudentName, Course
FROM Students
WHERE StudentName LIKE [Enter Starting Letters:] & "*";
If the user enters A, the query finds names beginning with A in Access's default ANSI-89 wildcard syntax.
PARAMETERS [Enter Text:] Text (255);
SELECT StudentName, Course
FROM Students
WHERE StudentName LIKE "*" & [Enter Text:] & "*";
If the user enters "an", the query searches for names containing that text sequence.
* matches zero or more
characters. Databases using ANSI-92 syntax generally
use % for the equivalent wildcard in SQL
patterns.
A school administrator can use one parameter query to search for students in ADCA today and Tally Prime tomorrow. There is no need to create a separate query for every course.
| Problem | Possible Solution |
|---|---|
| Access displays an unexpected parameter prompt. | Check for misspelled field names or parameter names. |
| No records are returned. | Verify the entered value and the field's actual data. |
| Numeric input causes a data-type error. | Declare the parameter as Number, Currency, or the appropriate numeric type. |
| Date criteria return unexpected results. | Check the Date/Time field type, date format, and time components. |
| Text search does not find expected matches. | Check the field name, spelling, wildcard syntax, and Access query mode. |
Create a table named Students with these fields:
Goal: Learn to create reusable parameter queries with text, numeric, and date inputs.
1. What does a parameter query ask the user to do?
2. Which parameter data type is appropriate for a minimum fee amount?
3. What is a major advantage of parameter queries?