ASP Tip: Populating form select fields from database
In relation databases very often the different options for a field are stored in a different table. Populating the <OPTION> fields reading the values from a table can then be very helpful and simplify the maintenance when those fields change.
For that purpose I created several functions that help me doing it in different formats.
All the functions are saved in an include file (remember you should call it *.asp not *.inc). These are the functions contained:
Function ExpSelectID ( Table, DescField, RetID)
Function ExpSelectCondID ( Table, DescField, RetID, Cond, MsgError)
Function ExpSelect ( Table, DescField, RetID)
Function ExpSelectCond ( Table, DescField, RetID, Cond)
This is the meaning of the parameters used:
| Table: | Name of the table |
| DescField: | Name of the field with the description |
| RetID: | Name of the value field |
| Cond: | SQL condition to be applied |
| MsgError: | Default value |
Now you can see an example of the output for each function:
ExpSelectID:
<OPTION VALUE="RetID">RetID - Description
ExpSelectCondID:
<OPTION VALUE="RetID">RetID - Description
Only the records that match the condition Cond
ExpSelect:
<OPTION VALUE="RetID">Description
ExpSelectCond:
<OPTION VALUE="RetID">Description
Only the records that match the condition Cond
And finally here is the code for the include file in a text file.
Author: Carlos Baez
You can also submit your own code examples. Just e-mail them.
Read the disclaimer.
|