Results 1 to 9 of 9
  1. #1
    b82726272 is offline Novice
    Windows Vista Access 2007
    Join Date
    Apr 2014
    Posts
    21

    Loop dynamic query

    Hi,



    I have created a dynamic query with the code below. This works fine for one record in a subform. But how can I execute the code for all the records in the subform? Imagine, if I have two records in the subform, one with function A and one with function B, how can I combine these in one query? Thank you for the help.

    Kind regards,

    Code:
    Private Sub cmd_ButtonClick()
        Dim db As DAO.Database
        Dim qdf As DAO.QueryDef
        Dim strSQL As String
        Set db = CurrentDb
        Set qdf = db.QueryDefs("Query")
        strSQL = "SELECT TABLE.FUNCTION " & _
                 "FROM TABLE " & _
                 "WHERE TABLE.FUNCTION='" & Me.FORM.Controls!ASS_FUNCTION.Value & "';"
        qdf.SQL = strSQL
        DoCmd.OpenQuery "Query"
        Set qdf = Nothing
        Set db = Nothing
    End Sub

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,549
    Usu. records in a subform are from a query...use THAT query then link to it and execute your results qry.

  3. #3
    b82726272 is offline Novice
    Windows Vista Access 2007
    Join Date
    Apr 2014
    Posts
    21

    Code with loop

    I have used the code below for the loop. But this doesn't work. Only the first function is in the loop. How can I set the subform as the datasource of the recordset?

    Kind regards,

    Code:
    Private Sub cmdButton_Click()
        Dim db As DAO.Database
        Dim qdf As DAO.QueryDef
        Dim strSQL As String
        Dim rs As DAO.Recordset    
        Set db = CurrentDb
        Set qdf = db.QueryDefs("Query")
        Set rs = CurrentDb.OpenRecordset("TABLE")    
        If rs.Recordcount <> 0 Then
         rs.MoveFirst
         While Not rs.EOF
           strSQL = "SELECT TABLE.FUNCTION " & _
                    "FROM TABLE " & _
                    "WHERE TABLE.FUNCTION='" & Me.SUBFORM.Controls!ASS_FUNCTION.Value & "';"
           qdf.SQL = strSQL
          rs.MoveNext
         Wend
        End If
        DoCmd.OpenQuery "QUERY"
        rs.Close
        Set qdf = Nothing
        Set db = Nothing
        Set rs = Nothing
    End Sub

  4. #4
    b82726272 is offline Novice
    Windows Vista Access 2007
    Join Date
    Apr 2014
    Posts
    21
    I have tested it with a count message and if I set the table of the main form or subform as datasource for the recordset I get all the records form the tables. But I want only to count the records of the subform. How can I do this? Between the main form and subform is an one to many relationship.

    Kind regards,

  5. #5
    ipisors is offline Access Developer
    Windows XP Access 2007
    Join Date
    Sep 2013
    Posts
    119
    ranmain:
    Not all forms are based on queries. Many people dynamically set recordsources in code..

    b827(...)

    What do you want to actually DO with the recordset after establishing it?

    You can set a Recordset to any bound form's current recordsetclone at the moment:

    Code:
    Dim db As DAO.Database
    Dim qdf As DAO.QueryDef
    Dim strSQL As String
    Dim rs As DAO.Recordset
    Set db = CurrentDb
    Set rs = Form_PutYourFormNameHere.RecordsetClone

  6. #6
    b82726272 is offline Novice
    Windows Vista Access 2007
    Join Date
    Apr 2014
    Posts
    21
    Hi ipisors,

    Thank you for the reaction. The form is not based on a query but on tables. I have made the form with the wizard. The problem is that I only want to get the records of the subform and not all records of the subtable. That is why I was thinking about a loop. Based on the data of the subform I want to execute a query.

    Kind regards,

    Based on this data I want to excecute a query.

    Quote Originally Posted by ipisors View Post
    ranmain:
    Not all forms are based on queries. Many people dynamically set recordsources in code..

    b827(...)

    What do you want to actually DO with the recordset after establishing it?

    You can set a Recordset to any bound form's current recordsetclone at the moment:

    Code:
    Dim db As DAO.Database
    Dim qdf As DAO.QueryDef
    Dim strSQL As String
    Dim rs As DAO.Recordset
    Set db = CurrentDb
    Set rs = Form_PutYourFormNameHere.RecordsetClone

  7. #7
    ipisors is offline Access Developer
    Windows XP Access 2007
    Join Date
    Sep 2013
    Posts
    119
    The problem is that I only want to get the records of the subform
    I understand that, so use the code I provided you to substitute the name of your subform (it's a Form object, too, you know).
    From that you'll have to decide what you want to DO with the data. I still don't understand that. "execute a query" isn't meaningful to me. If you want to select the data from the subform's recordset, then do what I posted. From there you'll need to decide what you want to do with that data.

  8. #8
    b82726272 is offline Novice
    Windows Vista Access 2007
    Join Date
    Apr 2014
    Posts
    21
    Hi ipisors,

    Thank you for your help. I did what you posted and it works.

    Kind regards,

  9. #9
    ipisors is offline Access Developer
    Windows XP Access 2007
    Join Date
    Sep 2013
    Posts
    119
    Glad you got it working to your satisfaction, good luck with the rest of the project.

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

Similar Threads

  1. Replies: 17
    Last Post: 04-07-2014, 07:48 PM
  2. How do I loop a query?
    By Baldeagle in forum Access
    Replies: 11
    Last Post: 08-13-2013, 02:23 AM
  3. Replies: 3
    Last Post: 03-10-2013, 07:04 AM
  4. To Loop, To Query or something else
    By Perdo123 in forum Access
    Replies: 12
    Last Post: 03-02-2012, 02:51 AM
  5. Dynamic Form, Dynamic labels/ captions?
    By JFo in forum Programming
    Replies: 15
    Last Post: 10-12-2011, 08:33 PM

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