By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,245 Members | 884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,245 IT Pros & Developers. It's quick & easy.

Creating forn controls with code

P: n/a
With excel VBA, I can do :

Dim ctrGo as commandbutton
Set ctrGo = me.controls.add("Go")

which would create a button on the form.

Can I do with with Access?
When I try it, first of all the is not a method "Add" available. when I run
it, it fails at this line.

I would like to create a button for every record in a query. The query
contains only unique room numbers for an individual job. I would like the
form to give the user the options of viewing each room separately by
clicking it button.

Is this possible.
Jul 23 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Sun, 23 Jul 2006 09:12:25 -0700, "John T Ingato" <jt******@hotmail.comwrote:
>With excel VBA, I can do :

Dim ctrGo as commandbutton
Set ctrGo = me.controls.add("Go")

which would create a button on the form.

Can I do with with Access?
When I try it, first of all the is not a method "Add" available. when I run
it, it fails at this line.

I would like to create a button for every record in a query. The query
contains only unique room numbers for an individual job. I would like the
form to give the user the options of viewing each room separately by
clicking it button.

Is this possible.
You can create buttons in code using the CreateControl function. See online help for doing this. Note that using this
can be somewhat timeconsuming, since the form must open in Design view (hence you cannot use the compiled .mde format),
and your controls are not saved unless you explicitly save them.

Most developers use a Continuous Form to do this. Are you displaying your query via a form? If so, you can set your
form's Default View to Continuous, and add a single button, with code like this in the Click Event:

Sub YourButton_Click()
'/<sample code here>
DoCmd.OpenForm "Some_Other_Form",,,"Some_ID_Field=" & Me!ID

End Sub

Access will generate a button for each "row" in your continuous form, and when the user clicks it Access will correctly
pickup the ID as needed.

The online help topic may assist you; it's available from VBA help, so open your VB Edit window and click Help, then
search on CreateControl.

'///////// example code from MS Access 2003 online help

Sub NewControls()
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 = 100
intDataX = 1000
intDataY = 100
' Create unbound default-size text box in detail section.
Set ctlText = CreateControl(frm.Name, acTextBox, , "", "", _
intDataX, intDataY)
' Create child label control for text box.
Set ctlLabel = CreateControl(frm.Name, acLabel, , _
ctlText.Name, "NewLabel", intLabelX, intLabelY)
' Restore form.
DoCmd.Restore
End Sub

>
Scott McDaniel
scott@takemeout_infotrakker.com
www.infotrakker.com
Jul 23 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.