Consulting

Results 1 to 15 of 15

Thread: LISTBOX : search by first letter

  1. #1

    LISTBOX : search by first letter

    Hello FORUM
    Hello everyone,
    I really need your help on how to do my research in my listbox1 in USF1 ..once I type the first letter of a word in textbox1 automatically displays all words begin with that letter .. and in TextBox2 appears the number of these words found
    my basic data is the sheet (DATABASE).
    thank you in advance
    cordially
    GAVIOTA ONE
    Attached Files Attached Files

  2. #2
    VBAX Expert
    Joined
    Oct 2012
    Posts
    726
    Location
    Hello.

    Private Sub TextBox1_Change()
        
        'requires reference to Microsoft ActiveX Data Objects xxx Library
        
        'https://www.connectionstrings.com/excel/
        Const cs As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                "Extended Properties='Excel 12.0 Xml;HDR=NO;';"
        
        Dim cn As New ADODB.Connection, rs As New ADODB.Recordset, ws As Worksheet
        Set ws = ThisWorkbook.Sheets("database")
        ListBox1.Clear
        With cn
            .Open cs & "Data Source=" & ws.Parent.FullName
                With rs
                    .Open "SELECT [f4] FROM `database$` where [f4] <> 'name' and " & _
                        "[f4] like '%" & TextBox1 & "%'", cn, adOpenStatic, adLockReadOnly
                    Do Until .EOF
                        ListBox1.AddItem .Fields(0)
                        .MoveNext
                    Loop
                    TextBox2 = rs.RecordCount
                    .Close
                End With
            .Close
        End With
    End Sub

  3. #3
    I have one example like your work. It may be useful for you.
    Attached Files Attached Files

  4. #4
    Quote Originally Posted by jonh View Post
    Hello.

    Private Sub TextBox1_Change()
        
        'requires reference to Microsoft ActiveX Data Objects xxx Library
        
        'https://www.connectionstrings.com/excel/
        Const cs As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                "Extended Properties='Excel 12.0 Xml;HDR=NO;';"
        
        Dim cn As New ADODB.Connection, rs As New ADODB.Recordset, ws As Worksheet
        Set ws = ThisWorkbook.Sheets("database")
        ListBox1.Clear
        With cn
            .Open cs & "Data Source=" & ws.Parent.FullName
                With rs
                    .Open "SELECT [f4] FROM `database$` where [f4] <> 'name' and " & _
                        "[f4] like '%" & TextBox1 & "%'", cn, adOpenStatic, adLockReadOnly
                    Do Until .EOF
                        ListBox1.AddItem .Fields(0)
                        .MoveNext
                    Loop
                    TextBox2 = rs.RecordCount
                    .Close
                End With
            .Close
        End With
    End Sub
    hello JONH
    thank you for help
    I do not know where is the problem in this code
    please see this picture
    cordially
    GAVIOTA ONE1.jpg

  5. #5
    Quote Originally Posted by Pongwipat View Post
    I have one example like your work. It may be useful for you.
    Hello Pongwipat
    thank you for help
    it's beautiful example
    I still have only the number of the desired words
    cordially
    GAVIOTA ONE

  6. #6
    VBAX Expert
    Joined
    Oct 2012
    Posts
    726
    Location
    There are a couple of comments in that code.

  7. #7
    Quote Originally Posted by jonh View Post
    There are a couple of comments in that code.
    HIII JONH
    thank you very much for help me
    I am a beginner in vba excel
    Please can you put me the code in this file below
    thank you in advance
    cordially
    GAVIOTA ONE

  8. #8
    VBAX Expert
    Joined
    Oct 2012
    Posts
    726
    Location
    Either go to the code window and select tools > references and add the library.

    Or change this

        Dim cn As New ADODB.Connection, rs As New ADODB.Recordset, ws As Worksheet
    to this

    Set cn = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")

  9. #9
    Hiii jonh
    it does not work
    thank you for your suggestion
    cordially
    GAVIOTA ONE

  10. #10
    VBAX Expert
    Joined
    Oct 2012
    Posts
    726
    Location
    try replacing
        Const cs As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & _ 
            "Extended Properties='Excel 12.0 Xml;HDR=NO;';"
    with

        Const cs As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Extended Properties="Excel 8.0;HDR=no;IMEX=1";

  11. #11
    Hello everyone
    Please there's not a way of out of this problem
    cordially
    GAVIOTA ONE

  12. #12
    VBAX Expert
    Joined
    Oct 2012
    Posts
    726
    Location
    Of course there is. But simply saying 'it doesn't work' won't make it work.
    If the code errors, tell us what the error says.

  13. #13
    Quote Originally Posted by jonh View Post
    Of course there is. But simply saying 'it doesn't work' won't make it work.
    If the code errors, tell us what the error says.
    Hello JONH
    the file is before you and your code is before you
    trying this code and you will see the error
    cordially
    GAVIOTA ONE

  14. #14
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Quote Originally Posted by GAVIOTA ONE View Post
    Hello JONH
    the file is before you and your code is before you
    trying this code and you will see the error
    cordially
    GAVIOTA ONE
    This is not the situation. The computer of Jonh is not the computer of GAVIOTA.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  15. #15
    Quote Originally Posted by SamT View Post
    This is not the situation. The computer of Jonh is not the computer of GAVIOTA.
    Hello SamT
    thank you ..
    cordially
    GAVIOTA ONE

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •