Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Search Form 2.0    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Pulling my hair out
Sandra Truax 
         
4 years ago
I love this code and have it working perfect... BUT I have to copy the exact code to each form to get it to work.  I have tried putting it in a Public Sub, and I have tried putting it in a Public Function called MyFilterCode, and cannot get it to work.  I even just created a button, and it's only function is to msgbox MyFilterCode and it comes back blank.

Can someone please help me make this code work?

I guess I should add, the first form is set up just like the one in the video to set the SQL source to filter the form.  The second form uses the same WhereStr portion, but then I used that to calculate the total of my checking accounts.

Like I said, both works, but I have to duplicate the WhereStr code on both forms, and that's a lot of code?
Sandra Truax OP  @Reply  
         
4 years ago
This is one attempt. I'm wondering, can I include a public sub or public function INSIDE a private function?

Private Sub RequeryMainForm()

    Dim SQL As String, WhereStr As String
          
    MyFilterCode
    MsgBox MyFilterCode
    SQL = "SELECT * FROM BudgetQ"
    
    If WhereStr <> "" Then
        SQL = SQL & " WHERE " & WhereStr
    End If
    
        SQL = SQL & " ORDER BY TransactionDate"
    
    
    Me.Register.Form.RecordSource = SQL
    SQL2 = SQL
Kevin Robertson  @Reply  
          
4 years ago
You will need to send several parameters to the procedure. Since I don't know your database, I used the one Richard built in the video and modified it. The screenshot below should give you a good idea how to proceed.
Kevin Robertson  @Reply  
          
4 years ago

Kevin Yip  @Reply  
     
4 years ago
Richard's code is designed to be run for that one form only, and if you want to put that code in a public procedure, that code needs to be re-written to run for other forms.  

For instance, at 9:30 of the extended cut video, in this part of the code:

     If FirstNameSearch <> "" Then ...

"FirstNameSearch" is equivalent to Me!FirstNameSearch, which only refers to that particular control on that form.  Outside of the form, that reference has no meaning (and has no value), and that's why your public procedure didn't work.  Your probably didn't use "Option Explicit", so Access thinks "FirstNameSearch" was a public variable, one that had no value.  So that's why your public function returned blank.

To make this work in a public function for ANY form that has a "FirstNameSearch" control, your public function needs to accept a reference of the form (F, in the example below) that needs to use this function:

     Public Function MyFilterCode(F As Variant) As String
          Dim WhereStr As String
          If F!FirstNameSearch <> "" Then
               WhereStr = ...
               ...
          End If
          MyFilterCode = WhereStr
     End Function

Your form, or any form, will then be able to call this function in this manner:

     WhereStr = MyFilterCode(Me)

"Me" is a reference to your current form.  It is passed to MyFilterCode, so it knows the code is being run on that form.
Sandra Truax OP  @Reply  
         
4 years ago
Thank you Kevin!  I think I now know what I need to do.
Sandra Truax OP  @Reply  
         
4 years ago
I want to say thanks again to both Kevins. Unfortunately I can't get either to work, so for now I guess I will just stay with duplicate code. I've not given up on your suggestions though, and will continue to try to get one of them to work.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Search Form 2.0.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 4/15/2026 6:46:48 PM. PLT: 1s