How do you clear a listbox? (2 Viewers)

cosmarchy

Registered User.
Local time
Today, 11:07
Joined
Jan 19, 2010
Messages
116
Hi,
For clarity, by clear I am meaning that I want to empty the listbox of any previous results - I want a blank listbox.

Code:
Set lstResults.Recordset = Nothing
Also doesn't do anything. Bit strange really as all the previous results are still in the listbox even though the recordset is nothing you may have expected it to have cleared out its contents...
 

Gasman

Enthusiastic Amateur
Local time
Today, 19:07
Joined
Sep 21, 2011
Messages
14,398
Not recordset, rowsource !
 

cosmarchy

Registered User.
Local time
Today, 11:07
Joined
Jan 19, 2010
Messages
116
Also, for reference, this is not a multiselect listbox (not sure whether this matters though as I'm not looking to clear the selected entries but clear the entire listbox of everything!!)
 

theDBguy

I’m here to help
Staff member
Local time
Today, 11:07
Joined
Oct 29, 2018
Messages
21,515
Also, for reference, this is not a multiselect listbox (not sure whether this matters though as I'm not looking to clear the selected entries but clear the entire listbox of everything!!)
Can you please tell us what's in the following properties for your Listbox? Thank you.

Name
Row Source
Row Source Type
Bound Column
Column Count
Column Widths
Allow Value List Edits
List Items Edit Form
Inherit Value List
Multi Select
 

cosmarchy

Registered User.
Local time
Today, 11:07
Joined
Jan 19, 2010
Messages
116
Can you please tell us what's in the following properties for your Listbox? Thank you.

Name - lstResults
Row Source - (blank)
Row Source Type - Table/Query
Bound Column - 1
Column Count - 3
Column Widths - 2cm;;3cm
Allow Value List Edits - yes
List Items Edit Form - (blank)
Inherit Value List - yes
Multi Select - none
 

theDBguy

I’m here to help
Staff member
Local time
Today, 11:07
Joined
Oct 29, 2018
Messages
21,515
Thanks. For starters, try changing the Inherit Value List to No.

With the Row Source being blank, can you also post a screenshot of the Listbox showing the items in it?
 

moke123

AWF VIP
Local time
Today, 14:07
Joined
Jan 11, 2013
Messages
3,933
The rowsource is blank yet there are items in the listbox?

Have you tried re-creating the listbox?
 

cosmarchy

Registered User.
Local time
Today, 11:07
Joined
Jan 19, 2010
Messages
116
Thanks. For starters, try changing the Inherit Value List to No.

With the Row Source being blank, can you also post a screenshot of the Listbox showing the items in it?
1666123139723.png


Setting Inherit Value list to no didn't make any difference.
 

Gasman

Enthusiastic Amateur
Local time
Today, 19:07
Joined
Sep 21, 2011
Messages
14,398
Time to upload the db now I think?
 

moke123

AWF VIP
Local time
Today, 14:07
Joined
Jan 11, 2013
Messages
3,933
I would re-create the listbox to rule out corruption.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 14:07
Joined
Feb 19, 2002
Messages
43,424
There is no reason to empty the RowSource of a listbox. You can always control the value list by modifying the RowSource query. If there are no records selected, the listbox will be empty. Something you are doing makes no sense.
 

cosmarchy

Registered User.
Local time
Today, 11:07
Joined
Jan 19, 2010
Messages
116
I'll upload a DB but it will have to be tomorrow now as it is large and i'll need to remove the intellectual data... :D
 

Gasman

Enthusiastic Amateur
Local time
Today, 19:07
Joined
Sep 21, 2011
Messages
14,398
All you need is what supplies that listbox, and perhaps the rest of the form.
 

cheekybuddha

AWF VIP
Local time
Today, 19:07
Joined
Jul 21, 2014
Messages
2,307
Are you using code to populate the to populate the listbox in the first place? If so, please post it
 
Last edited:

cosmarchy

Registered User.
Local time
Today, 11:07
Joined
Jan 19, 2010
Messages
116
Ok, attached is the offending DB.

Entering "wd-1" in to the textbox and hitting enter will provide some entries in the listbox.
 

Attachments

  • Submit.accdb
    528 KB · Views: 104

moke123

AWF VIP
Local time
Today, 14:07
Joined
Jan 11, 2013
Messages
3,933
Thats a first for me and I use a lot of listboxes. I have never seen a list set to a recordset like that.

All you really need is
Code:
Private Sub cmdOk_Click()

    Dim strSQL As String
      
    If (Not IsNull(txtPartNumber.Value)) Then
      
        strSQL = "SELECT PN as [Part No], DE as [Description], MC as [Manufacturers Code] FROM tblPT WHERE ((PN='" & txtPartNumber & "') OR (MC Like '*" & txtPartNumber & "*'));"
        
        lstResults.RowSource = strSQL
        
    End If
    
End Sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 19:07
Joined
Sep 21, 2011
Messages
14,398
Not sure why I cannot see form view on that form?

ANyway, a quick button and
Code:
Private Sub cmdClear_Click()
Me.lstResults.RowSource = ""
Set lstResults.Recordset = Nothing
End Sub

does clear it. Tried commenting out each in turn and it did not. :(
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 14:07
Joined
May 21, 2018
Messages
8,556
I added a requery after setting the recordset to nothing.

Code:
Private Sub cmdOk_Click()

    On Error GoTo Err_cmdOk_Click
 
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim strSQL As String
     
    If (Not IsNull(txtPartNumber.Value)) Then
        Set db = CurrentDb
        strSQL = "SELECT PN as [Part No], DE as [Description], MC as [Manufacturers Code] FROM tblPT WHERE ((PN='" & txtPartNumber & "') OR (MC Like '*" & txtPartNumber & "*'));"
 
        Set rs = db.OpenRecordset(strSQL)
     
        If (Not rs.EOF = True) Then
            rs.MoveLast 'NECESSARY WHEN THERE IS MORE THAN ONE RECORD RETURNED BUT NOT SURE OF IMPLEMENTATION...
          If (rs.RecordCount = 1) Then
            Debug.Print rs.RecordCount & " records found"
            'move on - open form, process etc...
            Set lstResults.Recordset = rs
          ElseIf (rs.RecordCount > 1) Then
            Set lstResults.Recordset = rs
            Debug.Print rs.RecordCount & " records found"
            txtPartNumber.SetFocus
          End If
        Else
          MsgBox "No Records Found"
          Set Me.lstResults.Recordset = Nothing
          Me.lstResults.Requery
        End If
    End If
 
Exit_cmdOk_Click:
    Exit Sub

Err_cmdOk_Click:
    MsgBox err.Description
    Resume Exit_cmdOk_Click
 
End Sub
 

Users who are viewing this thread

Top Bottom