Results 1 to 5 of 5
  1. #1
    octsim is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    15

    Change query field in form

    Hello,



    is it possible to change a query's field through a form?
    I have 2 list controls with the names all the fields in my table - I want the user to select 2 fields and perform a query that shows all the data of only those 2 fields. I know how to use a listbox in the criteria but I want the fieldname to be selected, not the search criteria - or am thinking wrong?

    Thanks in advance!

    octsim

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Why do you have two listboxes? Are they dependent upon each other? If you want a dynamic SELECT statement, this is possible. Is it a form that is dependent on the query?

  3. #3
    octsim is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    15
    I have 2 listboxes which display the same values - all field names in my table. I want the user to be able to select 2 fields from my table through a form and perform a select query, I just can't find how to use the fields as a parameter.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,959
    AFAIK, a saved Access query object cannot be set up to change the fields that are selected (I have tried).

    VBA can modify a saved Access query object using QueryDefs collection or delete query and resave it with different structure (I do this). Examples:
    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"
    End Sub
    
    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
    Or maybe you don't want to change the fields that are selected in the query, just change the filter criteria. I use VBA to construct WHERE clause and then apply it to the Filter property of form or pass it to a form/report with WHERE CONDITION argument of DoCmd.OpenForm (or OpenReport). Review http://allenbrowne.com/ser-62.html
    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.

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    In the end, you may find you only need one list box. It is a little more code to get selections from a multiselection list box verses a listbox that only allows one selection or a combobox.

    If you want your form's recordsource to be dependant on the listbox controls' values, you will need to adjust the form's filter or its recordsource. I prefer to adjust the recordsource. Adjusting the form's filter can be risky. The user will most likely have the ability to modify, turn on, and turn off the filter via toolbars, etc.

    The very first step is to translate the listbox values into a WHERE clause. In order to do this you need to identify a socalled Child Link and Main Link. YOu need to identify the field that will act as a PK(Main) in the listbox and the field that will act as the FK(Child) in the form. Don't take the terms used here too literaly. You need to identify which column the field resides in within the listbox.

    Your where clause may begin to look something like this....

    dim lngID as Long
    dim strWhere as string

    lngID = Me.lstControl1.Value
    strWhere = " WHERE [ID] = " & lngID

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

Similar Threads

  1. Change field criteria in a report, or query
    By mdnikki in forum Reports
    Replies: 3
    Last Post: 10-05-2012, 12:43 PM
  2. Form change a query field?
    By 4shl3y in forum Access
    Replies: 1
    Last Post: 07-11-2012, 12:14 PM
  3. Change Text field to Memo Field in a Query
    By Yarbz in forum Queries
    Replies: 5
    Last Post: 06-28-2012, 05:24 PM
  4. Replies: 1
    Last Post: 07-14-2011, 05:44 AM
  5. Change Name of field within Query
    By bkelly101 in forum Access
    Replies: 1
    Last Post: 06-23-2011, 06:56 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