We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 39,249 times

Downloads

Related Categories

Update Recordset using a MultiSelect Listbox

julesr

A common scenario that developers may encounter is to associate many database records with a record from another table. The example that I'll use is to associate a number of facilities with a particular property. On a webpage, the user can insert a property into the database. The page will also have a multiselect listbox for the user to associate many facilities with that property. The facilities are then stored as a comma delimited string in the Properties table.

The main problem that developers have is updating the property record. On the update page, the associated facilities in the listbox should be pre-selected. It's not immediately obvious on how to do this. You can download the support files by clicking the link at the bottom of this article. The important code is for the listbox on the edit.asp page

<select name="Facilities" size="5" multiple id="Facilities">
<%
While (NOT FacilitiesRS.EOF)
    Facilities=ListingsRS("Facilities") & ""
    IsSelected=false
    'Put the comma separated facilities into an Array
    Arr=Split(Facilities,",")
    for i=0 to ubound(Arr)
        if trim(Arr(i))=FacilitiesRS("Facility") then
            IsSelected=true
            Exit for
        end if
    next
    %>
  <option <% if IsSelected then Response.Write "selected" %> value="<%=(FacilitiesRS.Fields.Item("Facility").Value)%>">
  <%=(FacilitiesRS.Fields.Item("Facility").Value)%>
  </option>
    <%
    FacilitiesRS.MoveNext()
Wend
If (FacilitiesRS.CursorType > 0) Then
    FacilitiesRS.MoveFirst
Else
    FacilitiesRS.Requery
End If
%>
</select>

Note how the original value is split into an array. The code then loops through the arrayt to compare it to the value in the listbox. If there is a match, we can set the option to be selected.

Also, note that this is a quick'n'dirty solution. Technically, one should use a separate table to store the relationships.

I've been a freelance web developer since Feb '00. Prior to that, I'd spent most of my working life in the pub trade. I'd also worked as a security guard in a local factory. Luckily, I was able to my laptop into work. It was there that I used to spend my 12 hour shifts learning about web development! Over the years, I have worked on a lot of interesting sites. You can see a list of more recent sites in my portfolio. And acquired a string set of technical skillsets. These include: * ASP/VB Script * ASP.NET/C# * SQL Server 2000/Access 2000 * XML/XSLT It should be noted that my design skills are practically zero. Jojo, from the DW newsgroup, did the design for this site. I just put it all together in Dreamweaver MX. I don't really have much time for hobbies or pastimes. When I do get some spare time, I enjoy playing scrabble, doing crosswords and playing golf. At one time, I got down to a single-figure handicap at golf. But, I'd struggle to maintain that now!

Comments