Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   Seminars   Templates   TechHelp   Fast Tips   Forums   Help   Contact   Join   Order   Logon  
 
 
Access Search Seminar
By Richard Rost   Richard Rost on Twitter Richard Rost on LinkedIn Email Richard Rost   10 years ago


Back to Access Search Seminar
 

SearchF on multiple tables Upload Images   Link  
Elaine Heltman 
11 months ago
I created a Search form with unbound search fields from 4 different tables and with results appearing in 4 unbound list boxes. Works like a dream. Id like some advice on the proper VBA to add to the Search button On-Click event for a Msgbox to appear when an entry is not found for only the search field containing the search text. In my most recent version, the msgbox runs even when a search result is found and if I search using the other search fields - it's indiscriminate. Ive tried a few variations and searched your website for ideas; here is my most recent attempt:

(FYI, The form name is SearchF, the first search field Im testing is LesseeName, and the results listbox is LesseeList.)

If (Forms!SearchF.LesseeList) = " " Or IsNull(Forms!SearchF.LesseeList) Then
MsgBox ("Search entry not found.")
End If

Same results using .LesseeList or !LesseeList
Thank you.
Kevin Robertson
11 months ago
Try this:

If Forms!SearchF!LesseeList.ListCount = 0 Then
   MsgBox ("Search entry not found.")
End If
Elaine Heltman
11 months ago
Thanks for the quick response, but it didn't work. The MsgBox still runs if a result is returned.
Richard Rost
11 months ago
Where are you running that code? In a button?
Elaine Heltman
11 months ago
Yes, in the "Search" button.
Elaine Heltman
11 months ago
A single search button for all 4 search fields (4 tables).
Richard Rost
11 months ago
So the listbox is a BOUND listbox that shows the results of your search based on a query?
Elaine Heltman
11 months ago
No. It's unbound. Here's the code in the Search button:
Private Sub SearchBtn_Click()
    Dim Wh As String
    Dim SQL As String
    Dim X As Integer
    
    
    Wh = ""
    If LesseeName <> "" Then
        If Wh <> "" Then Wh = Wh & " "
        Wh = Wh & "LesseeName LIKE '*" & LesseeName & "*'"
        
        SQL = "SELECT * FROM LesseeQ WHERE " & Wh
        LesseeList.RowSource = SQL
     End If
        
    If LeaseNo <> "" Then
        If Wh <> "" Then Wh = Wh & " "
        Wh = Wh & "LeaseNo LIKE '*" & LeaseNo & "*'"
        
        SQL = "SELECT * FROM LeaseQ WHERE " & Wh
        LeaseList.RowSource = SQL
     End If
    
    If WellName <> "" Then
        If Wh <> "" Then Wh = Wh & " "
        Wh = Wh & "WellName LIKE '*" & WellName & "*'"
        
        SQL = "SELECT * FROM WellNameQ WHERE " & Wh
        WellNameList.RowSource = SQL
     End If

   If WellAPI <> "" Then
        If Wh <> "" Then Wh = Wh & " "
        Wh = Wh & "WellAPI LIKE '*" & WellAPI & "*'

Elaine Heltman
11 months ago
And here's the remainder:
SQL = "SELECT * FROM WellAPIQ WHERE " & Wh
        APIList.RowSource = SQL
     End If

    If OperatorName <> "" Then
        If Wh <> "" Then Wh = Wh & " "
        Wh = Wh & "OperatorName LIKE '*" & OperatorName & "*'"
        
        SQL = "SELECT * FROM OperatorQ WHERE " & Wh
        OperatorList.RowSource = SQL
    End If
        
    If Wh = "" Then
        MsgBox "Enter at least one search term."
        Exit Sub
    End If
      
End Sub
Elaine Heltman
11 months ago
Yes, based on queries.
Richard Rost
11 months ago
Looks like the problem is your Wh variable. You're not resetting it for each box. Therefore each time it's having more stuff added onto the end of it. Put Wh="" before each If block.
Elaine Heltman
11 months ago
Ah, because they're all separate tables, right? I'll try it. Thanks.
Elaine Heltman
11 months ago
Yay, it worked, with Kevin's suggestion (thank you!):
   If Forms!SearchF!LesseeList.ListCount = 0 Then
           MsgBox ("Search entry not found.")
        End If
but I had to delete
If Wh = "" Then
        MsgBox "Enter at least one search term."
        Exit Sub
or it kept popping up.

Now, I'll replicate Kevin's suggestion for each box. Thanks!

This thread is now closed. If you wish to comment, start a NEW discussion, below.
 


Back to Access Search Seminar
 

Start a NEW Conversation
 
Only students may post right now. Click here for more information on how you can set up an account. If you are a student, please LOG ON first.
 
Subscribe
 

You may want to read these articles from the 599CD News:

5/16/2022Referential Integrity
5/15/2022Invoicing Member Discussion
5/13/2022Multivalued
5/12/2022Number Field Size
5/11/2022Create Word Document
5/11/2022Export to Word
5/10/2022On Click Event Timing
5/9/2022Invalid Use of Null
5/7/2022Event Handler
5/6/2022Database Title & Icon
 

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
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
Email Richard
Mailing Address
Phone Number
Fax Number
Course Survey
Blog RSS Feed    Twitter

YouTube Channel    LinkedIn