Solved

Access 2007, VBA: Question about listboxes, multiple selections

Posted on 2011-02-15
10
1,120 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
ID: 34901893
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
ID: 34902102
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
ID: 34902113
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
ID: 34902129
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
ID: 34902174
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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 119

Expert Comment

by:Rey Obrero
ID: 34902214
can you upload a copy of the db.
0
 

Author Comment

by:Bevos
ID: 34902247
Sure, thanks!
Example---Copy.accdb
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 34902398
0
 

Author Comment

by:Bevos
ID: 34902462
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
ID: 34902765
<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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

919 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

17 Experts available now in Live!

Get 1:1 Help Now