Results 1 to 5 of 5
  1. #1
    Gina Maylone is offline Always learning
    Windows 7 64bit Access 2013
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544

    First item selected not showing up in text string

    Hello all!



    This is a continuation of a previous post. I have an unbound listbox "Needs", and in the onclick event have the following code which works great if I have more than one item selected, but if I only have one item selected, it does not put it in the text box "ServiceText". Thoughts? TIA!

    Code:
    Private Sub Needs_BeforeUpdate(Cancel As Integer)
    On Error GoTo errHandler
    Dim ctl As Control
    Dim strStart As String, strEnd As String
    Dim aryList As String
    Dim varSelected As Variant
    
      If Me.ServiceText = "" Then
      Me.ServiceText = Me.csvtext
      End If
      
    Set ctl = Me!Needs
    '  Me.Needs = Me.Needs.ItemData(0)
      Select Case ctl.ItemsSelected.Count
       Case Is = 0
          MsgBox "You haven't selected anything"
          Me.ServiceText = ""
          
       Case Is = 1
          aryList = ctl.Column(1, ctl.ItemsSelected(0))
       Case Is > 1
          For Each varSelected In ctl.ItemsSelected
              aryList = aryList & ctl.Column(1, varSelected) & ", "
          Next varSelected
          aryList = Left(aryList, Len(aryList) - 2)
          strStart = Left(aryList, InStrRev(aryList, ",") - 1)
          strEnd = Mid(aryList, InStrRev(aryList, ",") + 2)
          aryList = strStart & " and " & strEnd
          Me.ServiceText = aryList
          
    End Select
    Exit Sub
    errHandler:
      MsgBox "Error " & Err.Number & ": " & Err.Description & " in " & _
       VBE.ActiveCodePane.CodeModule, vbOKOnly, "Error"
    End Sub

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,549
    There is a LOT of code here. Usu you have a list box of values and the user picks one and it is assigned to the field.
    A bound list box plugs the value into the field using the combo bound column.
    NO code.

    what does all your code do that the above method cant?

  3. #3
    JamesDeckert is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Jul 2015
    Location
    Salina, KS
    Posts
    262
    this line is only in the case is >1
    Me.ServiceText = aryList
    it should also be in the
    case is = 1

  4. #4
    Gina Maylone is offline Always learning
    Windows 7 64bit Access 2013
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    Quote Originally Posted by ranman256 View Post
    There is a LOT of code here. Usu you have a list box of values and the user picks one and it is assigned to the field.
    A bound list box plugs the value into the field using the combo bound column.
    NO code.

    what does all your code do that the above method cant?
    Yea, I need more than one selection written to a string separated by commas with the last selection preceded by "and". It's used in another process. Multi select list boxes do not save that way.

    Thanks.

  5. #5
    Gina Maylone is offline Always learning
    Windows 7 64bit Access 2013
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    Quote Originally Posted by jamesdeckert View Post
    this line is only in the case is >1
    me.servicetext = arylist
    it should also be in the
    case is = 1
    perfecto!!! Duh! Thank you jamesdeckert!!!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 06-24-2015, 02:45 PM
  2. Replies: 6
    Last Post: 05-29-2015, 10:21 AM
  3. Filter on selected item in combox if not empty
    By Mbakker71 in forum Access
    Replies: 8
    Last Post: 03-05-2014, 03:03 PM
  4. Combo box - Open at last item selected
    By mitchy1111 in forum Programming
    Replies: 2
    Last Post: 10-07-2011, 07:23 AM
  5. Replies: 7
    Last Post: 08-31-2011, 05:09 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums