Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS Visual Basic 6.0/MS Access - Multiselect option

Status
Not open for further replies.

jbanker

Programmer
Joined
Jul 17, 2002
Messages
7
Location
US
I am trying to set up a form in Access with a list box that will let me select several items from a list, then save the items chosen to a database text box. Can this be done, and if so, how.

I have the multiselect listbox set up and it's working fine - just doesn't go anywhere. I think my answer lies somewhere in the Select Property, but I'm new at VB and not sure how to proceed. I want to be able to save the selected data to a single field in my database if possible.

Any suggestions? Like the specific code? :-)
 
The example below will loop through the listbox and add any selected items to the strItems string

Hope this is of some help.

Private Sub Command0_Click()
Dim intCount As Integer
Dim strItems As String

For intCount = 0 To List1.ListCount - 1
If List1.Selected(intCount) Then
strItems = strItems & List1.Column(0, intCount)
End If
Next intCount

MsgBox strItems

End Sub
 
Thanks for the reply. I think this is the right track, but I'm still having trouble getting it to work. When I set up the Event Procedure, do I have to define ListCount elsewhere?

The name of my listbox is Command0, the control source is Ext1. I changed the code to this:

Private Sub Command0_Click()
Dim intCount As Integer
Dim strItems As String

For intCount = 0 To Ext1.ListCount - 1
If Ext1.Selected(intCount) Then
strItems = strItems & Ext1.Column(0, intCount)
End If
Next intCount

MsgBox strItems

End Sub

When I do this, I get the message: "Compile Error: Method or Data Member not found." It then highlights the ListCount item.

I really appreciate your assistance, since this is a new area for me.
 
ListCount is a property of the ListBox in the code you've got there your trying to us it on your source.

Instead if you want to write all the selected items from the original code I posted exchange the

msgbox strItems

for

txtBox.text = strItems

where txtBox is the name of the textbox you want to write the value to.

 
OK, this is what I'm putting in my Event Procedure:

Private Sub Command0_Click()
Dim intCount As Integer
Dim strItems As String
For intCount = 0 To List1.ListCount - 1
If List1.Selected(intCount) Then
strItems = strItems & List1.Column(0, intCount)
End If
Next intCount

Ext1.Text = strItems
End Sub

When I do this, I get the message: "Compile Error: Method or Data Member not found." Now it highlights the .Text item.

Don't give up on me. I think we're getting closer!! :-D
 
Sorry my mistake! Getting confused between vb and Access.

If your form is bound to your record set put a text box on your form and make it bound to the field that you want to update. Say for example call it txtBox, make it bound to the field you want to update in your record set, say fldNames.

In your code replace

Ext1.Text = strItems

with

txtBox = strItems

Because this text box is bound to your recordset it will automatically write this data to the current record.

Hopefully closer still !!!!! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top