Results 1 to 4 of 4
  1. #1
    hazeleyre23 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Mar 2016
    Posts
    34

    Using a Command Button to bring up specified criteria

    Hi,

    I have created a command button called 'Search'.

    I want it to bring up the information based criteria in the Start Date and End Date text box, however I want the results to show in a sub form.

    My start date is called txtstartdate and my end date is called txtenddate. My sub form is called Roster_Subform.



    I am very new to Access so appreciate any help/tips.

    Thank you

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    the click event of the button will open a query:
    docmd.openquery "qsMyDateQuery"

    The query uses the date boxes.
    select * from table where [date] between forms!frmRpt!txtStartDate and forms!frmRpt!txtEndDate

  3. #3
    hazeleyre23 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Mar 2016
    Posts
    34
    How do I then get this to populate in a sub form?
    For now my sub form is called frmTest.

    thanks

  4. #4
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,740
    The way to alter the results in the subform is to change its recordsource. So, you need to create a query containing all the data you want to show in the subform and then assign that query as the recordsource for the subform, and you need to do it via VBA code. The below code will create a dynamic query to use as the recordsource. The query name is qtempQuery.
    Here's how.
    To create the query:
    put this somewhere appropriate in the main form's code:
    sub somesub
    Dim strSQL as string
    strSQL = "Select 'whatever you need'"
    CALL fcnCustomizeSQL("qtempQuery", strSQL)
    Me.YOURsubformcontrolname.requery
    end sub
    'Place below sub in a module
    Function fcnCustomizeSQL(qName As String, strPassedSQL As String) As Boolean
    Dim qthisQuery As DAO.QueryDef
    'if the query has been deleted, create it
    If DCount("Name", "MSysObjects", "[Name] = " & Chr$(39) & qName & Chr$(39)) = 0 Then
    Set qthisQuery = CurrentDb.CreateQueryDef(qName, strPassedSQL)
    Set qthisQuery = Nothing
    Exit Function
    End If
    'else modify it
    Set qthisQuery = CurrentDb.QueryDefs(qName)
    qthisQuery.SQL = strPassedSQL
    fcnCustomizeSQL_Exit:
    Set qthisQuery = Nothing
    End Function

    Make recordsource for the subform 'qtempQuery'.

    That should automate the entire process.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 02-13-2014, 05:27 AM
  2. Replies: 3
    Last Post: 08-04-2013, 07:11 AM
  3. Replies: 14
    Last Post: 02-19-2013, 03:16 PM
  4. Replies: 2
    Last Post: 03-22-2012, 01:01 AM
  5. Replies: 1
    Last Post: 07-27-2010, 02:27 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums