Link to home
Start Free TrialLog in
Avatar of marian68
marian68

asked on

multi-selection VBA code in a list box

I would like to build a list box where I could choose 2 or 3 items from a single list,I mean 2 or 3 items from a list of 5 items - multi-select property of the list box.
 Can you give me the VBA code to do that ? In Access 2007 this issue has been solved but in access 2003 I need a VBA code.
Thank you
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

The same code should work in either. See this link:

http://msdn.microsoft.com/en-us/library/aa196172(v=office.11).aspx

Basically, you loop through the ItemsSelected collection.
I'm not clear why you expect the code for A2003 to be any different?
I'm not aware of any differences in the list box between the two versions of Access.
Avatar of marian68
marian68

ASKER

LSM Consulting: The examples in the site are for printing. I am not a specialist in VBA code. All I want to strore in my table 2 or 3 concatenated items from my list box. I found the below code but it doesn't work. Maybe you can tell me why:
"Private Sub cmdStoreAllSelections_Click()
Dim SelectedValues, item

For Each item In lstItems.ItemsSelected
    If SelectedValues > "" Then
        SelectedValues = SelectedValues & "," & lstItems.ItemData(item)
    Else
        SelectedValues = lstItems.ItemData(item)
    End If
Next item
Me!CompoundValue = SelectedValues
End Sub"
Thank you


The link you posted refers to a new feature in A2007 called mult-valued fields.
These are nothing to do with standard listboxes or comboboxes.  The code you posted is used with listboxes NOT muti-valued fields.

There is NO equivalent to multi-valued fields in any previous version of Access.
mult-valued  s/b multi-valued
Ok I will rephrase my problem.
I have the following list box
aaaaaaaaaaaaaa
bbbbbbbbbbbbbb
cccccccccccccccccc
dddddddddddddd
eeeeeeeeeeeeee
I would like to be able to select more than 1 item lets say first 3 items and to have in my table:aaaaaaaaaaaa,bbbbbbbbbbbbbbbb,cccccccccccccc
Can anyone give the code for this?
Thank you
Capricorn where are you?
You can set the multi-select value to simple, and then you can select multiple items in a list box.

Then on a button click you would do something like

Private Sub Button1_Click()
Dim varItem as variant
Dim strSelect as string
Dim strSQL as string

For Each varItem In Me.lstBox.ItemsSelected
  strSelect = strSelect & lstBox.ItemData(varItem) & ";"
Next

strSQL = "Update [YourTable] Set [YourField]='" & strSelect & "' Where [YourTableID]=" & Me.ID
CurrentDb.execute strSQL, dbFailOnError

End Sub
<Capricorn where are you?>

That's quite rude. If you want cap to help you exclusively, maybe you should contact him directly and hire him. I'm sure he'd appreciate the work.

First: Storing data in this manner is NOT the proper way to manage this. Instead, you should build a table to hold the values selected, and use that to store the user's selections. You can then build a Subform that looks like a listbox, and use that instead of a listbox. This manages the storage for you.

That said, if you insist on storing denormalized data then the code you referenced here should work, with a few tweaks:

Dim SelectedValues As String
Dim oitem As Variant

For Each oitem In lstItems.ItemsSelected
    If SelectedValues > "" Then
        SelectedValues = SelectedValues & "," & lstItems.ItemData(oitem)
    Else
        SelectedValues = lstItems.ItemData(oitem)
    End If
Next item

Me!CompoundValue = SelectedValues

Assuming your Listbox is named "lstItems", and the field where you want to store this denormalized data is named "CompoundValue" (and is a string), then that should work.

However, this is only half the battle. What happens when the user then is simply viewing the data? You must "split" the data you've stored in CompoundValue, loop through the Items in the Listbox, and Select/Deselect them based on your stored, denormalized data. Also, reporting on this sort of data is a real bear as well, since you cannot easily determine how many users have selected the choice of "aaaaaaaaaaaaaaaa", for example.

That's why we stress storing data properly, in a normalized fashion.
I gave you the answer in your previous question.
Sorry LSMConsulting if you find my call "rude" but I come here with this list box because I am trying to find out how to store a string using a check box but nobody managed to help me.
Shortly I have 5 check boxes and I would like to have the label of my check stored as a string in my table.
"Private Sub T7_Click()
If Me.T7 Then
  T7 = "aaaaaaaaaaaaaaaaaaa"
Else
  T7 = ""
End If
End Sub"
aaaaaaaaaaaaaaaaaa being also the label of the check box.
Not finding a solution to that I decided to build a list box where I have all 5 strings. More than 1 string can be required.
thank you
marian68, is this question related to your question from this post;

https://www.experts-exchange.com/questions/26829402/check-boxes-in-forms-access-2003.html

The experts are right, storing these values from your list box is only half the battle, you really need another table to store the data.

ASKER CERTIFIED SOLUTION
Avatar of VTKegan
VTKegan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry again for my call out.
For DockieBoy: I tried your solution and it is working but can you tell me please what did you mean with the :"Again, i think this will cause more trouble than it's worth"? Do you have an easier solution to this problem?
Thank you
marian68, just to clarify, what I mean is, although that will work for you, I think it will cause you problems.  

As the experts have said (and they are far more experienced and qualified than I), this is not the way to go about it.

There are many "solutions", but I would not suggest that they are easier, however, they are far better.

The method that I have given you, while it may be working for you, will cause problems in that it will prevent normalization and make data recall and manipulation difficult.

For the alternative solutions please see the posts from the experts.  :)  
Thank you for your reply.
I noted a small problem. When I surfing between the records I can not see the small check in the box, in other words, the user if he browses through his records can not see what he checked and not.
Maybe  you can help me.
Thank you
Use the on current event for the form and the value of your hidden text boxes to set the value of your check boxes, for example, if one of your text boxes is T1 and check box is Chk1;

Select Case Nz(Me.T1,"")
 Case ""
 Me.Chk1 = False
 Case Is <> ""
 Me.Chk1 = True
 End Select