Hi
Not sure if anyone can shed any light on why I have this problem. I have a function that creates a form dynamically to enable the end use to edit the table (or query) contents.
For example, if I have a table called Activities. By calling this function it will create a form with the fields contained within this table. This form can then be viewed through a form (as a sub-form) in datasheet mode enabling the user to edit the details.
This approach works really well to view queries as well, especially cross tab queries where the column heading could be variable.
Anyway, I wrote it, tested it and it works fine as long as you are not using Access Runtime!
When it first failed when run under Runtime I thought it must have been the Application.CreateForm method, but it creates the forms fine, saves them, it is the Docmd.Rename method that does not work.
Here is the code: -
You call this function as so: -
The frmName returned is whatever Application.CreateForm has chosen. This is normally Form1 (or Form2 if Form1 exists etc).
I then rename the form to something more valid: -
No error is generated, it just does not do it when in RunTime. The error is generated when I try and reference the form: -
As the rename has failed, the form frmAdminDataSetModificationSub does not exist and so setting the subform.sourceObject to this is invalid.
Has anyone got any ideas as to why DoCmd.Rename does not work until RunTime?
Not sure if anyone can shed any light on why I have this problem. I have a function that creates a form dynamically to enable the end use to edit the table (or query) contents.
For example, if I have a table called Activities. By calling this function it will create a form with the fields contained within this table. This form can then be viewed through a form (as a sub-form) in datasheet mode enabling the user to edit the details.
This approach works really well to view queries as well, especially cross tab queries where the column heading could be variable.
Anyway, I wrote it, tested it and it works fine as long as you are not using Access Runtime!
When it first failed when run under Runtime I thought it must have been the Application.CreateForm method, but it creates the forms fine, saves them, it is the Docmd.Rename method that does not work.
Here is the code: -
Code:
Public Function CreateDataModificationForm(ByVal ObjectName As String, ByVal ObjectType As String) As String
Dim Tbl As TableDef
Dim fld As Field
Dim ctl As Control
Dim frm As Form
Set frm = CreateForm(, "__TemplateDataSetForm")
'This template form is a blank form that is set to Datasheet Only view
frm.RecordSource = ObjectName
If ObjectType = "Table" Then
For i = 0 To CurrentDb.TableDefs(ObjectName).Fields.Count - 1
Set ctl = CreateControl(frm.Name, acTextBox, acDetail, , CurrentDb.TableDefs(ObjectName).Fields(i).Name)
ctl.Name = CurrentDb.TableDefs(ObjectName).Fields(i).Name
Next i
Else
For i = 0 To CurrentDb.QueryDefs(ObjectName).Fields.Count - 1
Set ctl = CreateControl(frm.Name, acTextBox, acDetail, , CurrentDb.QueryDefs(ObjectName).Fields(i).Name)
ctl.Name = CurrentDb.QueryDefs(ObjectName).Fields(i).Name
Next i
End If
CreateDataModificationForm = frm.Name
DoCmd.Close acForm, frm.Name, acSaveYes
End Function
You call this function as so: -
Code:
frmName = CreateDataModificationForm(TableName, "Table")
The frmName returned is whatever Application.CreateForm has chosen. This is normally Form1 (or Form2 if Form1 exists etc).
I then rename the form to something more valid: -
Code:
DoCmd.Rename "frmAdminDataSetModificationSub", acForm, frmName
No error is generated, it just does not do it when in RunTime. The error is generated when I try and reference the form: -
Code:
Sub1.SourceObject = "frmAdminDataSetModificationSub"
As the rename has failed, the form frmAdminDataSetModificationSub does not exist and so setting the subform.sourceObject to this is invalid.
Has anyone got any ideas as to why DoCmd.Rename does not work until RunTime?