Retrieve all records or specific records

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KMEscherich
    New Member
    • Jun 2007
    • 69

    Retrieve all records or specific records

    Using Access 2003

    Hi there, can someone please tell me how I can give someone an option of entering 1 or two parameters or get all records???

    1) I would like to have an option of selecting Parameter 1( Date = 05/25/2008) and have all records displayed be according the date parameter of 05/25/2008.

    2) Another option would be to enter Parameter 1 (date = 05/25/2008) + Parameter 2 (Manager = Donald Duck). After doing this, I would see all data associated with Donald Duck with encounters on 05/25/2008.

    3) I would like to have the final option be when I don't select any parameters, I would get any and all data in the database.

    Thank you VERY MUCH for your assistance.
  • Delerna
    Recognized Expert Top Contributor
    • Jan 2008
    • 1134

    #2
    One method, which is hard to describe but not so hard to do, is

    you could use a datagrid form with a query string in the datasource for the form.
    In the forms header you would have controls for the user to enter the parameters you need. In the change event for each control you would have code that checks the contents of the parameter controls and create a query string accordingly and then sets the forms datasource to the new query string and refreshes the form.

    eg
    the original query in the forms datasource would be something like
    [code=sql]
    SELECT *
    FROM YourTable
    [/code]

    Then when the user enters a date in the date control and presses enter
    the code in the change event would be something like
    [code=vba]
    DIM strSQL as string
    strSQL="SELECT * FROM YourTable"
    if DateControl<>"" then
    if NameControl<>"" then
    strSQL=strSQL & " WHERE DteField=#" & DateControl & "#"
    strSQL=strSQL & " and NameField='" & NameControl & "'"
    else
    strSQL=strSQL & " WHERE DteField=#" & DateControl & "#"
    end if
    else
    if NameControl<>"" then
    strSQL=strSQL & " WHERE NameField='" & NameControl & "'"
    end if
    end if
    me.datasource=s trSQL
    me.requery
    me.refresh
    [/code]



    Hope that helps

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32662

      #3
      Check out Example Filtering on a Form. Let me know if you need further pointers after digesting that. It includes examples and a small database to download.

      Comment

      Working...