Select and de-select from a list box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tara99
    New Member
    • Oct 2006
    • 106

    #16
    Originally posted by mmccarthy
    Tara this is just a sample of the code:

    In the AfterUpdate event of the each of the four listboxes you would put the code to clear the other 3. For example, if listbox List2 has an item selected then the following code would clear the other 3. You need to try these things out Tara and come back then if you have any problems.

    [code]

    Private Sub List2_AfterUpda te()
    Dim i As Integer

    ' this clears listbox List1 by deselecting all items on the list
    For i = 0 To List1.ListCount - 1
    Me.List1.Select ed(i) = False
    Next i

    ' this clears listbox List3 by deselecting all items on the list
    For i = 0 To List3.ListCount - 1
    Me.List3.Select ed(i) = False
    Next i

    ' this clears listbox List4 by deselecting all items on the list
    For i = 0 To List4.ListCount - 1
    Me.List4.Select ed(i) = False
    Next i

    End Sub
    Hi guys
    While a go I had problem of selecting and deselecting.
    I had 4 List boxes, I wanted to select an item from ListBox1 and other three list boxes get deselected.
    The above code solved this, Thanks to Mary.
    Now
    I have 4 buttons, called ViewList1, ViewList2, ViewList3 and ViewList4 which display reports based on the selection from the list boxes. For example, when I select an item from Listbox1 and hit the button "ViewList1" , it will open a report1. When I select an item from Listbox2 and hit the button ViewList2, it will open report2 and the same for List3 and 4.
    Basically report1 shows information for the selected item from Listbox1; report2 shows information for the item selected from Listbox2 and etc….

    The problem that I have is that when
    I select an item from Listbox1
    and hit button ViewList2 (practically I shouldn't hit that button)
    it will open report2 where it shouldn't, because report2 is based on item selected from Listbox2 not Listbox1 and Listbox2's item wasn't even selected.
    I have this problem for all of the buttons.


    How can I fix this, I want to be able to display a message to the user when they hit the wrong button instead of opening a wrong report.


    The other question I have is, I wanted to merge all the buttons together, meaning to only have one button "View" to display the reports based on the selection from the selected Listboxes. How can I do this?

    I hope my questions and explanation are clear?

    Comment

    • MMcCarthy
      Recognized Expert MVP
      • Aug 2006
      • 14387

      #17
      Originally posted by tara99
      Hi guys
      While a go I had problem of selecting and deselecting.
      I had 4 List boxes, I wanted to select an item from ListBox1 and other three list boxes get deselected.
      The above code solved this, Thanks to Mary.
      Now
      I have 4 buttons, called ViewList1, ViewList2, ViewList3 and ViewList4 which display reports based on the selection from the list boxes. For example, when I select an item from Listbox1 and hit the button "ViewList1" , it will open a report1. When I select an item from Listbox2 and hit the button ViewList2, it will open report2 and the same for List3 and 4.
      Basically report1 shows information for the selected item from Listbox1; report2 shows information for the item selected from Listbox2 and etc….

      The problem that I have is that when
      I select an item from Listbox1
      and hit button ViewList2 (practically I shouldn't hit that button)
      it will open report2 where it shouldn't, because report2 is based on item selected from Listbox2 not Listbox1 and Listbox2's item wasn't even selected.
      I have this problem for all of the buttons.


      How can I fix this, I want to be able to display a message to the user when they hit the wrong button instead of opening a wrong report.


      The other question I have is, I wanted to merge all the buttons together, meaning to only have one button "View" to display the reports based on the selection from the selected Listboxes. How can I do this?

      I hope my questions and explanation are clear?
      Something like ...

      Code:
      Private Sub ViewList2_Click()
      
         If Listbox1.ListCount = 0 Then
      	  Msgbox "There is no selection in Listbox1 for this Report", vbOkOnly
      	  Listbox1.SetFocus ' or whichever control you want 
         Else
      	  'Code to open your report here
         End If
      
      End sub
      Mary

      Comment

      • tara99
        New Member
        • Oct 2006
        • 106

        #18
        Originally posted by mmccarthy
        Something like ...

        Code:
        Private Sub ViewList2_Click()
        
           If Listbox1.ListCount = 0 Then
        	  Msgbox "There is no selection in Listbox1 for this Report", vbOkOnly
        	  Listbox1.SetFocus ' or whichever control you want 
           Else
        	  'Code to open your report here
           End If
        
        End sub
        Mary
        I Mary
        I have tried this but this won't work.
        Is this correct
        This code is behind ViewApplication button

        Code:
        Private Sub ViewApplication_Click()
        On Error GoTo Err_ViewApplication_Click
          
           Dim stDocName As String
        
          
           If ServerList.ListCount = 0 Then
           'Or DepartmentList.ListCount = 0 Or LocationList.ListCount = 0 Then
              MsgBox "There is no selection in Server for this Report", vbOKOnly
              ServerList.SetFocus
              
           ElseIf DepartmentList.ListCount = 0 Then
              MsgBox "There is no selection in Department for this Report", vbOKOnly
              DepartmentList.SetFocus
         
           ElseIf LocationList.ListCount = 0 Then
              MsgBox "There is no selection in Location for this Report", vbOKOnly
               LocationList.SetFocus
         
           Else
              'Code to open your report here
              stDocName = "ApplicationSelect"
              DoCmd.OpenReport stDocName, acPreview
           End If
        
           
         
        
        Exit_ViewApplication_Click:
            Exit Sub
        
        Err_ViewApplication_Click:
            MsgBox Err.Description
            Resume Exit_ViewApplication_Click
            
        End Sub

        Comment

        • MMcCarthy
          Recognized Expert MVP
          • Aug 2006
          • 14387

          #19
          Sorry Tara my logic was a little off. Try this ...

          Code:
          Private Sub ViewApplication_Click()
          On Error GoTo Err_ViewApplication_Click
            
             Dim stDocName As String
          
             If ApplicationList.ListCount = 0 Then
             MsgBox "There is no selection in Application for this Report", vbOKOnly
             ApplicationList.SetFocus
                Else
             'Code to open your report here
             stDocName = "ApplicationSelect"
             DoCmd.OpenReport stDocName, acPreview
             End If
          
          Exit_ViewApplication_Click:
              Exit Sub
          
          Err_ViewApplication_Click:
              MsgBox Err.Description
              Resume Exit_ViewApplication_Click
              
          End Sub
          Mary

          Comment

          • tara99
            New Member
            • Oct 2006
            • 106

            #20
            Originally posted by mmccarthy
            Sorry Tara my logic was a little off. Try this ...

            Code:
            Private Sub ViewApplication_Click()
            On Error GoTo Err_ViewApplication_Click
              
               Dim stDocName As String
            
               If ApplicationList.ListCount = 0 Then
               MsgBox "There is no selection in Application for this Report", vbOKOnly
               ApplicationList.SetFocus
                  Else
               'Code to open your report here
               stDocName = "ApplicationSelect"
               DoCmd.OpenReport stDocName, acPreview
               End If
            
            Exit_ViewApplication_Click:
                Exit Sub
            
            Err_ViewApplication_Click:
                MsgBox Err.Description
                Resume Exit_ViewApplication_Click
                
            End Sub
            Mary
            Thanks Mary
            I have tried this, but it still not working, it still doing the same thing.
            I am not sure how but it seems like although the application list is not selected but it has its value. the same goes for the rest of the List Boxes.

            How can I do this via one button.
            To have only one button called View which opens the reports for the different selection from different List Boxes.

            Can you help please?

            Thanks

            Comment

            • MMcCarthy
              Recognized Expert MVP
              • Aug 2006
              • 14387

              #21
              Originally posted by tara99
              Thanks Mary
              I have tried this, but it still not working, it still doing the same thing.
              I am not sure how but it seems like although the application list is not selected but it has its value. the same goes for the rest of the List Boxes.

              How can I do this via one button.
              To have only one button called View which opens the reports for the different selection from different List Boxes.

              Can you help please?

              Thanks
              Miscommunicatio n.

              Sorry Tara I thought the listboxes were empty.

              Change

              Code:
              If ApplicationList.ListCount = 0 Then
              to either
              Code:
              If ApplicationList.ItemsSelected = 0 Then
              or

              Code:
              If IsNull(ApplicationList.ItemsSelected) Then
              One of these should work

              Mary

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #22
                Hi. I "subscribed " to this thread some timeback, but haven't had a chance to read it for a while. Has the issue been resolved?

                Comment

                • tara99
                  New Member
                  • Oct 2006
                  • 106

                  #23
                  Originally posted by mmccarthy
                  Miscommunicatio n.

                  Sorry Tara I thought the listboxes were empty.

                  Change

                  Code:
                  If ApplicationList.ListCount = 0 Then
                  to either
                  Code:
                  If ApplicationList.ItemsSelected = 0 Then
                  or

                  Code:
                  If IsNull(ApplicationList.ItemsSelected) Then
                  One of these should work

                  Mary
                  Mary I have tried those too but the value is never null it always displays the first selection.
                  I don't know why,

                  Comment

                  • MMcCarthy
                    Recognized Expert MVP
                    • Aug 2006
                    • 14387

                    #24
                    Originally posted by tara99
                    Mary I have tried those too but the value is never null it always displays the first selection.
                    I don't know why,
                    You will need to put

                    Code:
                    Me.Refresh
                    in you code where you want to boxes cleared after any action.

                    Mary

                    Comment

                    Working...