Apply Filter as Variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anduril12
    New Member
    • Mar 2010
    • 6

    Apply Filter as Variable

    Hello,
    I'm very new to VBA programming, so this is my first attempt at creating a interactive (with forms) database. I have a database of a few thousand records, containing first name, last name, city, state, and phone numbers.

    What I'd like to do is have the user enter the First Name into a text box and then the FIRSTNAME column is filtered by that value, thereby searching the database for all records with that first name.

    Now the first time I open the database and click the button that triggers the code it pops up a window that says "parameter value?" for the name I've entered into the text box.

    Any ideas? Thank you so much.

    Here is the code I have:

    Private Sub Find_Click()
    On Error GoTo Err_Find_Click
    Dim strName As String
    strName = ""
    Me.first.SetFoc us
    strName = first.Text
    DoCmd.ApplyFilt er , "FIRSTNAME = " & strName
    Me.first.SetFoc us

    Exit_Find_Click :
    Exit Sub

    Err_Find_Click:
    MsgBox Err.Description
    Resume Exit_Find_Click

    End Sub

    *Note: "first" is the name of the text box
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Code:
    Dim varFirst As Variant    'In case of NULLs
    
    varFirst = Me![First]
    
    If Not IsNull(Me![First]) Then      'Is a Value present?
      'Do any Record(s) with that First Name actually exist? Be sure
      'to substitute your own Table and Field Name below
      If DCount("*", "Employees", "[FirstName] = '" & varFirst & "'") > 0 Then
        DoCmd.ApplyFilter , "FIRSTNAME = '" & varFirst & "'"
      Else
        MsgBox "No Records with a First Name of " & varFirst & " exist in the Database", _
                vbExclamation, "No Matching Records Found"
      End If
    End If

    Comment

    • anduril12
      New Member
      • Mar 2010
      • 6

      #3
      ADezii,
      Thanks so much! It worked perfectly! great to have the help :)

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        You are quite welcome.

        Comment

        Working...