Page 1 of 3 123 LastLast
Results 1 to 15 of 32
  1. #1
    data808 is offline Noob
    Windows XP Access 2010 32bit
    Join Date
    Aug 2012
    Posts
    727

    VBA Code not working


    I have a split form with an unbound text box and two buttons. One button for filter and the other for undo filter. Why does this code not work?

    Private Sub cmdFilter_Click()

    Me.Filter = "[initials:] = " & Me.[txtboxFilter]
    Me.FilterOn = True
    Me.Requery

    End Sub

    Private Sub cmdundofilter_Click()

    Me.FilterOn = False
    Me.Requery

    End Sub

    I first used <> in place of the []. The <> give a compile error, expected identifier or bracketed expression. So I tried the []. The [] will work but a after I enter what I want to search in the text box, a parameter prompt pops us again to ask for the same thing I just entered. Its annoying. How do I get this to stop?

    Also when I open this split form that is bound to a query that searches the initials field, it shows all records. Even the records that are missing the initials field. How do I make so that on opening this split form, I start with just the records that actually have the initials field filled out? And from there I can filter it down to one particular clerk's initials?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Initials has : in name? The [] are required if names have spaces or special characters or are reserved words.

    I assume initials is a text field. Text criteria need apostrophe delimiters.

    Me.Filter = "[initials:]='" & Me.txtboxFilter & "'"

    Can save the form with the Filter property set with: Not Is Null

    Or can bind the form to a query that has that static criteria.

    Or can open the form with code that passes filter criteria.

    DoCmd.OpenForm "form name", , , "[initials:] Not 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.

  3. #3
    data808 is offline Noob
    Windows XP Access 2010 32bit
    Join Date
    Aug 2012
    Posts
    727
    Ok I tried the apostrophe delimiters and now the filters are working nicely.

    I don't understand what the form's filter property set to "Not Is Null" does? I set it to that but when I go back and check that property setting, it goes right back to "Initials: chs" which "chs" is the initials I just did a search on. So what is the point of changing it to "Not Is Null" if the setting does not stay saved.

    I also don't understand the following instructions after that as well. What will all this do?

  4. #4
    data808 is offline Noob
    Windows XP Access 2010 32bit
    Join Date
    Aug 2012
    Posts
    727
    Nevermind. You are a genius. Thank you so much for the help. I put the Not Is Null in the criteria row of the query and now there are no blank initial fields when opening the form. So this is awesome.

    I was wondering if you could help me do the same thing on another split form but I want to view a date range. A start date and end date. Do I just make 2 unbound text boxes and then the same filter and undo filter buttons? If so, what would the VBA code be? I want to search records within the two dates that I enter into the 2 unbound text boxes. From 1/1/2001 to 1/1/2005 for example.

  5. #5
    data808 is offline Noob
    Windows XP Access 2010 32bit
    Join Date
    Aug 2012
    Posts
    727
    Sorry another question, do you use apostrophe delimiters for number fields too?

  6. #6
    data808 is offline Noob
    Windows XP Access 2010 32bit
    Join Date
    Aug 2012
    Posts
    727
    How do you add more than just one field to filter? Like if I wanted to add another field besides initials:? How to you add it to the code. Or if I just wanted to search all fields with a partial value. So using the "*" wildcard in the code. Do you know how to do that?

  7. #7
    ipisors is offline Access Developer
    Windows XP Access 2007
    Join Date
    Sep 2013
    Posts
    119
    Just use the single quote delimiter for text - use nothing for number, and # for date. Just like you do in SQL query in Access.

    You should step back and go to the basics of design steps - remove all spaces and odd characters from your field names. Ensure they don't contain reserved words while you're at it.

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    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.

  9. #9
    data808 is offline Noob
    Windows XP Access 2010 32bit
    Join Date
    Aug 2012
    Posts
    727
    Yes but how do you continue the code with a second field? Would it be like this?

    Me.Filter = "[initials:]='" "[permit number:]=" & Me.txtboxFilter & "'"

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Allen Browne's code shows how to build multiple criteria string.
    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.

  11. #11
    data808 is offline Noob
    Windows XP Access 2010 32bit
    Join Date
    Aug 2012
    Posts
    727
    Sorry I tried looking at the Allen Browne's code and looks like greek to me. Is this how you would do it?

    Private Sub cmdFilter_Click()

    Me.Filter = "[initials:]='" & Me.txtFilter & "'" ) AND "
    Me.Filter = "[permit number:]=" & Me.txtFilter & "
    Me.FilterOn = True
    Me.Requery

    End Sub

  12. #12
    data808 is offline Noob
    Windows XP Access 2010 32bit
    Join Date
    Aug 2012
    Posts
    727
    Sorry I do not know what SQL query is. Yeah I got the idea that : is not supposed to be used in a field name from the reaction of June7. I hate to have to back and rename everything and change all the things that are bound to it. I will keep that in mind when making future databases. The code that I wanted, did end up working even with the : character in the field name so right now I think I'm going to leave it alone. I just really want to finish this project up and use it as a learning experience to start my next one. So if you know how to do any of the other filters that I mentioned in this thread, please post what you can. I am a hands on learner. I need to get into things, make mistakes and learn from it to absorb it. I'm not very good at learning the fundamentals first and going the traditional way.

  13. #13
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    How can the same value be found in two completely different fields? Initials and PermitNumber cannot have the same criteria.

    PermitNumber is a number data type?

    Private Sub cmdFilter_Click()
    Me.Filter = "[initials:]='" & Me.txtInitials & "' AND [permit number:]=" & Me.txtPermitNum
    Me.FilterOn = True
    End Sub
    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.

  14. #14
    ipisors is offline Access Developer
    Windows XP Access 2007
    Join Date
    Sep 2013
    Posts
    119
    This is how you would do it:

    Me.Filter = "[initials:]= '" & Me.txtInitials & "'" & " AND [permit number:]=" & Me.txtPermitNum

    Assuming that you really have colons in your field names (which you should not) and assuming that the field initials: is a text field in the table, and assuming taht permit number is a numerical field and it really has a space which it should not.

    You should clean up the basics of table design before going further in your database - no spaces, no special characters, etc., in any object names.

  15. #15
    data808 is offline Noob
    Windows XP Access 2010 32bit
    Join Date
    Aug 2012
    Posts
    727
    "How can the same value be found in two completely different fields? Initials and PermitNumber cannot have the same criteria."

    The goal was to have a single text box to type whatever I want in it and for the filter to show the records that contain all or part of the value I type. I'm not sure if this can be done but the idea came from the navigation control search box at the bottom of the window. The only thing I do not like about it is it still shows all the records and just highlights the record when it finds the first one that fits my criteria entered.

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

Similar Threads

  1. Replies: 2
    Last Post: 06-28-2013, 12:58 PM
  2. Code not working!!
    By jfn15 in forum Programming
    Replies: 6
    Last Post: 06-10-2013, 09:20 AM
  3. Code Not Working
    By Kirsti in forum Programming
    Replies: 3
    Last Post: 03-26-2012, 02:48 PM
  4. VBA Code Not working
    By jo15765 in forum Programming
    Replies: 12
    Last Post: 12-03-2010, 04:01 PM
  5. VBA Code for working with IE
    By smikkelsen in forum Programming
    Replies: 3
    Last Post: 04-15-2010, 01:05 AM

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