singel quote in drop down

I'm building a search page that checks an Access database and displays all of tha matching results. The problem is I have about 6 choices in the drop-down list that have a single quote in them. Something like:

Dave's first choice

Since I can specify the value for this choice in the drop down list I think this may be the solution to my problem. The problem is I'm not sure if I should wrap the whole thing is double quotes or single quotes, or just the single quote in double quotes or what... What value should I specify for the option of, Dave's first choice" to work?

Who is Participating?
Göran AnderssonCommented:
Apostrophes (') is no problem for HTML. You only have to handle them in the SQL call.

Use this:
strSQL = "SELECT * from Online_For_Agency_Reps Where Agency = '" & Replace(Agency,"'","''") & "'"
Have you tried various options to see?

"Dave's first choice" should work as a string
so should
'Dave''s first choice'

RotigelAuthor Commented:
No luck. I tried both options :(
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Post your code so we will know what you actually wanted.

THe single quote shouldn't be a problem.

<option value='" & RS("whatever") & "'">" & RS("whatever") & "</option>"

Use a single followed by a double for the value.

Hope that helps.

Using SERVER.HTMLEncode():

<option value="<%=Server.HTMLEncode(RS("whatever"))%>"><%=RS("whatever")%></option>

Wee Siong
Try this

Response.Write "<option value=""" & RS("whatever") & """>" & RS("whatever") & "</option>"

And let me know if it helped
Michel SakrCommented:
No need to double the things while displaying.. in your action page where you post data to simply set a replaces before querying the database like:

TheDropValue = Replace(TheDropValue,"'","''")

I added blanks so you can distinguish the above line:

TheDropValue = Replace(TheDropValue,"   '   ","   '   '   ")

RotigelAuthor Commented:
Hummmmmm, I'm not having much luck here. Here is the drop down from the first page:

<form method="POST" action="Results.asp">
  <p align="left"><font face="Tahoma" size="2"><b>Select your agency from the list
  <p align="left"><font face="Tahoma" size="2"><select size="1" name="agency">
    <option value="Aging">Aging</option>
    <option>City of Harrisburg</option>
    <option>Civil Service Commision</option>
    <option>Community &amp; Economic Development</option>
    <option>Conservation &amp; Natural Resources</option>
    <option>County of Dauphin</option>
    <option>Dauphin County HHS</option>
    <option>Environmental Protection</option>
    <option>Executive Offices</option>
    <option>Fish &amp; Boat Commission</option>
    <option>Game Commission</option>
    <option>General Services</option>
    <option>Health Care Cost Containment Council</option>
    <option>Highmark Inc.</option>
    <option>Historical &amp; Museum Commission</option>
    <option>Human Relations Commission</option>
    <option>Labor &amp; Industry</option>
    <option>Liquor Control Board</option>
    <option>Military &amp; Veterans' Affairs</option>
    <option>Office of Administration</option>
    <option>PA Emergency Management Agency</option>
    <option>PA Higher Education Assistance Agency</option>
    <option>PA Municipal Retirement System</option>
    <option>PA School Boards Association</option>
    <option>Probation &amp; Parole</option>
    <option>Public School Employes' Retirement Sys</option>
    <option>Public Utility Commission</option>
    <option>Public Welfare</option>
    <option>Public Welfare - Allegheny CAO</option>
    <option>Public Welfare - Bertolino</option>
    <option>Public Welfare - H&amp;W</option>
    <option>Public Welfare - Hsbg State Hospital</option>
    <option>Public Welfare - Loysville Complex</option>
    <option>Public Welfare - Torrance State Hospital</option>
    <option>Securities Commission</option>
    <option>Sproule &amp; Associates</option>
    <option>State Civil Service Commission</option>
    <option value="State Employee's Retirement System">State Employee's
    Retirement System</option>
    <option>State Police</option>
    <option>State System of Higher Education</option>
    <option>The Curtis Group</option>
    <option>Turnpike Commission</option>
    <option>Washington Group International, Inc.</option>
  <p align="left"><font face="Tahoma" size="2"><input type="submit" value="Submit" name="B1">&nbsp;&nbsp;
  <input type="reset" value="Reset" name="B2"></font></p>

+++++++++++end of drop-down+++++++++++++

And here is the results page:

<%@ Language=VBScript %>
<% Option Explicit %>
<% Response.Buffer = True %>

<!--#include file=""-->
<!--#include file="DatabaseConnect.asp"-->


 dim Agency
 Agency = request.form("agency")
 Dim strSQL
 strSQL = "SELECT * from Online_For_Agency_Reps Where Agency = '" & Agency & "'"

 Dim objRS
 Set objRS = Server.CreateObject("ADODB.Recordset")
 objRS.Open strSQL, objConn

 If objRS.eof Then    
 Response.Write "<center>There are no members in that Agency.  Click the back button and try again.<br><br></center>"    

<title>Agency Membership Results</title>

<center><b><font size="5" color="#093A7F" face="Tahoma"> Members in
</font></b><br>&nbsp;<%="<b><font size=5 color=#093A7F face=Tahoma>" & Agency & "</b></font>"%></center>
<font face="Tahoma">
 <div align="center">
 <TABLE bordercolor="#093A7F" border=1 width="100%">
          <TD align=center><B>Agency</B></TD>
          <TD align="center"><B>First</B></TD>
          <TD align="center"><B>MI</B></TD>
          <TD align="center"><B>Last</B></TD>
          <TD align="center"><B>Exp</B></TD>
          <TD align="center"><B>Email</B></TD>
    Do While Not objRS.EOF
        Response.Write "<TR><TD>" & objRS("agency") & "</TD>"
        Response.Write "<TD>" & objRS("FName") & "</TD>"
        Response.Write "<TD>" & objRS("MI") & "</TD>"
        Response.Write "<TD>" & objRS("LName") & "</TD>"
        Response.Write "<TD>" & objRS("Exp") & "</TD>"
        Response.Write "<TD>" & objRS("WorkEmail") & "</TD>"

    'Clean up our ADO objects
    Set objRS = Nothing

    Set objConn = Nothing




<p><font face="Tahoma"><b><a href="Default.asp">Search Again</a> | <a href="../../">HOME
Home Page</a></b>
End If

Replace single quote with some identifier (like "zzz") and later in next page replace that
Identifier by single Quote. This is one of the work arounds you can try.

for example,
.Write "<option value='" & replace(RsSub("myvalue"),"'","zzz") & "'>"     & RsSub("myvalue") & "</option>"    

In the second page,

Dim MyOptionvalue    
MyOptionvalue=trim(Request.QueryString("selvalue")) 'selvalue is your list box

if instr(MyOptionvalue,"zzz")>0 then MyOptionvalue=replace(MyOptionvalue,"zzz","'")    

I have tried the same example and it is working fine.

Best of luck
Michel SakrCommented:
huh didn't you read my post.. nothing should be changed while displaying.. only after you post and fetch the listbox info that will populate the sql query

Replace your quotes in the select line with chr(34)like this:-

strSQL = "SELECT * from Online_For_Agency_Reps Where Agency = " & chr(34) & Agency & chr(34)

Göran AnderssonCommented:
Sorry, Silvers5, I kind of lost your perfectly good solution in the overflow of misleading suggestions. Points to Silvers5, if you find this solution sufficient, Rotigel.

TDK, we usually DON'T post a suggestion as a proposed answer, as that moves the question to the locked list.
If you have a lot of records in your database, and in your "Agency" field you are saving these long names - for each record, you probably will end up wasting a lot of space.

Personally I would switch to  three or four digit code for each dept and assign that as the value in the drop-down ie 'corr' for corrections, and 'sshe' for State System of Higher Education.  Then you wouldn't have this problem anyway.  Or you may want to move over to a relational database model if your data has to be meaningful.

Jsut a thought.

Ah, okay - thanks for the tip.
RotigelAuthor Commented:
Didn't work :(
RotigelAuthor Commented:
That works! Thanks a million!!!!
Michel SakrCommented:
and what I said was different? DOH!
RotigelAuthor Commented:
  I tried your answer and it did not work. I then tried the answer that I accepted and it did work. I justy tried yours again and it worked - so it must have been some problem on my part. Since you were first I'm going to give you 50 points. Sorry about this, it was my fault.

Thanks -
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.