Results 1 to 7 of 7
  1. #1
    RayMilhon is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,085

    Filter on a Form

    I have a form in MS Access 2010 That displays contract information. There is a subform that displays the customer information from that contract. I have a command button on the form so that if the user doesn't know the contract number they can find all contracts related to a specific person. The command button displays 2 prompts 1 for the last name and 1 for the first name with a minimum of 3 characters. It then builds a query and gets all of the contractID's related to the matching people. Code Below:



    Code:
    Private Sub cmd_find_customer_Click()
    Dim lastname as string, firstname as string, tmpstr as string
    Dim strsql as string, rs as new adodb.recordset
    
    lastname = Inputbox("Please Enter the last name (Minimum of 3 characters)","Last Name"
    firstname = Inputbox("Please enter the first name ","First Name")    'There is no min on the first name just in case they want all with the same lastname
    
    tmpstr = ""   'Clear the tmpstr
    strsql = "Select contractID from tbl_customer_contract_link inner join tbl_customer on tbl_customer_contract_link.customerid = tbl_customer.customerid " & _
        "Where tbl_customer.lastname Like ""*" & lastname & "*"" and tbl_customer.firstname like ""*" & firstname & "*"""
    
    
    rs.open strsql,currentproject.connection,adopenforwardonly,adlockreadonly
    
    do until rs.eof
    
        tmpstr = tmpstr & rs!contractID & ","
        rs.movenext
    loop
    
       If tmpstr > 1 Then
           tmpstr = left(tmpstr,len(tmpstr)-1)
           me.filter = "ContractID In (" & tmpstr & ")"
           me.filteron = true
       End if
    rs.close
    set rs = nothing
    
    End sub

    When I click on the button I get the 2 prompts put in the data for the prompts but when the code executes I get 0 records. If I put in a breakpoint at the rs.open command pull the strsql string out and put it into a query I get the expected records. Executing in the code returns 0 records????

    Anybody have an idea what I'm missing?

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Have you stepped through the code after setting the breakpoint, to see if the string is being populated? I would wonder if this:

    If tmpstr > 1 Then

    should be


    If Len(tmpstr) > 0 Then
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    RayMilhon is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,085
    No the tmpstr is not being populated because the Recordset is not returning any data. Even though the query works when I run it in VBA it doesn't

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Ah, just noticed, when using an ADO recordset, you need to use % instead of * as the wildcard.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    RayMilhon is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,085
    Thanks that worked also realized I forgot the quotes in the tmpstr 99% of the time there will be only 1 contract for a person but just in case need to account for the possibility of more need to change the following lines



    When building the tmpstr should be as follows:

    tmpstr = tmpstr & rs!contractID & ""","""

    Also the
    tmpstr = LEFT(tmpstr,Len(tmpstr)-1) should be
    tmpstr = LEFT(tmpstr,Len(tmpstr)-2) To account for the extra ,"

    the ME.Filter line should be

    me.filter = "ContractID In (""" & tmpstr & ")"


    Need to change the

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    No problem. I don't use ADO much, and didn't notice the wildcard right off.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    RayMilhon is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,085
    I use it all the time but this is the first time I needed a wild card.

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

Similar Threads

  1. Replies: 9
    Last Post: 02-24-2015, 11:19 AM
  2. Filter By Form and Apply Filter Buttons
    By JustLearning in forum Forms
    Replies: 13
    Last Post: 02-18-2015, 01:01 PM
  3. Replies: 5
    Last Post: 02-07-2013, 12:21 PM
  4. Replies: 1
    Last Post: 08-16-2012, 01:51 PM
  5. Replies: 28
    Last Post: 03-08-2012, 06:47 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