Solved

Need VBA code for search button

Posted on 2009-04-03
5
842 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 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

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…
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

739 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