Results 1 to 11 of 11
  1. #1
    m_low is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Jun 2014
    Posts
    8

    Smile Create a search function using a macro on a datasheet form

    Hi everyone,



    So i need to create a search function on a datasheet form that uses a macro since it will potentially be used on sharepoint. I could program this in VBA but I have no idea how to do it using a macro.

    Click image for larger version. 

Name:	houseporn database.png 
Views:	21 
Size:	29.9 KB 
ID:	17154


    The idea is that the search above would search ALL of the fields and return matches.

    Any help or pointing me towards a tutorial would be greatly appreciated.

    Michael

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,944
    Would a dynamic parameterized query using LIKE operator and wildcard work in SharePoint? The query would be the form RecordSource.

    SELECT * FROM tablename WHERE [Firm Name] LIKE "*" & Forms!formname!SearchBoxName & "*" AND [Firm Type] LIKE "*" & Forms!formname!SearchBoxName & "*";

    Then the macro would requery the 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
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,527
    This can get you started...it will build sql for each field in the search table..
    you can alter it to make Queries if needed.

    Code:
    'search all fields for this text
    Public Sub FindInAllFlds(ByVal pvFind, ByVal pvTbl)
    Dim tdf As TableDef
    Dim fld  'As Field
    Dim vDelim
    Dim sSql As String
    Dim db
    Const kNUM = 4
    Const kDATE = 8
    Const kSTR = 10
    Const kMEMO = 12
    Set db = CurrentDb
    Set tdf = db.TableDefs(pvTbl)
    For Each fld In tdf.Fields
          Select Case TypeName(fld)
               Case kSTR, kMEMO
                 vDelim = "'"
               Case kDATE
                 vDelim = "#"
               Case Else  'number
                 vDelim = ""
          End Select
        
        sSql = "Select * from [" & pvTbl & "] where [" & fld.Name & "]=" & vDelim & pvFind & vDelim
        Debug.Print sSql
    Next
    Set db = Nothing
    Set fld = Nothing
    Set tdf = Nothing
    End Sub

  4. #4
    m_low is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Jun 2014
    Posts
    8
    June 7 let me try that and get back to you

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,944
    Correction to my suggestion: use AND instead of OR - edited 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.

  6. #6
    m_low is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Jun 2014
    Posts
    8
    June7 it works great! I just need to tweak the query slightly so that it can return results even if it is only a partial match. For instance, if Beta Architects is returns if Beta is typed. Thank you!

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,944
    The LIKE operator and wildcard should accommodate that.
    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.

  8. #8
    m_low is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Jun 2014
    Posts
    8
    So do I just use an InStr() or something?

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,944
    That is an alternative but I already gave you a method that should allow partial match.

    However, there is a complication if the search fields have Null. If Null is a possibility, need to deal with. One way:

    SELECT * FROM tablename WHERE ([Firm Name] LIKE "*" & Forms!formname!SearchBoxName & "*" OR Is Null) AND ([Firm Type] LIKE "*" & Forms!formname!SearchBoxName & "*" OR Is Null);
    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
    m_low is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Jun 2014
    Posts
    8
    Thanks for the query it is actually OR instead of AND so you were right the first time

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,944
    The AND should work when using LIKE and wildcard.
    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.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 10-09-2013, 05:45 PM
  2. Replies: 1
    Last Post: 05-03-2013, 01:40 PM
  3. Replies: 2
    Last Post: 08-17-2012, 09:28 AM
  4. Replies: 5
    Last Post: 07-13-2012, 01:15 AM
  5. Replies: 4
    Last Post: 08-05-2011, 07:27 AM

Tags for this Thread

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