Solved Listbox data not displaying when adding items to a destination Listbox

What you can solve for me know please is that when I make selection it must go to the top of destination list and not at the bottom please.
Yes, but you are taking no notice of what is selected, just picking the first data row? :(

1734783797686.png
 
Code:
Option Compare Database
Option Explicit

Private Sub cmdSelect_Click()
     Dim idx As Integer
     Dim firstitem As String
     Dim i As Integer
     If SearchList.ListCount > 0 Then
        ' Build a comma-delimited string with all columns of the first row
       If Not IsNull(Me.SearchList) Then
           firstitem = ""
           'since you have a header
           idx = Me.SearchList.ListIndex + 1
            'add the header first time
            If Me.destListbox.ListCount = 0 Then
                       For i = 0 To SearchList.ColumnCount - 1
                            If i > 0 Then firstitem = firstitem & ","
                            firstitem = firstitem & SearchList.Column(i, 0)
                        Next i
            destListbox.AddItem firstitem, 0
            End If
           firstitem = ""
          
           'do not re-add the header
           If idx > 0 Then
                Debug.Print "add"
                For i = 0 To SearchList.ColumnCount - 1
                   If i > 0 Then firstitem = firstitem & ","
                   firstitem = firstitem & SearchList.Column(i, idx)
                Next i
                
                destListbox.AddItem firstitem, 1
           End If
        ' Add the constructed string to the destination listbox
          
        End If
    End If
    
cmdLookupBookCode_Click_Exit:
    Exit Sub

cmdLookupBookCode_Click_Err:
    MsgBox Error$
    Resume cmdLookupBookCode_Click_Exit
  
End Sub
 
Does not work for me, unsless I misunderstood you? :unsure:
The -1 values were what I meant when I said 'Not producing what you want I believe?'
View attachment 117599

Yes, but you are taking no notice of what is selected, just picking the first data row? :(

View attachment 117601
Yes, but you are taking no notice of what is selected, just picking the first data row? :(

View attachment 117601
Hi Gasman. The first row in my real application is always different. In the sample DB it stays the same. I hope you understand.
 
I do not believe the O/P wanted the headers?
That was the whole basis of the post, they were getting the header and not the data?
 
Code:
Option Compare Database
Option Explicit

Private Sub cmdSelect_Click()
     Dim idx As Integer
     Dim firstitem As String
     Dim i As Integer
     If SearchList.ListCount > 0 Then
        ' Build a comma-delimited string with all columns of the first row
       If Not IsNull(Me.SearchList) Then
           firstitem = ""
           'since you have a header
           idx = Me.SearchList.ListIndex + 1
            'add the header first time
            If Me.destListbox.ListCount = 0 Then
                       For i = 0 To SearchList.ColumnCount - 1
                            If i > 0 Then firstitem = firstitem & ","
                            firstitem = firstitem & SearchList.Column(i, 0)
                        Next i
            destListbox.AddItem firstitem, 0
            End If
           firstitem = ""
         
           'do not re-add the header
           If idx > 0 Then
                Debug.Print "add"
                For i = 0 To SearchList.ColumnCount - 1
                   If i > 0 Then firstitem = firstitem & ","
                   firstitem = firstitem & SearchList.Column(i, idx)
                Next i
               
                destListbox.AddItem firstitem, 1
           End If
        ' Add the constructed string to the destination listbox
         
        End If
    End If
   
cmdLookupBookCode_Click_Exit:
    Exit Sub

cmdLookupBookCode_Click_Err:
    MsgBox Error$
    Resume cmdLookupBookCode_Click_Exit
 
End Sub
Hi MajP. Thanks for the code. I will try it out.
 
Hi Gasman. The first row in my real application is always different. In the sample DB it stays the same. I hope you understand.
Now I do, but if you supply a DB again, give us EXACTLY what it works like
Even @MajP was not aware of your needs. I doubt anyone else was either. :(

Why bother with a button then, just populate that listbox regardless. :unsure:
 
Last edited:
That is not going to do what you just said you need to do. :(
Yes, you right. I need code to place the new element of the top of the list every time. Any code that you think will work. Please send me updated solution please. Thanks
 
Yes, you right. I need code to place the new element of the top of the list every time. Any code that you think will work. Please send me updated solution please. Thanks
You already have that, as you have shown?
 
I do not believe the O/P wanted the headers?
That was the whole basis of the post, they were getting the header and not the data?
Did you try the code?
First time trhough it adds the selected row and the header row
Next time it adds the selected row below the header row.

Is that not what is desired?
 
1st selection
S1.png



Second selection
S2.png
 
Did you try the code?
First time trhough it adds the selected row and the header row
Next time it adds the selected row below the header row.

Is that not what is desired?
No, seems they just wanted whatever was in the first row of the listbox, regardless of what might be selected. :(
They were getting the header all the time as the listindex was 0. They have now changed it to 1 and got what they wanted.
30 odd posts to just do that. :(

Edit: I belive they wanted just that one row, not append others as well.
 
No, seems they just wanted whatever was in the first row of the listbox, regardless of what might be selected
Really? That seems useless.
 
No, seems they just wanted whatever was in the first row of the listbox, regardless of what might be selected. :(
They were getting the header all the time as the listindex was 0. They have now changed it to 1 and got what they wanted.
30 odd posts to just do that. :(

Edit: I belive they wanted just that one row, not append others as well.
Thanks Gasman making that clearing that up. So all i need now is the first row must appear in the destination list. What you can do is. Press the select button add the first row to the destination list. Go to the Selection list and delete the first row manually and press Selection again. In this way we can see if it is in the first position by having different data from the selection list
 
Thanks Gasman making that clearing that up. So all i need now is the first row must appear in the destination list. What you can do is. Press the select button add the first row to the destination list. Go to the Selection list and delete the first row manually and press Selection again. In this way we can see if it is in the first position by having different data from the selection list
Not sure if you can manually delete while program is running. Or you can put in a button to remove the first element if that will work and try adding records to destination list again. Thanks for assisting
 
So all i need now is the first row must appear in the destination list. What you can do is. Press the select button add the first row to the destination list. Go to the Selection list and delete the first row manually and press Selection again. In this way we can see if it is in the first position by having different data from the selection list
I do not get it. What are you trying to show to the user? There may be a better way.
Showing the first row in a second listbox provides nothing more than looking at the first row in the first listbox.

Maybe if the second listbox was formatted differently. But simply showing what is the first item in the other listbox? Why even have a button.

Maybe form opens and automattically shows the first item (no need to click). On the first item if you then add a button to remove the first item from the list and show the new first item in the destination. But not sure what that does.
 
Really? That seems useless.
Hi. The actual application scans in a book. The new book is in the first position. Then I want to put that book details in the destination list. The next book we scan which will be a different book is also in the first position then we add that book to the destination list. So we build a list of books captured. I hope you understand. I will not ask something to waste your time. All I need know is to have the books that gets captured to appear in the first position of our destination list. You can add a extra button to just to remove the first element form the search list and in order to have different data in the destination list every time. Thanks
 

Attachments

Well hopefully it makes sense to you. I still do not understand the value.

If you need to move things back and forth between listboxes here is class that may be useful
if you need to sort a listbox manually here is a class
 

Users who are viewing this thread

Back
Top Bottom