Solved

Need VBA code for search button

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
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…

776 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