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

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
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,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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