Tuesday, 29 November 2011

DISPLAY PART OF DATABASE FIELD

There are times when you may only need to display part of the data from a complex database field on your web page. For example, you may have a Location field that contains the city where your office is located and also the driving directions to your office. Normally, these data types will be separated by a comma, colon, or something similar. So how do you separate this data on the web?

The first thing you need to do is create a database called MyDatabase. Then create a table called tblLocations with these fields:
ID - autonumber
Location - text field
DateEntered - date/time field *** Also add "=Date()" as the default value for the field. This will add the date automatically, everytime a new record is entered. ***

Then, start adding a new record and enter "Miami : Take a left on Harris Street, right on Morgan Avenue, and we are the third building on the left". Enter some more records in the same fashion if you like.

Next, you create a page called displaypartoffield.asp and copy the below code into your page:



<% DIM mySQL, objRS mySQL = "SELECT * FROM tblLocations" Set objRS = Server.CreateObject("ADODB.Recordset") objRS.Open mySQL, objConn %>

<% DO WHILE NOT objRS.EOF %>

<% DIM strLocation strLocation = Left(objRS("Location"), InStr(1, objRS("Location"), ":") - 1) Response.Write strLocation %>

<% objRS.MoveNext Loop objRS.Close Set objRS = Nothing objConn.Close Set objConn = Nothing %>

This script will return all of the records in your database and will display only the data to the left of the ":" in your Location field. If you want to display only the data to the right of the ":" , substitute the below code for the above.

<% DIM strLocation strLocation = Mid(objRS("Location"), InStr(1, objRS("Location"), ":") + 1) Response.Write strLocation %>

That's it, happy displaying!

http://www.aspwebpro.com/aspscripts/database/displaypartoffield.asp

No comments:

Post a Comment