Results 1 to 15 of 15
  1. #1
    Gina Maylone is offline Always learning
    Windows 10 Access 2016
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544

    Need another set of eyes please

    Good morning!

    I am trying to filter a form based on a wildcard search, I also need to filter it IF a date filter is in place. Here's what I have and the results it produces.


    Code:
    Private Sub WildProjectName_AfterUpdate()
    
    Dim strWhere As String
    
    
        'Make string
            If Nz([Forms]![FMainMenu].[DateFilter], "") <> "" Then
                strWhere = strWhere & "[biddate] >= #" & [Forms]![FMainMenu]![DateFilter] & "# AND "
            End If
    
    
        If Nz(Me.WildProjectName, "") <> "" Then
            strWhere = strWhere & "ProjectName like '*" & [Forms]![FMainMenu]![FProjectMain].[Form]![WildProjectName] & "*'"
        End If
    
    
        'Apply filter
        If strWhere <> "" Then
            Me.Form.Filter = strWhere
            Debug.Print strWhere
            
            Form_FProject.FilterOn = True
        'Else
            Form_FProject.Filter = ""
            Form_FProject.FilterOn = False
        End If
    
    
    End Sub
    Debug gives me: [biddate] >= #7/2/2020# AND ProjectName like '*veri*', yet it doesn't filter the form correctly/at all. I thank you in advance for taking the time!

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Debug gives me: [biddate] >= #7/2/2020# AND ProjectName like '*veri*', yet it doesn't filter the form correctly/at all.
    Is that 7th Feb or 2nd July. Perhaps read: http://allenbrowne.com/ser-36.html 2. Wrong Formatting in Code
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    Gina Maylone is offline Always learning
    Windows 10 Access 2016
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    Thanks Bob! For the records it's July 2, 2020. I looked at Allen Brown's page and wrapped the date as he suggested, and got the same results. My error is here:
    Code:
      strWhere = strWhere & "ProjectName like '*" & [Forms]![FMainMenu]![FProjectMain].[Form]![WildProjectName] & "*'"
    It's just straight text, I don't know what I'm missing. Thank you again for your time.

  4. #4
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Quote Originally Posted by Gina Maylone View Post
    Thanks Bob! For the records it's July 2, 2020. I looked at Allen Brown's page and wrapped the date as he suggested, and got the same results. My error is here:
    Code:
      strWhere = strWhere & "ProjectName like '*" & [Forms]![FMainMenu]![FProjectMain].[Form]![WildProjectName] & "*'"
    It's just straight text, I don't know what I'm missing. Thank you again for your time.
    How do you know that the error is there and not in:
    strWhere = strWhere & "[biddate] >= #" & [Forms]![FMainMenu]![DateFilter] & "# AND "
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  5. #5
    Gina Maylone is offline Always learning
    Windows 10 Access 2016
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    I commented out the date section, and got the error with the next section. :-)

  6. #6
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Quote Originally Posted by Gina Maylone View Post
    I commented out the date section, and got the error with the next section. :-)
    What error did you get.
    What does Debug.Print give you
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  7. #7
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,416
    See the red. Both refer to same form? It isn't clear what form contains the code...

    Code:
    Private Sub WildProjectName_AfterUpdate()
    
    
    Dim strWhere As String
    
    
        'Make string
            If Nz([Forms]![FMainMenu].[DateFilter], "") <> "" Then
                strWhere = strWhere & "[biddate] >= #" & [Forms]![FMainMenu]![DateFilter] & "# AND "
            End If
    
    
        If Nz(Me.WildProjectName, "") <> "" Then
            strWhere = strWhere & "ProjectName like '*" & [Forms]![FMainMenu]![FProjectMain].[Form]![WildProjectName] & "*'"
        End If
    
    
        'Apply filter
        If strWhere <> "" Then
            Me.Form.Filter = strWhere
            Debug.Print strWhere
            
            Form_FProject.FilterOn = True
        'Else
            Form_FProject.Filter = ""
            Form_FProject.FilterOn = False
    	End If
    End Sub

  8. #8
    Gina Maylone is offline Always learning
    Windows 10 Access 2016
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    Type mismatch with
    Code:
     strWhere = strWhere & "[ProjectName] Like " * " & [Forms]![FMainMenu]![FProjectMain].[Form]![WildProjectName] & " * ""
    highlighted. Thank you again Bob!

  9. #9
    Gina Maylone is offline Always learning
    Windows 10 Access 2016
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    Good catch davegri! Didn't fix the error, but I changed the form reference. Thank you!

  10. #10
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,117
    Can you please try:

    Code:
     strWhere = strWhere & "[ProjectName] Like '*" & Forms("FMainMenu").FProjectMain.Form.Controls("WildProjectName") & "*'"
    assuming the textbox bound to the WildProjectName field has the same name.
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  11. #11
    Gina Maylone is offline Always learning
    Windows 10 Access 2016
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    I got a syntax error Vlad.

    Thank you for your time though!Click image for larger version. 

Name:	syntaxerror.JPG 
Views:	27 
Size:	12.8 KB 
ID:	42519

  12. #12
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,117
    Where is the semi-colon coming from, should be AND? What do you get in the intermediate window?
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  13. #13
    Gina Maylone is offline Always learning
    Windows 10 Access 2016
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    From immediate window: [biddate] >= #07/01/2020# AND ProjectName like '*veri*'

    No longer getting the syntax error. But not getting filtered results either. I can get what I need using a change in the recordsource. I just wish I could master the strwhere. Thank you again Vlad.

  14. #14
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,416
    Shot in the dark. I seem to remember a similar problem that I had and solved it this way.
    Try setting the form control values to variables like this:

    Code:
    Private Sub WildProjectName_AfterUpdate()
    
    
    Dim strWhere As String
    Dim dDate as date
    dim sName as string
    
    
    dDate = "#" & [Forms]![FMainMenu].[DateFilter] & "#"
    sName = "*" & [Forms]![FMainMenu]![FProjectMain].[Form]![WildProjectName] & "*"
        'Make string
            If dDate <> "##" Then
                strWhere = strWhere & "[biddate] >= " & dDate & " AND "
            End If
    
    
        If sName <> "**" Then
            strWhere = strWhere & "ProjectName like '" & sName & "'"
        End If
    
    
        'Apply filter
        If strWhere <> "" Then
            Me.Form.Filter = strWhere
            Debug.Print strWhere
            
            Form_FProject.FilterOn = True
        'Else
            Form_FProject.Filter = ""
            Form_FProject.FilterOn = False
    	End If
    End Sub

  15. #15
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Don't forget to strip the " AND " off the end of the string if the second filter option doesn't execute.

    Something like:
    str = left( str, len( str ) - 5)

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

Similar Threads

  1. Need a fresh set of eyes please (Dcount issue)
    By Gina Maylone in forum Access
    Replies: 3
    Last Post: 02-23-2017, 03:12 PM
  2. Unknown syntax error. Any spare eyes out there?
    By redbull in forum Programming
    Replies: 3
    Last Post: 08-31-2012, 11:55 AM
  3. New eyes on a quick update query
    By Lorlai in forum Queries
    Replies: 3
    Last Post: 07-14-2011, 09:51 AM
  4. VBA Excel Formatting: Need new eyes!!
    By Lorlai in forum Programming
    Replies: 3
    Last Post: 06-09-2011, 10:22 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