Results 1 to 8 of 8
  1. #1
    ts_hunter is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    30

    How clear VBA variable on another form

    I have a search form (frmSearch) that has a Listbox (SearchResults) that is populated by a query. The WHERE clause on that query is 'like *searchtext*', and I get 'searchtext' from a field on the screen that the user can use to narrow the list of records in the Listbox. Once the user double clicks on the record they want, I put up an Edit form for them to modify the record. On the 'Form Close' event of the Edit form, I have this code:



    Forms("frmSearch").SearchResults.Requery

    Which reloads the SearchResults list box, however I would like to clear the 'searchtext' variable on the the first form so I see all the updated records on the first form. In the line of code above, I reference a Listbox on the other form, is there VBA code to set the 'searchtext' = ""?

  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,631
    AFAIK, a query cannot reference a VBA variable. So I am somewhat confused by what you describe.
    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
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    a field on the screen
    I would guess that you are using an unbound text box control on a form.
    In VBA you can use
    Me.TextBox = "" or
    Me.TextBox = NULL

    Replace "TextBox" with the name of the actual control. Then requery the list box.

  4. #4
    ts_hunter is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    30
    Quote Originally Posted by June7 View Post
    AFAIK, a query cannot reference a VBA variable. So I am somewhat confused by what you describe.
    Here is the VBA code that sets up the variable:

    Click image for larger version. 

Name:	code1.JPG 
Views:	12 
Size:	99.0 KB 
ID:	15143

    Here is the Query in Design mode:

    Click image for larger version. 

Name:	qry1.JPG 
Views:	12 
Size:	23.9 KB 
ID:	15144

  5. #5
    ts_hunter is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    30
    Quote Originally Posted by ssanfu View Post
    I would guess that you are using an unbound text box control on a form.
    In VBA you can use
    Me.TextBox = "" or
    Me.TextBox = NULL

    Replace "TextBox" with the name of the actual control. Then requery the list box.

    I tried this but I still get a compile error (below). Isn't the 'Me' reference for the current form?

    Click image for larger version. 

Name:	error.JPG 
Views:	12 
Size:	25.2 KB 
ID:	15145

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,631
    Which line is triggering the error?

    Using a variable and a hidden textbox in addition to the user input textbox seems overly complicated.

    That is the listbox RowSource query? Could be:

    SELECT Plan_ID, BOSS_Plan_Name, BOSS_CSM FROM BOSS_Data WHERE Plan_ID LIKE "*" & [SearchFor] & "*";

    What is important about preserving trailing space? Never seen that.

    Suggest the textbox AfterUpdate event instead of Change event.

    Where is the code that opens the Edit form?

    If you want to provide db for analysis, follow instructions at bottom of my 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.

  7. #7
    ts_hunter is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    30
    Quote Originally Posted by June7 View Post
    Using a variable and a hidden textbox in addition to the user input textbox seems overly complicated.

    What is important about preserving trailing space? Never seen that.
    -This is sample code I found on the net, the hidden box works for me, so I kept it. The trailing space is so someone can't enter an invalid Plan ID.

    Quote Originally Posted by June7 View Post
    Suggest the textbox AfterUpdate event instead of Change event.
    I want the list to refresh after each keystroke


    Anyway, I found the proper syntax to clear the text boxes in the calling form:


    [Forms]![frmSearch]![HiddenSrchText] = ""
    [Forms]![frmSearch]![SearchFor] = ""

  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,631
    So you had the code behind the Edit form to clear the boxes on the Search form.

    An alternative is to open the Edit form in dialog mode. This suspends code execution on the calling form until Edit form is closed.

    Then code could be on the Search form.

    Me.HiddenSrchText = ""
    Me.SearchFor = ""
    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. clear form
    By slimjen in forum Forms
    Replies: 1
    Last Post: 05-16-2012, 02:53 PM
  2. Clear Form
    By hithere in forum Access
    Replies: 4
    Last Post: 03-28-2012, 09:28 AM
  3. How to clear form each time
    By JackieEVSC in forum Forms
    Replies: 1
    Last Post: 10-28-2011, 12:20 PM
  4. How to clear the form?
    By Mrcams in forum Access
    Replies: 3
    Last Post: 01-03-2011, 12:15 AM
  5. Refering to variable form names inside a variable
    By redpetfran in forum Programming
    Replies: 2
    Last Post: 05-21-2010, 01:39 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