Solved

Need VBA code for search button

Posted on 2009-04-03
5
841 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
  • 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 300 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 200 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 300 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

680 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