Access 2007, VBA: Question about listboxes, multiple selections

Hello, I have been working on a database with the help of some of the users of experts exchange and I was hoping I could get some comments about how to change how my form's list boxes are functioning.  I have several list boxes (in multi-select mode simple) all of which pass their values to a variable nst and then this value is passed onto the [notes] field on my form/table.  The way the list boxes function now works pretty well, but when a user attempts to deselect his or her listbox choice the entire field nst is cleared.  The code for the list boxes right now is as follows:

Does anyone know if it is possible to accomplish what I have requested, and if so how should the above code be modified?

Thanks in advance for any comments,
Bevo

Private Sub List58_AfterUpdate()

With Me.List58
If .Selected(.ListIndex) = True Then
    If nst = "" Then
        nst = .Column(0, .ListIndex)
        Else
                  If InStr(nst, .Column(0, .ListIndex)) = 0 Then
            nst = nst & ", " & .Column(0, .ListIndex)
        End If

    End If
   
    Else
    If InStr(rst, ",") > 0 Then
        If InStr(nst, .Column(0, .ListIndex)) = 1 Then
            nst = Replace(nst, .Column(0, .ListIndex) & ", ", "")
        Else
            nst = Replace(nst, ", " & .Column(0, .ListIndex), "")
        End If
        Else
        nst = ""
    End If
End If

End With
Me.[Notes] = nst
End Sub
BevosAsked:
Who is Participating?
 
Rey Obrero (Capricorn1)Connect With a Mentor Commented:
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Did you copy/paste this? If so, you've got a type in the main Else section, I believe:

If InStr(rst, ",") > 0 Then

I would think that "rst" should be "nst"
0
 
BevosAuthor Commented:
Hi, there was a typo there, thanks for spotting it!  However, after correcting this code I still have an issue.  Items that are deselected no longer cause the whole field to become erased, but they also are not removed from the notes field either.  Any thoughts?

Thank you
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
BevosAuthor Commented:
This issue only takes place if multiple selections are made in the list box.  If a single selection is made, it can be removed by clicking that entry again.
0
 
Rey Obrero (Capricorn1)Commented:
the codes should work as they are (exclude the typo).

is the text string listed in the [notes] control coming only from list58?
0
 
BevosAuthor Commented:
Hi again capricorn1,  no it is coming from several list boxes.  I put the code here as an example not thinking that it would be effected.  Currently there are 12 list boxes using the code from above.
0
 
Rey Obrero (Capricorn1)Commented:
can you upload a copy of the db.
0
 
BevosAuthor Commented:
Sure, thanks!
Example---Copy.accdb
0
 
BevosAuthor Commented:
Works completely ... I'm checking out the VBA code and it seems the same as what's posted above.  Mind if I ask how you addressed this?
0
 
Rey Obrero (Capricorn1)Commented:
<I'm checking out the VBA code and it seems the same as what's posted above.>

yes that is how all the codes from the listboxes afterupdate event should look like.
0
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.