Solved

Need VBA code for search button

Posted on 2009-04-03
5
838 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now