Results 1 to 10 of 10
  1. #1
    armyofdux is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2014
    Posts
    55

    Clear Search Form


    I searched this topic and got many replies but none seem to be working for me. I have an unbound search form that takes user input and displays data based on the input. The search functions work as advertised but I need a button to clear the fields. I have tried the on_click methods with me.FieldName and it returns the error message "If 'me; is a new macro or macro group, make sure you have typed its names correctly.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,948
    Where did you put the code? Is this VBA?

    Post code for analysis.
    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
    armyofdux is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2014
    Posts
    55
    Code:
    Private Sub FY_Click()
    Me.FY = vbNullString
    End Sub
    
    
    End Sub
    
    Private Sub SCHEDULE_ID_Click()
    Me.SCHEDULE_ID = vbNullString
    End Sub

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,948
    Where is this code placed? Behind what form? The same form that has those controls?

    I don't use empty strings, I set controls to 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.

  5. #5
    armyofdux is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2014
    Posts
    55
    Yeah I found where I messed up. I put the code in the wrong spot. Got too quick. I also found copy/paste tends to screw things up as opposed to typing stuff out. It's not the ideal design, as I wanted a single "clear form" button at the bottom.

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,948
    At bottom of what? One button should certainly be able to run code to clear each control.
    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
    armyofdux is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2014
    Posts
    55
    At the bottom of the search form. As my Access skills grow June I am getting better,,,but VBA and I don't get along yet. Right now it's clearing each field properly as you click on it.

  8. #8
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Code:
    dim ctl as control
    
    for each ctl in me.controls
        ctl = null
    next ctl
    Is probably the simplest way

    if your controls contain a mix of data types you will have to modify this (for instance yes/no checkboxes)

    If you need to identify a subset of your controls you can use the TAG property and all your 'search' fields put SF in the TAG property then do something like

    Code:
    dim ctl as control
    for each ctl in me.controls
        if instr(ctl.tag, "SF") > 0 then
            ctl = null
        endif
    next ctl

  9. #9
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Quote Originally Posted by armyofdux View Post

    ...I also found copy/paste tends to screw things up as opposed to typing stuff out...
    It does, indeed! That's how you get things like this

    Code:
    Private Sub FY_Click()
     Me.FY = vbNullString
    End Sub
    
    End Sub

    where you've pasted one Sub into the middle of another, existing Sub! If you'd posted more of you code, here, you'd undoubtedly have something like this:

    Code:
    Private Sub SomeOtherControlName_Click()
    
     Private Sub FY_Click()
      Me.FY = vbNullString
     End Sub
    
    End Sub

    which can give you some really strange Error Messages that can make it difficult to debug your code! I'm not saying don't ever copy and paste, but writing code is a very precise endeavor, and consequently you have to be very precise in inserting code into code!

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  10. #10
    ryanmce92 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2015
    Posts
    48
    Quote Originally Posted by armyofdux View Post
    I searched this topic and got many replies but none seem to be working for me. I have an unbound search form that takes user input and displays data based on the input. The search functions work as advertised but I need a button to clear the fields. I have tried the on_click methods with me.FieldName and it returns the error message "If 'me; is a new macro or macro group, make sure you have typed its names correctly.
    I had the same issue trying to create a clear form button, originally I was trying to use the Undo button that Access already create, but hopefully the below will work:


    It should be simple enough to clear the form with the use of a single button, go to form design, create the button from the design toolbar, click exit.

    Click on view code in the menu bar

    Enter/copy and paste the following code


    Private Sub Command52_Click()
    [txtName].Value = ""
    End Sub

    (Command 52 is the name of the button and should be changed to the name of your button)
    (txtName is the name of the text box/combo box in the form)
    (To add more just hit enter after the second speech mark and go with the same format [cboText].Value = ""

    Hopefully this helps.
    Last edited by ryanmce92; 03-03-2015 at 10:53 AM.

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

Similar Threads

  1. Replies: 2
    Last Post: 03-12-2014, 01:31 AM
  2. Replies: 2
    Last Post: 08-03-2013, 03:41 PM
  3. Search field will not clear
    By SemiAuto40 in forum Programming
    Replies: 1
    Last Post: 10-13-2011, 08:23 PM
  4. How to clear a search form ?
    By Trojnfn in forum Access
    Replies: 2
    Last Post: 10-05-2011, 01:12 PM
  5. Search and Clear button
    By polk383 in forum Programming
    Replies: 1
    Last Post: 08-30-2006, 08:51 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