UserForm Textbox - "Instant Search"

ldoodle

New Member
Joined
Mar 2, 2010
Messages
40
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a UserForm that has 3 controls - textbox and 2 buttons (Clear and Close). I am trying to replicate search engines "instant search" so results (from a SQL database) are updated as you type.

The problem is that the Change event causes the text input to lag while it is off doing it's SQL query, so the characters don't show in the textbox until the query is done. Can this be changed so typing is fluid.

One thing I've literallty just thought of is having a delay between keystrokes so if the time is longer than say 2 seconds, then do the search. Is that possible?

Also, is it possible to do what I want without a UserForm? Can I replicate this in an "in-sheet" control?

Thanks
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Code:
Private Sub cmdSearch(value As String)
    Dim Connection, Command, Recordset As Object
    
    Application.ScreenUpdating = False
    
    Set Connection = New ADODB.Connection
    Connection.Open SqlString
    
    Set Command = New ADODB.Command
    Set Command.ActiveConnection = Connection
    
    With Command
    
        .CommandType = adCmdStoredProc
        .CommandText = "dbo.ef_sp_InproSearchExcel"
        .Parameters.Append .CreateParameter("@search", adVarWChar, adParamInput, 100, value)
        
    End With
    
    Sheet1.Range("A4", "A65536").EntireRow.ClearContents
    Application.Goto Range("A1"), True
    Sheet1.Range("A1").Select
    
    Set Recordset = Command.Execute
    If Not Recordset.EOF Then
        Sheet1.Range("A4", "G4").CopyFromRecordset Recordset
        For Each Cell In Range("A4:A65536")
            If Cell.value <> "" Then
                Cell.Hyperlinks.Add ANCHOR:=Cell, Address:="Y:\RootFolder\" & Left(Cell.value, 1) & "\" & Left(Cell.value, 2) & "\" & Cell.value, TextToDisplay:=Cell.value
            End If
        Next
    End If
    
    Recordset.Close
    Set Recordset = Nothing
    
    Set Command.ActiveConnection = Nothing
    Set Command = Nothing
    
    Connection.Close
    Set Connection = Nothing
    
    Application.ScreenUpdating = True
    
End Sub
 
Last edited:
Upvote 0
Ok so I've been reading on adAsyncExecute but when I add it, the recordset never opens:

Code:
Set Recordset = Command.Execute(, , adAsyncExecute)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top