Results 1 to 9 of 9
  1. #1
    mystifier is offline Novice
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Nov 2010
    Posts
    24

    Update Subform Recordsource

    There are lots of threads on subforms and I have seen http://www.mvps.org/access/forms/frm0031.htm as a result, but frustratingly, I can't get something really simple to work.



    If I have a form 'frmAdHoc' with a subform 'subAdHoc', a textbox 'txtSQL' and a button 'cmdApply'

    So I want something like:

    Private Sub cmdApply_Click()
    Me!subAdHoc.Form.RecordSource = Me!txtSQL.Text
    Me!subAdHoc.Form.Requery
    End Sub

    (which returns: You can't reference a property or method for a control unless the control has the focus).

    How hard can it be
    Last edited by mystifier; 11-14-2010 at 07:21 AM.

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Use the .Value property instead of the .Text property.
    Me!subAdHoc.Form.RecordSource = Me!txtSQL.Value
    ...instead of...
    Me!subAdHoc.Form.RecordSource = Me!txtSQL.Text
    FYI:
    Since the .Value property is the default property of a control
    Me!txtSQL
    ...is the same as...
    Me!txtSQL.Value
    .
    Another thing: you get an automatic Requery when you change the RecordSource so:
    Me!subAdHoc.Form.Requery
    ...is not necessary and in fact is redundant.





  3. #3
    mystifier is offline Novice
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Nov 2010
    Posts
    24
    Thanks RuralGuy,

    Now I have a more fundamental:

    If I have a qryCustomer as 'Select * From Customer' and I build a (sub)form shown as a datagrid, it creates textboxes bound to each field.

    If I change the recordsource to 'Select * From Customer Where Firstname = "John"'; everything is fine because it uses the same fields.

    If I change the recordsource to 'Select * From [Order]'; it falls over because there are no valid fields.

    So, any pointers on how to have a general purpose datagrid (in Access Runtime) where the Recordsource can be set to any desired query?

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Have you looked at just running a dynamic query rather than making it a SubForm?

  5. #5
    mystifier is offline Novice
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Nov 2010
    Posts
    24
    I am afraid that I am completely green RuralGuy.

    Is there a simple way to open a recordset along the lines:

    Private Sub cmdQuery_Click()
    qryAdHoc.RecordSource = "Select * From Customer"
    DoCmd.OpenQuery ("qryAdHoc")
    End Sub

    The idea of trying to use a subform was that the full process will be:
    - Click a button to programmatically create SQL and display it in datagrid.
    - If it seems to make sense, click a button to create an export file.

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Try:
    Code:
    Private Sub cmdQuery_Click()
       Dim MySQL As String
       MySQL = "Select * From Customer"
       DoCmd.OpenQuery MySQL
    End Sub

  7. #7
    mystifier is offline Novice
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Nov 2010
    Posts
    24
    I tried exactly that, but it reported Can't find the object 'select * from customer'

    Does there need to be a recordset in there somewhere? I also need to get hold of something to export with ExportWithFormatting.

  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Okay, I'm all wet here. I don't use this so I will have to take a stab at it. You will probably need to define a DAO.QueryDef and then open the query in order to do what you want dynamically for the user.

  9. #9
    mystifier is offline Novice
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Nov 2010
    Posts
    24
    Thanks for the pointers.

    I have the following which keeps on dropping the created QueryDef which is not ideal but at least gets me over the hurdle:


    Private Sub cmdQuery_Click()
    Dim sqlQuery As String
    Dim sqlDrop As String
    Dim qryDef As Object

    sqlDrop = "Drop Table qryAdHoc"
    On Error Resume Next
    CurrentDb.Execute sqlDrop, dbFailOnError

    sqlQuery = Me!txtSQL.Value
    Set qryDef = CurrentDb.CreateQueryDef("qryAdHoc", sqlQuery)
    DoCmd.OpenQuery "qryAdHoc"
    End Sub

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

Similar Threads

  1. Update field in table after update on a subform
    By jpkeller55 in forum Queries
    Replies: 3
    Last Post: 09-30-2010, 08:02 PM
  2. update field from one subform to another
    By bluezidane in forum Programming
    Replies: 3
    Last Post: 08-20-2010, 07:54 AM
  3. Replies: 3
    Last Post: 02-10-2010, 07:29 AM
  4. update recordsource of subform
    By AndyKim in forum Forms
    Replies: 1
    Last Post: 06-16-2009, 09:15 AM
  5. Form/Subform - Update control help!
    By f_ali in forum Forms
    Replies: 2
    Last Post: 02-26-2006, 12:59 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