Createform, no FormHeader

  • Thread starter Thread starter Doug MacDonald
  • Start date Start date
D

Doug MacDonald

I can create a form in VB using Set frm = CreateForm and
add a control to the Detail section. According to a
knowledgebase article(ACC2000: Using CreateForm() and
CreateReport() Functions) I should be able to access the
form header using Section(1) such as frm.Section
(1).Height = 1.5 I get the error message " The section
number you entered is invalid". frm.Section(0).Height
= 1.5 works fine for the Detatil section.

How do I get the form Header to appear on the form?

Thanks,

Doug
 
Doug MacDonald said:
I can create a form in VB using Set frm = CreateForm and
add a control to the Detail section. According to a
knowledgebase article(ACC2000: Using CreateForm() and
CreateReport() Functions) I should be able to access the
form header using Section(1) such as frm.Section
(1).Height = 1.5 I get the error message " The section
number you entered is invalid". frm.Section(0).Height
= 1.5 works fine for the Detatil section.

How do I get the form Header to appear on the form?

The statement

RunCommand acCmdFormHdrFtr

creates or removes the form header and footer sections. They don't
exist until you create them.
 
Dirk,

Here's the code. I put it in a command button click
subroutine. The CreateForm and CreateControls is right out
of Access Help. I added code at the end to bind the
textbox, set the default view to Continuous Form, set the
height of the Detail section, and make the window
smaller. I tried adding the Runcommand acCmdFormHdrFtr in
different places but I get an error saying "The command or
action'FormHdrFtr' isn't available now.

I'm not sure the DoCmd is proper as the Help file says it
could also be a Application object. I have it commented
out at the end of the code below.

The code as below creates the form, puts the label and
textbox in the Detail section and opens the form as a
continuous form showing the OrderID's.

It works on my system but I can't get the Header to
appear. I would like to put the label in the header.

It 1:46AM here on the East Coast. I've got to get to
bed. I'll look for your reply later this morning.

Thanks,

Doug
************* CreateForm Code Below ***************
Private Sub cmdCreateControls_Click()
On Error GoTo Err_cmdCreateControls_Click

Dim stDocName As String
Dim stLinkCriteria As String

' NewControls
' *********** Create Controls ***********************
Dim frm As Form
Dim ctlLabel As Control, ctlText As Control
Dim intDataX As Integer, intDataY As Integer
Dim intLabelX As Integer, intLabelY As Integer


' Create new form with Orders table as its record
source.
Set frm = CreateForm()
frm.RecordSource = "Orders"
' Set positioning values for new controls.
intLabelX = 100
intLabelY = 20
intDataX = 1000
intDataY = 20
' Create unbound default-size text box in detail
section.
Set ctlText = CreateControl(frm.Name, acTextBox,
acDetail, "", "", _
intDataX, intDataY)
' Create child label control for text box.
Set ctlLabel = CreateControl(frm.Name, acLabel, , _
ctlText.Name, "NewLabel", intLabelX, intLabelY)
' Restore form.
DoCmd.Restore
' *******************************************
frm.text0.ControlSource = "OrderID"
frm.[DefaultView] = 1
frm.Section(0).Height = 1.5
frm.Caption = "frmTest1"
DoCmd.MoveSize , , 7000
stDocName = "Form1"
DoCmd.OpenForm stDocName, , , stLinkCriteria
' DoCmd.RunCommand acCmdFormHdrFtr

Exit_cmdCreateControls_Click:
Exit Sub

Err_cmdCreateControls_Click:
MsgBox Err.Description
Resume Exit_cmdCreateControls_Click

End Sub
 
Continuing to work on how to create controls in
the Header of a form, the following is somewhat
of a concern.

The Access Object Browser shows the CreateControl
Method with [Section As AcSection = acDetail]
almost indicating that "acDetail" is the only
valid parameter. The Help file shows a slightly
different format for CreateControl. I have them
both listed below.

***** Object Browser Format ***********
Function CreateControl(FormName As String,
ControlType As AcControlType,
[Section As AcSection = acDetail], [Parent], [ColumnName],
, [Top], [Width], [Height]) As Control

******Help File Format ****************
CreateControl(formname, controltype[,
section[, parent[, columnname[,
left[, top[, width[, height]]]]]]])
**************************************

I have several hours into this "Control in Header"
search effort now. I'm certainly learning a lot but
it sure is frustrating. Thanks for the support.

Doug​
 
Doug MacDonald said:
Dirk,

Here's the code. I put it in a command button click
subroutine. The CreateForm and CreateControls is right out
of Access Help. I added code at the end to bind the
textbox, set the default view to Continuous Form, set the
height of the Detail section, and make the window
smaller. I tried adding the Runcommand acCmdFormHdrFtr in
different places but I get an error saying "The command or
action'FormHdrFtr' isn't available now.

I'm not sure the DoCmd is proper as the Help file says it
could also be a Application object. I have it commented
out at the end of the code below.

The code as below creates the form, puts the label and
textbox in the Detail section and opens the form as a
continuous form showing the OrderID's.

It works on my system but I can't get the Header to
appear. I would like to put the label in the header.

It 1:46AM here on the East Coast. I've got to get to
bed. I'll look for your reply later this morning.

Thanks,

Doug
************* CreateForm Code Below ***************
Private Sub cmdCreateControls_Click()
On Error GoTo Err_cmdCreateControls_Click

Dim stDocName As String
Dim stLinkCriteria As String

' NewControls
' *********** Create Controls ***********************
Dim frm As Form
Dim ctlLabel As Control, ctlText As Control
Dim intDataX As Integer, intDataY As Integer
Dim intLabelX As Integer, intLabelY As Integer


' Create new form with Orders table as its record
source.
Set frm = CreateForm()
frm.RecordSource = "Orders"
' Set positioning values for new controls.
intLabelX = 100
intLabelY = 20
intDataX = 1000
intDataY = 20
' Create unbound default-size text box in detail
section.
Set ctlText = CreateControl(frm.Name, acTextBox,
acDetail, "", "", _
intDataX, intDataY)
' Create child label control for text box.
Set ctlLabel = CreateControl(frm.Name, acLabel, , _
ctlText.Name, "NewLabel", intLabelX, intLabelY)
' Restore form.
DoCmd.Restore
' *******************************************
frm.text0.ControlSource = "OrderID"
frm.[DefaultView] = 1
frm.Section(0).Height = 1.5
frm.Caption = "frmTest1"
DoCmd.MoveSize , , 7000
stDocName = "Form1"
DoCmd.OpenForm stDocName, , , stLinkCriteria
' DoCmd.RunCommand acCmdFormHdrFtr

Exit_cmdCreateControls_Click:
Exit Sub

Err_cmdCreateControls_Click:
MsgBox Err.Description
Resume Exit_cmdCreateControls_Click

End Sub

You were probably stepping through the code when "RunCommand
acCmdFormHdrFtr" gave you that error. If I leave all your code intact
except for the lines ...
stDocName = "Form1"
DoCmd.OpenForm stDocName, , , stLinkCriteria

which don't make sense to me in context, so it ends like this:

frm.Caption = "frmTest1"
DoCmd.MoveSize , , 7000
DoCmd.RunCommand acCmdFormHdrFtr

it works just fine so long as I'm not stepping through the code in the
VB window. Naturally, when the VB window has the focus, the command
RunCommand acCmdFormHdrFtr isn't available, since it want to act on the
current form window.

Were these lines ...
stDocName = "Form1"
DoCmd.OpenForm stDocName, , , stLinkCriteria

.... supposed to open the newly created form under the name "Form1"?
That won't work.

BTW, it doesn't really matter whether you write

DoCmd.RunCommand acCmdFormHdrFtr
or
Application.RunCommand acCmdFormHdrFtr
or just
RunCommand acCmdFormHdrFtr

The last two are equivalent, and it seems simpler to me to use the
Application method rather than the DoCmd method, but they all accomplish
the same thing.
 
Doug MacDonald said:
Continuing to work on how to create controls in
the Header of a form, the following is somewhat
of a concern.

The Access Object Browser shows the CreateControl
Method with [Section As AcSection = acDetail]
almost indicating that "acDetail" is the only
valid parameter. The Help file shows a slightly
different format for CreateControl. I have them
both listed below.

***** Object Browser Format ***********
Function CreateControl(FormName As String,
ControlType As AcControlType,
[Section As AcSection = acDetail], [Parent], [ColumnName],
, [Top], [Width], [Height]) As Control

******Help File Format ****************
CreateControl(formname, controltype[,
section[, parent[, columnname[,
left[, top[, width[, height]]]]]]])
**************************************

I have several hours into this "Control in Header"
search effort now. I'm certainly learning a lot but
it sure is frustrating. Thanks for the support.​


That format for the parameter specification -- [Section As AcSection =
acDetail] -- simply means that the Section parameter is optional, and if
you don't specify it the Detail section (acDetail) is assumed.​
 
Dirk,

It works now.

I must have been stepping through it as you suggest.

I moved the:
DoCmd.RunCommand acCmdFormHdrFtr
up to just before the CreateControl for the label.

I had to remove the Parent parameter in the CreateControl
for the label in order for acHeader to put the label in
the Header.

I still have to use:
DoCmd.OpenForm "Form1", , , stLinkCriteria
at the end to open the form as without this statement the
form remains in Design mode.

A funky thing does happen if I try to use the new Caption
name as follows in the OpenForm statement:
DoCmd.OpenForm "frmTest1", , , stLinkCriteria
I get two forms. One entitled frmTest1 and the other
entitled Form1. The one entitled frmTest1 has the label
still in the Detail section while the one entitled Form1
has the label in the Header but is still in Design mode.
It's not a problem, however, as I just use Form1.

The OpenForm statement was generated when I created the
command button. I don't know and can find no indication
of what the WHERE parameter stLinkCriteria is used for.
It appears to be always blank.

Thanks so much for your help. I may be coming back as I
would like to put a command button in the footer to close
the form and I'm not too sure how much help I will get
from the system.

The updated code follows:
--------------------------------
Private Sub cmdCreateControls_Click()
On Error GoTo Err_cmdCreateControls_Click

Dim stDocName As String
Dim stLinkCriteria As String

' NewControls
' *********** Create Controls ***********************
Dim frm As Form
Dim ctlLabel As Control, ctlText As Control
Dim intDataX As Integer, intDataY As Integer
Dim intLabelX As Integer, intLabelY As Integer


' Create new form with Orders table as its record
source.
Set frm = CreateForm()
frm.RecordSource = "Orders" 'imported from Nwind.mdb
' Set positioning values for new controls.
intLabelX = 100
intLabelY = 20
intDataX = 100
intDataY = 20
' Create unbound default-size text box in detail
section.
Set ctlText = CreateControl(frm.Name, acTextBox,
acDetail, "", "", _
intDataX, intDataY)
DoCmd.RunCommand acCmdFormHdrFtr
' Create child label control for text box.
Set ctlLabel = CreateControl(frm.Name, acLabel,
acHeader, _
"", "NewLabel", intLabelX, intLabelY)
' Restore form.
DoCmd.Restore
'
***********************************************************
********
frm.text0.ControlSource = "OrderID"
frm.[DefaultView] = 1
frm.Section(0).Height = 1.5
frm.Caption = "frmTest1"
DoCmd.MoveSize , , 7000
DoCmd.OpenForm "Form1", , , stLinkCriteria
' DoCmd.OpenForm "frmTest1", , , stLinkCriteria

Exit_cmdCreateControls_Click:
Exit Sub

Err_cmdCreateControls_Click:
MsgBox Err.Description
Resume Exit_cmdCreateControls_Click

End Sub


I do use the
-----Original Message-----
Doug MacDonald said:
Continuing to work on how to create controls in
the Header of a form, the following is somewhat
of a concern.

The Access Object Browser shows the CreateControl
Method with [Section As AcSection = acDetail]
almost indicating that "acDetail" is the only
valid parameter. The Help file shows a slightly
different format for CreateControl. I have them
both listed below.

***** Object Browser Format ***********
Function CreateControl(FormName As String,
ControlType As AcControlType,
[Section As AcSection = acDetail], [Parent], [ColumnName],
, [Top], [Width], [Height]) As Control

******Help File Format ****************
CreateControl(formname, controltype[,
section[, parent[, columnname[,
left[, top[, width[, height]]]]]]])
**************************************

I have several hours into this "Control in Header"
search effort now. I'm certainly learning a lot but
it sure is frustrating. Thanks for the support.​


That format for the parameter specification -- [Section As AcSection =
acDetail] -- simply means that the Section parameter is optional, and if
you don't specify it the Detail section (acDetail) is assumed.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)


.
 
(comments inline)

Doug MacDonald said:
Dirk,

It works now.

Actually, I don't think it's working as well as you think. More on this
below.
I had to remove the Parent parameter in the CreateControl
for the label in order for acHeader to put the label in
the Header.

Yes, you would. That's because a label in one section can't have a
parent control that is in another section.
I still have to use:
DoCmd.OpenForm "Form1", , , stLinkCriteria
at the end to open the form as without this statement the
form remains in Design mode.

But this is going to open an existing form named "Form1", not the form
you've just created -- unless the new form happens to be named Form1,
and that will only happen if there is no existing form with that name.

What you really want to do is drop the line
DoCmd.OpenForm "Form1", , , stLinkCriteria

and add the line

RunCommand acCmdFormView

when you're ready to display the form you've just created in form view.
A funky thing does happen if I try to use the new Caption
name as follows in the OpenForm statement:
DoCmd.OpenForm "frmTest1", , , stLinkCriteria
I get two forms. One entitled frmTest1 and the other
entitled Form1. The one entitled frmTest1 has the label
still in the Detail section while the one entitled Form1
has the label in the Header but is still in Design mode.
It's not a problem, however, as I just use Form1.

No matter what caption you assigned to the new form, the name of the
form was chosen by Access. That name would begin with "Form", with a
number tacked on the end to make a name that is not already in use.
Setting the name of the form to your own name isn't as simple as one
might expect. It seems you can't just say

frm.Name = "frmTest1" ' WRONG!

That raises an error. I suspect, but haven't tried it, that you must
save the form under its current, system-assigned name, and then use
DoCmd.Rename to rename it. However, if your purpose here is to create a
form dynamically and then throw it away after use, you wouldn't need to
do that.
The OpenForm statement was generated when I created the
command button. I don't know and can find no indication
of what the WHERE parameter stLinkCriteria is used for.
It appears to be always blank.

You use the command button wizard to create a button to open a form.
That code includes the option of specifying "link criteria" to open the
form pre-filtered to display only records that match certain criteria.
If you didn't chooe that option when you ran the wizard, the generated
code includes the definition of the stLinkCriteria variable and passes
it in the call to DoCmd.OpenForm, but never sets its value. For your
particular purpose (as I understand it), you don't need that.
Thanks so much for your help. I may be coming back as I
would like to put a command button in the footer to close
the form and I'm not too sure how much help I will get
from the system.

May I ask what it is you're actually trying to do here? You may be
making problems for yourself down the road. There's seldom a need for
dynamically creating forms in a working application. And I'd argue
strongly against modifying your VB project on the fly.
 
Dirk,

My idea behind creating form and creating controls is to
provide a very flexible way to present data to the users
of the database. I want to set up combo boxes and text
boxes on a search form that can be used to select the
fields to be presented and then build the form and
controls to match the fields selected based on the
SELECT query which I will build on the fly.

Based on the insight the users gain from these forms
they could then decide what kind of Reports they need.

The alternative I felt was to set up several template
forms and limit the search results to those formats.
That may be the way I end up doing it as I need code
in the search result forms and I can't see any way of
doing that when I create the form.

I did find a way of using a template with the CreateForm()
method however it appears to only use the form layout and
not any of the controls and/or events or modules built into
the template form.

Thanks,

Doug

The code I came up with for using a template form is
as follows:

Private Sub cmdCreateControls2_Click()
On Error GoTo Err_cmdCreateControls2_Click

' NewControls
' *********** Create Controls ***********************
Dim frm As Form
Dim ctlLabel As Control, ctlText As Control, ctlCmd As
Control
Dim intDataX As Integer, intDataY As Integer
Dim intLabelX As Integer, intLabelY As Integer
Dim intCmdX As Integer, intCmdY As Integer
Dim con As Container
Dim dbs As Database

Set dbs = CurrentDb
Set con = dbs.Containers("Forms")

' Create new form with Orders table as its record
source.
Set frm = CreateForm(, con.Documents!frmTemplate.Name)
' Set frm = CreateForm()
frm.RecordSource = "Orders"
' Set positioning values for new controls.
intLabelX = 100
intLabelY = 20
intDataX = 100
intDataY = 20
intCmdX = 4500
intCmdY = 20
' Create unbound default-size text box in detail
section.
Set ctlText = CreateControl(frm.Name, acTextBox,
acDetail, "", "", _
intDataX, intDataY)
' Create label control for text box.
Set ctlLabel = CreateControl(frm.Name, acLabel,
acHeader, _
"", "NewLabel", intLabelX, intLabelY)
' Create command button for Closing the form
Set ctlCmd = CreateControl(frm.Name, acCommandButton,
acFooter, _
"", "Close Form", intCmdX, intCmdY)
' Restore form.
DoCmd.Restore
'
***********************************************************
********
ctlText.ControlSource = "OrderID"
ctlCmd.Caption = "Close Form"
frm.[DefaultView] = 1
frm.Section(0).Height = 1.5
frm.Caption = "frmTest1"
DoCmd.MoveSize , , 7000
DoCmd.RunCommand acCmdFormView

Exit_cmdCreateControls2_Click:
Exit Sub

Err_cmdCreateControls2_Click:
MsgBox Err.Description
Resume Exit_cmdCreateControls2_Click

End Sub
 
Doug MacDonald said:
Dirk,

My idea behind creating form and creating controls is to
provide a very flexible way to present data to the users
of the database. I want to set up combo boxes and text
boxes on a search form that can be used to select the
fields to be presented and then build the form and
controls to match the fields selected based on the
SELECT query which I will build on the fly.

Based on the insight the users gain from these forms
they could then decide what kind of Reports they need.
The alternative I felt was to set up several template
forms and limit the search results to those formats.
That may be the way I end up doing it as I need code
in the search result forms and I can't see any way of
doing that when I create the form.

Although I haven't had occasion to do this myself, I gather that one
fairly common approach to this problem is to create a form that already
contains all the controls you expect ever to need, but have them all
initially be invisible. Then at run time you identify the controls you
need, position them on the form, set other properties such as
controlsources, and then make them visible. All the control that aren't
needed this time simply remain hidden. Depending on the nature of the
code required, you may be able to create event procedures at design
time, and hook them up at run time.

Yet another approach to the code problem might be to have the necessary
code in private function procedures defined within the form's class
module. Then at run time you could set various controls' event
properties to function expressions; e.g., "=HandleAfterUpdate()".
Within the functions, you could use Me.Active control if needed to get a
reference to the control that raised the event. You wouldn't be able to
get access to the arguments that would be passed to an event procedure,
though.
I did find a way of using a template with the CreateForm()
method however it appears to only use the form layout and
not any of the controls and/or events or modules built into
the template form.

Yes, that's what happens with form templates. However, maybe you could
just copy the template form and modify the copy.

You should be able to insert code in the module of a form you've
created. After all, the form and control wizards do it. However,
you'll not that the wizards themselves aren't in the database they are
modifying, but rather are in external databases. I still don't much
care for the idea of modifying code dynamically in a running database,
though.
 
Dirk,
Thanks so much for the ideas. I'll probably be back in
touch via the newsgroup.

Thanks,

Doug
 
Back
Top