Solved

Access 2007, VBA: Question about listboxes, multiple selections

Posted on 2011-02-15
10
1,108 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:Bevos
  • 5
  • 4
10 Comments
 
LVL 84
Comment Utility
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
 

Author Comment

by:Bevos
Comment Utility
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
 

Author Comment

by:Bevos
Comment Utility
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
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
the codes should work as they are (exclude the typo).

is the text string listed in the [notes] control coming only from list58?
0
 

Author Comment

by:Bevos
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
can you upload a copy of the db.
0
 

Author Comment

by:Bevos
Comment Utility
Sure, thanks!
Example---Copy.accdb
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
Comment Utility
0
 

Author Comment

by:Bevos
Comment Utility
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
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
<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

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now