Have you ever had to enter information from an HTML form and place it into a database memo field only to display the information back to the user and try to format it the way they put it into your form field?
You could use the "
" tags to format the data so it's displayed exactly as the user entered it. The only problem using this method is it won't wrap the text to the next line. It will just display the text as one line and not start a new line unless the user actually placed a carriage return in the entered data. This could really distort your page and cause some really serious formatting problems.
The way around this is to use the VBScript Replace function in your ASP code to change each occurrence of a carriage return line feed into the equivalent html tag. This way it will handle the data as any other html text and wrap it when it needs to be wrapped and place a "
" or "
" tag where there needs to be one.
For example we look for the corresponding ascii character code and then replace it with the appropriate html tag:
strMemo = Replace(strMemo, Chr(13), "
The Replace function is a really nice function to have some other handy items for it are preventing your SQL statements from crashing when you try to insert a string into a field that contains an apostrophe, say a name like O'Hara. In order to filter for data like this use the Replace function to double them up:
strField = Replace(strField, "'", "''")
Now you have some simple insurance against one of the most common problems encountered by beginning ASP developers.
Building SQL Statements
Another item for using the Replace function, that I use is for capturing the space in an entered field for keyword searching and then replacing the space with the appropriate SQL code. Say a user enters two keywords in a search field like "ASP Engine", and you want to be able to do a search on a field for both of these values, or any of the values, or the exact phrase. How do you do this with minimal amount of code? Use the Replace statement:
Keywords = Replace(Request.Form("Keywords"), "'", "''")
'my form value with the apostrophes filtered.
'now build the SQL code....
MySQL = "SELECT FROM MyDatabase WHERE "
MySQL = MySQL & "MyField LIKE '%" & _
Replace(Keywords, " ", "%' AND MyField LIKE '%") & "%'"
So what we did here is replace the space the user entered with an AND statement in order to make criteria that a field would have to have both keywords in it for it to be pulled. Now you could replace the AND with an OR and this way the statement will search for either of the words in the field.