?
Solved

Need VBA code for search button

Posted on 2009-04-03
5
Medium Priority
?
845 Views
Last Modified: 2012-05-06
I'm still trying to learn VBA for Access. I'm in a bit of a crunch right now and need a simple piece of code...
I have a lookup button on a form. I want to search records by name, but I want it to look at multiple fields on the table and find matches, then give me the option to select a match.

I want to look at either first_name or last_name on Tbl_People.
For instance if I type in "Joe" I want it to return the first and last name of everyone named Joe and allow me to select the record I want.

Using the automated search button allows me to lookup by first_name only. The generated code is below. I can't see where it's even looking at the table, let alone which field based on this code...
Private Sub Command18_Click()
On Error GoTo Err_Command18_Click
 
 
    Screen.PreviousControl.SetFocus
    DoCmd.DoMenuItem acFormBar, acEditMenu, 10, , acMenuVer70
 
Exit_Command18_Click:
    Exit Sub
 
Err_Command18_Click:
    MsgBox Err.Description
    Resume Exit_Command18_Click
    
End Sub

Open in new window

0
Comment
Question by:statler01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 9

Author Comment

by:statler01
ID: 24061165
Ok, I feel a little stupid. I need to not work in the morning...

The form is bound to Tbl_People. I still am not sure how the generated code determines which field in the table to look at though...
0
 
LVL 65

Assisted Solution

by:rockiroads
rockiroads earned 900 total points
ID: 24062735
it will require a little more coding for this. For what you want, a list of records then pick from what you what, probably better to create a new search form for this.

Possibly easier might be to just filter current form with what it finds?


0
 
LVL 30

Accepted Solution

by:
hnasr earned 600 total points
ID: 24062742
Try this Model:

On the form, have a button when clicked it does the following:
   find a recordset of the records with a certain name, assume the fields are ID, Name.
  Loop through record set, and populate a listbox of 2 columns.

When user clicks on one entry, do the following:
   Create a clone recordset of the form
   Find the record with the ID that matches Column(0) of the listbox
  Set the form's bookmark to the recordset's bookmark


VBASearch.mdb
0
 
LVL 65

Assisted Solution

by:rockiroads
rockiroads earned 900 total points
ID: 24062771
Sample of filtering. Validating first that there are records found

Private Sub cmdSearch_Click()

    Dim rs As DAO.Recordset
    Dim sName As String
   
    'Create a copy of the form's recordset
    Set rs = Me.RecordsetClone
   
    'Prompt user for a name. Wrap with NZ to handle nulls
    sName = Nz(InputBox("Enter Name", "Search"), "")
   
    If sName <> "" Then
        rs.FindFirst "first_name = '" & sName & "' OR last_name = '" & sName & "'"
        If rs.NoMatch Then
            MsgBox "No Records Found"
        Else
            Me.filter = "first_name = '" & sName & "' OR last_name = '" & sName & "'"
            Me.FilterOn = True
        End If
    End If
    rs.Close
    Set rs = Nothing
End Sub
0
 
LVL 9

Author Closing Comment

by:statler01
ID: 31566295
This didn't quite take care of it, but got me on the right track. I have more questions, which I will post as new.
Thanks!
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question