Results 1 to 6 of 6
  1. #1
    dipitiduda2 is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jan 2014
    Posts
    28

    ListBox Items - Displaying End of List Instead of First of List

    Hello: I have a ListBox control - where Multi Select property is set to Extended - on a subform. The values are being loaded into the control in alpha order as expected from the Row Source = Table/Query.

    The problem is that when the list is loaded the position or focus moves to the end of the list (Zapata County) instead of the beginning of the list (Anderson). I have checked all settings for the control, subform, and main form but I am unable to determine why the list does not load at the beginning.

    I have enclosed the code I'm using for the ListBox control below. Let me know if I need to provide anything more to clarify my issue. Thank you!

    BEGIN CODE BLOCK
    Private Sub Form_Current()
    Dim oItem As Variant
    Dim bFound As Boolean
    Dim sTemp As String
    Dim sValue As String
    Dim sChar As String
    Dim iCount As Integer
    Dim iListItemsCount As Integer

    sTemp = Nz(Me!txt_CountiesInServiceAreaSelections.Value, " ")
    iListItemsCount = 0
    bFound = False
    iCount = 0

    Call clearListBox

    For iCount = 1 To Len(sTemp) + 1
    sChar = Mid(sTemp, iCount, 1)
    If StrComp(sChar, ",") = 0 Or iCount = Len(sTemp) + 1 Then


    bFound = False
    Do
    If StrComp(Trim(Me!lst_CountiesInServiceArea.ItemData (iListItemsCount)), Trim(sValue)) = 0 Then
    Me!lst_CountiesInServiceArea.Selected(iListItemsCo unt) = True
    bFound = True
    End If
    iListItemsCount = iListItemsCount + 1
    Loop Until bFound = True Or iListItemsCount = Me!lst_CountiesInServiceArea.ListCount
    sValue = ""
    Else
    sValue = sValue & sChar
    End If
    Next iCount
    End Sub

    Private Sub clearListBox()

    Dim iCount As Integer

    For iCount = 0 To Me!lst_CountiesInServiceArea.ListCount
    Me!lst_CountiesInServiceArea.Selected(iCount) = False
    Next iCount
    End Sub

    Private Sub cmd_ClearListBoxCounties_Click()
    On Error GoTo cmd_ClearListBoxCounties_Click_Err
    On Error Resume Next

    Call clearListBox
    Me!txt_CountiesInServiceAreaSelections.Value = Null

    If (MacroError <> 0) Then
    Beep
    MsgBox MacroError.Description, vbOKOnly, ""
    End If
    cmd_ClearListBoxCounties_Click_Exit:
    Exit Sub
    cmd_ClearListBoxCounties_Click_Err:
    MsgBox Error$
    Resume cmd_ClearListBoxCounties_Click_Exit
    End Sub
    Private Sub cmd_ListBoxSelections_Click()
    Call SQL_Criteria_Str
    End Sub

    Function SQL_Criteria_Str() As String
    'Build Where Condition for SQL Statement (Other Columns - String data type)

    Dim varItem As Variant
    Dim ctl As Control
    Dim strCriteria As String
    Set ctl = Me.lst_CountiesInServiceArea
    strCriteria = ""

    For Each varItem In ctl.ItemsSelected
    'Use the ItemData Property to select the Bound Column
    'Use the Column Property to specify the Row, Column

    strCriteria = strCriteria + ctl.Column(1, varItem) & ": " & vbCrLf

    Next varItem
    Me.txt_CountiesInServiceAreaSelections = strCriteria

    End Function

    END CODE BLOCKClick image for larger version. 

Name:	ListBoxControl.jpg 
Views:	6 
Size:	211.9 KB 
ID:	17756
    Last edited by dipitiduda2; 08-11-2014 at 01:18 PM. Reason: Attach Pic

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I did not look at all of your code very closely but, have you tried commenting out...

    Code:
    For Each varItem In ctl.ItemsSelected
            'Use the ItemData Property to select the Bound Column
            'Use the Column Property to specify the Row, Column
    
            strCriteria = strCriteria + ctl.Column(1, varItem) & ": " & vbCrLf
    
        Next varItem
        Me.txt_CountiesInServiceAreaSelections = strCriteria

  3. #3
    dipitiduda2 is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jan 2014
    Posts
    28
    No, I am using that code to build out the string for one or more counties selected - then moved - to the TextBox control below. Then, the user should be able to enter some brief text next to the County Name. I have enclosed another picture to illustrate. I will comment the code out to see what happens.
    Click image for larger version. 

Name:	ListBoxControl2.jpg 
Views:	6 
Size:	59.8 KB 
ID:	17758

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    OK this code here would be selecting items on behalf od the user.
    Me!lst_CountiesInServiceArea.Selected(iListItemsCo unt) = True

    Perhaps this is why you are at the end of your list.
    If StrComp(Trim(Me!lst_CountiesInServiceArea.ItemData (iListItemsCount)), Trim(sValue)) = 0 Then
    Me!lst_CountiesInServiceArea.Selected(iListItemsCo unt) = True
    bFound = True
    End If

  5. #5
    dipitiduda2 is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jan 2014
    Posts
    28
    I will take a look...thank you!

  6. #6
    dipitiduda2 is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jan 2014
    Posts
    28
    That worked perfectly! After the Do...Loop Until block I set and selected the 1st value in the list "Anderson" - see reposted code block for the solution.

    Private Sub Form_Current()
    Dim oItem As Variant
    Dim bFound As Boolean
    Dim sTemp As String
    Dim sValue As String
    Dim sChar As String
    Dim iCount As Integer
    Dim iListItemsCount As Integer

    sTemp = Nz(Me!txt_CountiesInServiceAreaSelections.Value, " ")
    iListItemsCount = 0
    bFound = False
    iCount = 0

    Call clearListBox

    For iCount = 1 To Len(sTemp) + 1
    sChar = Mid(sTemp, iCount, 1)
    If StrComp(sChar, ",") = 0 Or iCount = Len(sTemp) + 1 Then
    bFound = False
    Do
    If StrComp(Trim(Me!lst_CountiesInServiceArea.ItemData (iListItemsCount)), Trim(sValue)) = 0 Then
    Me!lst_CountiesInServiceArea.Selected(iListItemsCo unt) = True
    bFound = True
    End If
    iListItemsCount = iListItemsCount + 1
    Loop Until bFound = True Or iListItemsCount = Me!lst_CountiesInServiceArea.ListCount
    'sValue = ""
    sValue = Me.lst_CountiesInServiceArea.ItemData(0)
    Me.lst_CountiesInServiceArea.Selected(0) = True
    Else
    sValue = sValue & sChar
    End If
    Next iCount
    End Sub

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

Similar Threads

  1. Replies: 3
    Last Post: 05-24-2014, 03:56 PM
  2. Replies: 7
    Last Post: 06-20-2013, 12:09 PM
  3. Replies: 5
    Last Post: 04-04-2011, 09:57 AM
  4. Replies: 3
    Last Post: 03-25-2010, 12:31 PM
  5. list box items
    By thewabit in forum Forms
    Replies: 12
    Last Post: 01-01-2010, 08:59 PM

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