variable Recordset

Lanason

Registered User.
Local time
Today, 08:52
Joined
Sep 12, 2003
Messages
258
Final stupid question on my nested loop

How do I put a variable into the form name to replace frmABC (as I get this from my data)
myrec2 = Forms!?????.Recordset
 
normally, you have a permanent form name for the recordset.
frmClients, show client recordset
frmCustomers ,show customers,
BUT if like the above , the form has the same datafields ,you can swap recordsets
Code:
docmd.OpenForm "myForm"
forms!myForm.Recordsource = "qsClients"
 
The long version for referring to a form's recordset is:

Forms.Item(FormName).Recordset

You can replace FormName with either a string inside quotes or with a variable.

If the form isn't open you'll receive an error.

Basically, the ! in Forms!FormName.Recordset tells Access to pull the default property for whatever it follows. With the Forms collection, that's Item. The rest just tells it that the Item is named FormName and to access the Recordset property.
 
Last edited:
Following on from the last response, you can test if the form is open using:
Code:
SysCmd(acSysCmdGetObjectState, acForm, "FormName")

This returns 0 if closed and rather oddly 1 (not -1) if true

So you could add this code to avoid the error:
Code:
If SysCmd(acSysCmdGetObjectState, acForm, "FormName")=0 Then DoCmd.OpenForm "FormName"
 
Actually, I find it easier to test via CurrentProject.AllForms("FormName").IsLoaded. It returns TRUE of the form is open (in ANY view) and FALSE otherwise.
 

Users who are viewing this thread

Back
Top Bottom