3

I have a form with a subform, and I want the subform record source that populates the subform to change depending on the inputs on some combo boxes in the main form.

Using VBA, I have built a function that generates the SQL statement I want to populate the subform with. I know this works because I have tested it with the msgbox and it gives me the SQL statement I want. The sql statement uses an aggregate function so the resulting table has a different structure than the table it is querying from.

The code I use to change the subform record source is:

me![subformname subform].form.recordsource=myfunction()

This has worked for me in the past, but does not work here, I simply get "#Name?" in the subform on my form.

When I open the subform separately I get "#Name?" but when I open the Recourd Source and run the query from the record source I get a value so I am confused.

Any ideas?

5
  • Does your new RecordSource contain all of the fields that your subform needs? For example, if you have a Textbox with a ControlSource of Street_Address is there a column in your new RecordSource that says Street_Address. Commented Jun 15, 2015 at 18:31
  • 1
    Have you tried: Forms!frmMyMainFormName.frmMySubName.Form.RecordSource = myfunction() Commented Jun 15, 2015 at 18:32
  • I think my problem is that I am trying to change the structure of the subform when I change the Record Source. I originally have two columns, location, and population, and after I change the Recourd source I have an aggregate function that sums the population in the locations I want so the new table has only the column "number of people in locations" Commented Jun 15, 2015 at 18:50
  • So your RecordSource was missing a needed ControlSource? Commented Jun 15, 2015 at 19:09
  • Make sure your query returns the same names of fields to be used in your subform textboxes -- and that you have the correct number of fields. If old RS produced list of locations and counts, the aggregate is OK as long as you keep location and 'count' fields. Commented Jun 15, 2015 at 19:12

3 Answers 3

0

Try setting up two subforms - one for each format that you need - and when you swap the underlying recordsets also swap or hide/unhide the relevant subform. Then you can set the controlsource for the relevant controls to the fields that are actually available in the current recordset.

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

Comments

0

I got it to work by changing the structure of the subform I was changing so that the structure stayed the same when I altered the recordsource. The lesson is, you can change the recordsource in VBA, but you can't change the structure of the subform.

Comments

0

My problem occurred because I used an alias in my subform record source SELECT query. When I replaced the alias table names with the actual database table names in my VBA code, the .recordsource = strSQL01 statement worked and the data appeared in my subform.

Comments

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.