Page 2 of 2 FirstFirst 12
Results 16 to 25 of 25
  1. #16
    Gina Maylone is offline Always learning
    Windows 7 64bit Access 2013
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    I'm sorry I thought it was apparent in my code


    [CODE]For Each varSelected In ctl.ItemsSelected
    strList = strList & ctl.Column(1, varSelected) & ", "
    [CODE]

    Truly sorry to have wasted your time.

  2. #17
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,643
    I am confused. If your code is concatenating values with comma then why is there a trailing 'and'? Where does 'and' come from? If anything, there should be a trailing comma.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #18
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    The code in my example was not meant to say "Use an array". It was just to show how to replace the last comma with "And".
    Changing a few variable names and dropping in the list box selections, here is the adapted code.

    Be warned, I was not able test this, but it seems to compile.
    Code:
        Dim ctl As Control
        Dim strList As String
        Dim varSelected As Variant
        Dim pos As Integer, i As Integer
        
        
        Me!ServiceText = sTemp
    
        i = 0  'counter
    
        Set ctl = Me!Needs
    
        If ctl.ItemsSelected.Count = 0 Then
            msgbox "You haven't selected anything"
        Else
            For Each varSelected In ctl.ItemsSelected
                strList = strList & ctl.Column(1, varSelected) & ", "
                i = i + 1
            Next varSelected
    
            'remove training space
            strList = Trim(strList)
    
            'remove last comma
            strList = Left(strList, Len(strList) - 1)
    
            If Len(strList) > 0 And i > 1 Then
                pos = InStrRev(strList, ", ")
                strList = Left(strList, pos - 1) & " and " & Right(strList, Len(strList) - pos)
            End If
    
        End If
    
        '    strList = Left(Me.ServiceText, InStrRev(Me.ServiceText, ",") - 2) & Replace(Me.ServiceText, ",", "and ", InStrRev(Me.ServiceText, ","))
        Me.ServiceText = strList
    
        'MsgBox "You selected the following items:" & vbCrLf & strList

  4. #19
    Gina Maylone is offline Always learning
    Windows 7 64bit Access 2013
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    Quote Originally Posted by June7 View Post
    I am confused. If your code is concatenating values with comma then why is there a trailing 'and'? Where does 'and' come from? If anything, there should be a trailing comma.
    Yes, it concatenates like a,b,c and d - which is what I want it to do. But now I have the problem of "and" being at the end of the string, when no other chosen item follows.
    Code:
    Private Sub Needs_AfterUpdate()
        Me!ServiceText.Value = sTemp
        
        Dim ctl As Control
        Dim strList As String
        Dim varSelected As Variant
      
        Set ctl = Me!Needs
        If ctl.ItemsSelected.Count = 0 Then
        MsgBox "You haven't selected anything"
        Else
        For Each varSelected In ctl.ItemsSelected
          strList = strList & ctl.Column(1, varSelected) & ", "
        Next varSelected
    '    ServiceText = Left$(strList, Len(strList) - 2)
        ServiceText = Left(strList, InStrRev(strList, ",") - 1) & Replace(strList, ",", " and ", InStrRev(strList, ","))
           
          End If
    End Sub
    Sorry for the confusion!

  5. #20
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,643
    That's because Replace() is replacing the trailing comma with ' and'.

    So do another manipulation:

    ServiceText = Left(ServiceText, Len(ServiceText) - 4)
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #21
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,372
    I have a listbox that contains a list of services.
    Well, I was under the impression that you were using a listbox, so I took a crack at it. I ended up with a final string variable called aryList, which when I substitute in June7's expression does not result in the answer I want. Maybe I did something wrong, but I have to admit, the expression is beautifully simple. I think the issue may be that if the last string portion is not one character long, it doesn't return enough. The posted code worked with a starting string of "400, 2R1, 3, 4, 5R1, 6, 8, 15, ". So, to allow any number of characters between commas at any position in the string, I came up with this:

    Code:
    Private Sub Command2_Click()
    Dim ctl As Control
    Dim strStart As String, strEnd As String
    Dim aryList As String
    Dim varSelected As Variant
      
    Set ctl = Me!Needs
    If ctl.ItemsSelected.Count = 0 Then
        MsgBox "You haven't selected anything"
        Exit Sub
    End If
    
    For Each varSelected In ctl.ItemsSelected
       aryList = aryList & ctl.Column(1, varSelected) & ", "
    Next varSelected
    
    'drop the last space and comma
    aryList = Left(aryList, Len(aryList) - 2) 
    
    'extract all from left before last remaining comma
    strStart = Left(aryList, InStrRev(aryList, ",") - 1)
    
    'get the last remaining digits
    strEnd = Mid(aryList, InStrRev(aryList, ",") + 2)
    
    'string both parts together with ' and ' in between
    aryList = strStart & " and " & strEnd 
    End Sub
    You might want this to be a function instead of a sub. I tried to use your form/control names.
    NOTE Re: Me!ServiceText.Value = sTemp that you have in your sub -
    You don't seem to be using this, so I'm not sure if a caution is warranted. Looks like you are trying to set the value of a control to a variable rather than the other way around, which is normal. But I don't see where sTemp comes from, nor do I see you refreshing the form after setting the value of the control. Hope the code helps - took a long time because I origninally tried to build the string by nesting loops.

    P.S. just noticed that it errs if the listcount = 1, so needs a bit of tweaking. Won't be doing that if you don't intend to use it.

  7. #22
    Gina Maylone is offline Always learning
    Windows 7 64bit Access 2013
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    This works beautifully! Except for the error that you mentioned when only one item is selected. Not sure what to do there. Thank you Micron!

  8. #23
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,372
    You are welcome. Very glad it helped.
    Copy what you have to a notepad document in case there's a problem, then replace everything between the Set statement and End Sub with this:
    Code:
    Select Case ctl.ItemsSelected.Count
       Case Is = 0
          MsgBox "You haven't selected anything"
       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
    End Select
    All of this is provided with the understanding that you are happy with the correlation between the bound column of your listbox and the actual column the value is being pulled from, assuming there is more than one column. If not, there could be a mis-match between what is being constructed in the string versus what you expect. Verify the data is correct for each case (one selected item or more than one selected item).

  9. #24
    Gina Maylone is offline Always learning
    Windows 7 64bit Access 2013
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    SO SO close...I don't get an error when only one item is selected but it also doesn't set the value of that one item. I've added one line to the bottom of the code
    Code:
    Me.ServiceText = aryList

  10. #25
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,372
    I didn't know what you wanted to do with it once the string was constructed. Glad you could figure it out.

Page 2 of 2 FirstFirst 12
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 07-19-2014, 01:15 PM
  2. Replies: 8
    Last Post: 04-18-2013, 04:41 PM
  3. Replies: 1
    Last Post: 10-08-2012, 09:01 AM
  4. Replies: 16
    Last Post: 11-01-2011, 01:35 PM
  5. ComboBox "Select" and "DLookUp"
    By witooldas in forum Forms
    Replies: 0
    Last Post: 03-23-2011, 03:31 AM

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