Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    CaptainKen is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2012
    Location
    Arizona
    Posts
    31

    Search as you type

    I'm trying to modify the sample database found here use a textbox to filter a subform rather than a ListBox.

    http://www.opengatesw.net/ms-access-...pe-Access.html

    At the bottom of the page it states:
    You can also apply this same technique to a form instead of just a listbox with some minor tweaks. All you would need to change is the function:
    fLiveSearch to change references from RowSource to RecordSource, and ListCount with RecordSetClone.RecordCount

    I've tried those modifications as shown below, however it keeps generating the error "91, Object variable or With block variable not set" on this line:


    ctlSubForm = Forms![frmSearchExample]!sbfItems.Form

    You'll see that I commented out the original code as a reference.

    Code:
    Private Sub txtSearch_Change()
    'CODE THAT HANDLES WHAT HAPPENS WHEN THE USER TYPES IN THE SEARCH BOX
    'http://www.opengatesw.net/ms-access-tutorials/Access-Articles/Search-As-You-Type-Access.html
    
    Dim strFullList       As String
    Dim strFilteredList   As String
    Dim ctlSubForm As Control
    
    ctlSubForm = Forms![frmSearchExample]!sbfItems.Form
    
    ' This msgbox returns the correct result
    MsgBox Me.sbfItems.Form.RecordSource
    
    10    If blnSpace = False Then
    20      Me.Refresh 'refresh to make sure the text box changes are actually available to use
    
            'specify the default/full rowsource for the control
    30      strFullList = "SELECT RecordID, First, Last FROM tblNames ORDER BY First;"
    
            'specify the way you want the rowsource to be filtered based on the user's entry
    40      strFilteredList = "SELECT RecordID, First, Last FROM tblNames WHERE [First] LIKE ""*" & Me.txtSearch.Value & _
                            "*"" OR [Last] LIKE ""*" & Me.txtSearch.Value & "*"" ORDER BY [First]"
     
            'run the search
    '50      fLiveSearch Me.txtSearch, Me.lstItems, strFullList, strFilteredList, Me.txtCount
    
    50      fLiveSearch Me.txtSearch, ctlSubForm, strFullList, strFilteredList, Me.txtCount
    60    End If
    
    End Sub
    Code:
    Function fLiveSearch(ctlSearchBox As TextBox, ctlFilter As Control, _
                          strFullSQL As String, strFilteredSQL As String, Optional ctlCountLabel As Control)
    
    '10    On Error GoTo err_handle
      
              'restore the cursor to where they left off
    20        ctlSearchBox.SetFocus
    30        ctlSearchBox.SelStart = Len(ctlSearchBox.Value) + 1
            
    40        If ctlSearchBox.Value <> "" Then
                     'Only fire if they've input more than two characters (otherwise it's wasteful)
    50               If Len(ctlSearchBox.Value) > iSensitivity Then
    '60                   ctlFilter.RowSource = strFilteredSQL
    60                   ctlFilter.RecordSource = strFilteredSQL
    '70                   If ctlFilter.ListCount > 0 Then
    70                   If ctlFilter.RecordsetClone.RecordCount > 0 Then
    80                       ctlSearchBox.SetFocus
    90                       ctlSearchBox.SelStart = Len(ctlSearchBox.Value) + 1
    100                   Else
    110                     If blnEmptyOnNoMatch = True Then
    '120                      ctlFilter.RowSource = ""
    120                      ctlFilter.RecordSource = ""
    130                     Else
    '140                      ctlFilter.RowSource = strFullSQL
    140                      ctlFilter.RecordSource = strFullSQL
    150                     End If
    160                   End If
    170             Else
    '180               ctlFilter.RowSource = strFullSQL
    180               ctlFilter.RecordSource = strFullSQL
    190             End If
    
    200        Else
    '210           ctlFilter.RowSource = strFullSQL
    210           ctlFilter.RecordSource = strFullSQL
    220        End If
                
                'if there is a count label, then update it
    230         If IsMissing(ctlCountLabel) = False Then
    240           ctlCountLabel.Caption = "Displaying " & Format(ctlFilter.ListCount - 1, "#,##0") & " records"
    250         End If
                
    260   Exit Function
    err_handle:
    270   Select Case Err.Number
        Case 91 'no ctlCountLabel
           'exit
    280       Case 94 'null string
           'exit
    290       Case Else
    300         MsgBox "An unexpected error has occurred: " & vbCrLf & Err.Description & _
                vbCrLf & "Error " & Err.Number & vbCrLf & "Line: " & Erl
    310   End Select
    
    End Function
    Thank you,

    Ken

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Try:
    Set ctlSubForm = Forms![frmSearchExample]!sbfItems.Form
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    CaptainKen is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2012
    Location
    Arizona
    Posts
    31
    Thank you for your quick reply.

    That gives me an Error 13 "Type Mismatch" error.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Your goal is to filter the recordset of a form as user types an entry in a textbox?

    According to the article, that variable should not be needed. You have modified the txtSearch_Change procedure when it said only the fLiveSearch function should have to be modified. What is your reasoning for the procedure mod?

    Possibly form is not a Control, try Object instead.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    CaptainKen is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2012
    Location
    Arizona
    Posts
    31
    Me.lstItems is no longer a listbox, but a subform instead.

    I tried replacing me.lstItems as follows, but I get a type mismatched. So I figured a variable would be better to try inside the fLiveSearch call.

    50 fLiveSearch Me.txtSearch, Forms![frmSearchExample]!sbfItems.Form, strFullList, strFilteredList, Me.txtCount

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Is the textbox on main form?

    The function is expecting ctlFilter to be a Control as defined in the Function declaration. The article did not indicate that would be an issue. What might be the issue is that you are trying to pass a reference to a subform (or any form maybe an issue). I always name subform container control different from the object it holds, like ctrItems. Then the reference would be: Me.ctrItems.Form

    I use .Form when referring to controls (textboxes, comboboxes, etc) of the form, like: Me.ctrItems.Form.txtID

    Maybe the function call should reference subform like:
    Me.ctrItems.Form.Name
    or
    Me.ctrItems.SourceObject
    or
    "frmSearchExample.ctrItems.Form"

    You might want to test your code on a form that is not a subform.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    CaptainKen is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2012
    Location
    Arizona
    Posts
    31
    Dim as an object gives same error.

    Set objSubForm = Forms![frmSearchExample]!sbfItems.Form

    fLiveSearch Me.txtSearch, objSubForm, strFullList, strFilteredList, Me.txtCount

  8. #8
    CaptainKen is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2012
    Location
    Arizona
    Posts
    31
    These all return "Compile Error, Type Mismatch"

    fLiveSearch Me.txtSearch, Me.sbfItems.SourceObject, strFullList, strFilteredList, Me.txtCount

    fLiveSearch Me.txtSearch, Me.sbfItems.Form.Name, strFullList, strFilteredList, Me.txtCount

    fLiveSearch Me.txtSearch, "Form!frmSearchExample.sbfItems.Form", strFullList, strFilteredList, Me.txtCount

    fLiveSearch Me.txtSearch, "Form_frmSearchExample.sbfItems.Form", strFullList, strFilteredList, Me.txtCount

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    It is possible the article errored in what mods are required for the function to work with a form. I am testing in the Immediate window of VBA editor:

    x = "Tests"
    ?Forms(x).RecordsetClone.RecordCount

    Returns the recordcount for my form 'Tests' that is open.

    x = "SampleManagement"
    ?Forms(x).ctrSampleList.Form.RecordsetClone.Record Count

    Returns the recordcount for a subform on an open form.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    CaptainKen is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2012
    Location
    Arizona
    Posts
    31
    Ug, that didn't work either.

    In case you're up to it, the database with my attempts can be found here.
    www.captainken.us/files/SearchAsYouType-Ken.zip

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I was right. Quite a few other changes needed to make this work, at least for a form, did not attempt a subform.

    Even the field names were reversed. Last names were in the First field and vice versa.

    Attached is a mod of the original sample database. Easier than trying to describe. I retained only a few of the 50,000 records to reduce file size.
    Attached Files Attached Files
    Last edited by June7; 04-14-2012 at 02:06 AM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    CaptainKen is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2012
    Location
    Arizona
    Posts
    31

    Smile

    Awesome!

    Thank you for all your time and effort!!!

  13. #13
    CaptainKen is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2012
    Location
    Arizona
    Posts
    31
    accessissue,

    I will be posting a link to the RMA database this is used in hopefully over the weekend. OpenGate's version had some bugs that June7 helped me with. I've made some other tweaks and it works really nice. I think you'll love all the nice examples of many features that are contained in the database.

    Ken

  14. #14
    accessissue is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2012
    Posts
    4
    Thank you so much CaptainKen

  15. #15
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    accessissue, my example db is now reattached to earlier post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. search as you type in combo box
    By pratim09 in forum Forms
    Replies: 3
    Last Post: 08-05-2011, 07:46 AM
  2. Replies: 3
    Last Post: 06-04-2011, 12:23 PM
  3. How do I fix the #type!
    By Shortone in forum Access
    Replies: 3
    Last Post: 12-02-2010, 11:34 PM
  4. Replies: 2
    Last Post: 08-31-2010, 08:57 PM
  5. Replies: 2
    Last Post: 03-18-2010, 08:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums