Results 1 to 2 of 2
  1. #1
    NickWren is offline Advanced Beginner
    Windows XP Access 2013
    Join Date
    Dec 2015
    Location
    Connecticut
    Posts
    70

    Query Criteria and excluding fields when extracting the query

    So I have a query that a user wants to be able to pick and choose the fields they want to extract to excel.


    Ex. Lets say the first time the user wants to extract fields A, B, and C and next time they want A, B, D, and E and Next time A,E,F,G and H. (So they might have a similar fields as last time but the user wants to define the fields they want to extract)

    I was thinking about making a checkbox for each field in the query and if the checkbox was checked then extract the values for that field, is there an easier way of doing this?

    Next lets use the example from above where they want to extract A, E, F, G, and H.
    Now for A's criteria they want everyone with their name starting with J
    For E's criteria they want a date later than 2017
    For F's criteria they want anyone over the age of 30
    for G's they want someone with a score higher than 80
    and for H's criteria they want someone with a certain diagnosis.

    I know how criteria works;
    Ex. Like "J*" for A's Criteria, Year(ReceivedDate) >= 2017, Age > 30, Score > 80, "Some Diagnosis"

    But now putting this all together in one line or each on their own separate criteria line the criteria doesnt work properly for me; either not filtering anything or not being able to find any samples that fall into that criteria even though I know some samples have that as criteria. ( I am going to need this to work with the above question)

    Side note: How would you extract what you see on a (data table) subform?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    This will require VBA that modifies query object referencing QueryDefs. Here is example code that just modifies query filter criteria:
    Code:
    Private Sub btnRun_Click()
    If IsNull(Me.tbxFilter) Then
       Me.tbxFilter = "SELECT * FROM ProjectRatesMainSub WHERE grading Like '*' " & GetMisc() & " ORDER BY projects.proj_num, grading;"
    End If
    Dim qdfUser As DAO.QueryDef
    CurrentDb.QueryDefs.Delete ("UserQuery")
    Set qdfUser = CurrentDb.CreateQueryDef("UserQuery", Me.tbxFilter)
    DoCmd.OpenQuery "UserQuery"
    
    
    Private Sub btnExcel_Click()
    Dim qdfUser As DAO.QueryDef
    CurrentDb.QueryDefs.Delete ("UserQuery")
    Set qdfUser = CurrentDb.CreateQueryDef("UserQuery", Me.tbxFilter)
    DoCmd.OpenQuery "UserQuery", , acReadOnly
    DoCmd.RunCommand acCmdExportExcel
    End Sub
    Here is the code for the GetMisc() function that builds the filter criteria:
    Code:
    Public Function GetMisc() As String
    GetMisc = GetMisc & IIf(Me.chkIssue, " major_issue=True AND", "")
    GetMisc = GetMisc & IIf(Me.chkMaterial, " new_material=True AND", "")
    GetMisc = GetMisc & IIf(Me.chkTechnique, " new_technique=True AND", "")
    GetMisc = GetMisc & IIf(Me.chkSpec, " new_spec=True AND", "")
    If GetMisc <> "" Then GetMisc = " AND " & Left(GetMisc, Len(GetMisc) - 4) & " "
    End Function
    You will need more code that dynamically builds the field string to replace the * as shown in my SQL statement.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Replies: 3
    Last Post: 03-31-2016, 09:31 AM
  2. Query multiple fields for the same criteria
    By labratKY in forum Queries
    Replies: 3
    Last Post: 08-07-2014, 02:37 PM
  3. Query is excluding records with blank fields
    By Menelaus in forum Queries
    Replies: 1
    Last Post: 03-28-2013, 08:14 PM
  4. Using form fields as query criteria
    By bener in forum Queries
    Replies: 3
    Last Post: 10-28-2011, 12:54 PM
  5. Yes/No Fields and Criteria in a Query
    By omicronkappa278 in forum Access
    Replies: 1
    Last Post: 05-22-2009, 09:51 AM

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