0

I'm using the CurrentDb.QueryDefs object to alter the sql in my query. This works well, until I change the select clause. I want the query to now show only the fields named in my new select clause. Instead, the column headers are still showing, but there are no values. I'm displaying the query in a subform.

How can I force the subform/query to only show the specified columns that can change on a button click?

REASON: This is an advanced search form where checkboxes represent each field, and the user can remove and add fields each time they search.

10
  • This one reminds me of another question from last week. See if the answers there are useful to you: stackoverflow.com/questions/6194992/… Commented Jun 9, 2011 at 19:21
  • @HansUp -Not exactly, I already have the data displayed. My problem is in updating the display. Commented Jun 9, 2011 at 20:08
  • I thought you wanted a form which adapts itself in response to different sets of fields in its record source. That's what @HK1 described. How is your goal different? Commented Jun 9, 2011 at 20:20
  • @HansUp -Its much different. @HK1 doesn't even have the results displayed yet in any fashion. "How can I display the recordset records in Access?" he asked. I already have mine displayed, I just cannot figure out how to update that afterwards. Commented Jun 9, 2011 at 20:33
  • I'm almost certain you and I are not on the same page. Scott asked the question you quoted. @HK1 responded in part "... set the controlsource on your controls to correspond with one of the fields in the recordset. On every control that is not in use needs to have it's ColumnHidden property set to true. You'll also have to change the caption of the associated label to show the appropriate column name for each control that will be visible." Commented Jun 9, 2011 at 21:05

1 Answer 1

2

You can't requery, you have to refresh the subform's source object:

MySubformControl.SourceObject = ""
MySubformControl.SourceObject = "Query.MyQuery"

For testing, I created a table Table1 with fields Field1,...,Field4, then a form with 4 check boxes and a subform, then a query Query1 which the fields from Table1. Here is the code behind the form (I let Access name all my objects, so the subform is called Child8):

Private Sub Check0_AfterUpdate()
    Rewrite_Query
End Sub

Private Sub Check2_AfterUpdate()
    Rewrite_Query
End Sub

Private Sub Check4_AfterUpdate()
    Rewrite_Query
End Sub

Private Sub Check6_AfterUpdate()
    Rewrite_Query
End Sub

Private Sub Rewrite_Query()

    Dim qdf     As QueryDef
    Dim strSQL  As String

    Set qdf = CurrentDb.QueryDefs("Query1")
    If Check0.Value = True Then
        If Len(strSQL) > 0 Then strSQL = strSQL & ","
        strSQL = strSQL & "Field1"
    End If

    If Check2.Value = True Then
        If Len(strSQL) > 0 Then strSQL = strSQL & ","
        strSQL = strSQL & "Field2"
    End If

    If Check4.Value = True Then
        If Len(strSQL) > 0 Then strSQL = strSQL & ","
        strSQL = strSQL & "Field3"
    End If

    If Check6.Value = True Then
        If Len(strSQL) > 0 Then strSQL = strSQL & ","
        strSQL = strSQL & "Field4"
    End If

    strSQL = "SELECT " & strSQL & " FROM Table1"

    qdf.SQL = strSQL
    qdf.Close
    Set qdf = Nothing

    Child8.SourceObject = ""
    Child8.SourceObject = "Query.Query1"

End Sub
Sign up to request clarification or add additional context in comments.

3 Comments

You got it! The only piece I was missing was to set the SourceObject to empty string then back to the query. Thanks!
No problem. Not strictly necessary to use the empty string--you can comment it out and it will still work, but it shows what the next line does implicitly I thought.
Yes, I just tested that, it makes sense. I tried to use .Requery before, which kept the same fields but without data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.