CreateControl (1 Viewer)

homer2002

Registered User.
Local time
Today, 14:11
Joined
Aug 27, 2002
Messages
152
CreateControl - Trying to create somthing useful

Hi all

Is it possible to create a complete form (or edit a form) to create a complete all singing all dancing form.

Say for example I wanted to create a form with a command button that executes a simple function


for example
__________________________________________

Create a form and name it frmMyForm
Add a command button
add a line of code attached to the On_click event - Call Myfunction)

Create the function

private function MyFunction()
Msgbox "I made this"
endsub


save the form
open the form

__________________________________________

Alternativly if I had a form that already exists can I Add new controls to that form?

if for example I had a form with all the code in it that is needed, can I add new controls to this form (like a text box name txtMyTextbox)

I am just experementing really, this all came about when I learnt to fake control arrays so I would like to be able to add a series of controls to a form like -

txtMyTextbox1 & txtMyTextbox2 & txtMyTextbox3

Any help would be appreciated :)
 

fernin8r

Registered User.
Local time
Today, 14:11
Joined
May 28, 2002
Messages
142
Sorry to disappoint, homer2002, but i don't have an answer.

I was actually wondering the exact same thing. Does anyone know if you can create a new control on a currently open form.

I would like the user to be able to click a cmd_button that will add another cmd_button to the same form.

Any help would be awesome.

Thanks...mike
 

Mile-O

Back once again...
Local time
Today, 14:11
Joined
Dec 10, 2002
Messages
11,316
Not that I've done it but I believe you can open a form in DesignView and Hidden - where you can then add a control, save it, whatever...


...though, why would you want to?
 

Cosmos75

Registered User.
Local time
Today, 08:11
Joined
Apr 22, 2002
Messages
1,281
I have tried to do the same thing. But is isn't working (see line in RED)?
:( :confused:
Code:
Private Sub cmdAddLbl_Click()
    Dim myFrm As Form
    Dim ctlLabel As Control

    Set myFrm = Me
    frmName = myFrm.Name

    Dim Top1, Left1, Height1 as Integer
    [color=green]' Set positioning values for new label.[/color]
    Top1 = Me.lblCombi1.Top
    Height1 = Me.lblCombi1.Height
    Left1 = 0

    Dim fLabel As Control
    Dim CountLbl As Integer
    CountLbl = 0

    For Each fLabel In myFrm
        If Left(fLabel.Name, 8) = "lblCombi" Then
            [color=green]'Count labels that start with "lblCombi"[/color]
            CountLbl = CountLbl + 1
            [color=green]'Figure out position of last label[/color]
            Left1 = Me.Controls("lblCombi" & Countlbl).Left
        End If
    Next

    [color=green]'Calculate Left position of the new label[/color]
    LeftNew = Left1 + Me.Controls("lblCombi" & CountLbl).Width

    DoCmd.Close acForm, myFrm.Name, acSaveNo

    DoCmd.OpenForm frmName, acDesign
    
    [color=green]' Create unbound default-size text box in Header section.[/color]
    ctlName = "lblCombi" & CountLbl + 1
    [color=red][b]Set ctlLabel = CreateControl(frmName, acLabel, acHeader,_
     "", "", LeftNew, Top1)[/b][/color]
    ctlLabel.Name = ctlName

    [color=green]' Restore form.[/color]
    DoCmd.Restore

End Sub
What am I missing?

Edit: THe run-time error I get is # 29054 - Microsoft Access can't add, rename, or delete the control(s) you requested.
 
Last edited:

Oldsoftboss

AWF VIP
Local time
Today, 23:11
Joined
Oct 28, 2001
Messages
2,499
Do I have too much time on my hands...?
Well I did come off my bike and haven't worked since the end of Nov

:D

Dave
 

Attachments

  • create controls.zip
    52.3 KB · Views: 704

Oldsoftboss

AWF VIP
Local time
Today, 23:11
Joined
Oct 28, 2001
Messages
2,499
And this I created because I was sick of creating a form and then going though and setting the forms properties to my reqular choices.

Dave
 

Attachments

  • form template wizard.zip
    47.2 KB · Views: 510

Oldsoftboss

AWF VIP
Local time
Today, 23:11
Joined
Oct 28, 2001
Messages
2,499
...though, why would you want to?

Tend to agree with Mile. My excuse was bordem.

Dave
 

Cosmos75

Registered User.
Local time
Today, 08:11
Joined
Apr 22, 2002
Messages
1,281
OldSoftBoss,

Thank you for your wonderful examples.

You code is similiar to mine. Read somewhere else that it may not be working because the form module is still loaded and that I need to do this from another module.

Well, I copied the code from the existing form over to a new form. I now have code that closes the form that I want to add the control and then opens the new form.
Code:
    DoCmd.Close acForm, Me.Name, acSaveYes
    DoCmd.OpenForm "NewForm"
The original code now runs on the OnOpen event of the new form.

Doesn't work when I do that. However!, when I open up the new form all by itself without having existing form open, it works!?
:confused:

Is the the existing form module is still open when I close the from? If so, how do I close the form module?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:11
Joined
Feb 19, 2002
Messages
43,233
Nice piece of work Dave :) But, as I have said many times, I've written my million lines of code and I don't write any that I don't have to. Here's a copy of a presentation I gave this week to the local Access users group. I have to post the sample db separately because together they are too big. The presentation is just the major bullets but hopefully it's enough to get you to the correct menu items to set up your own template forms.

Part 1:
 

Attachments

  • accesstipsandtricks.zip
    41.5 KB · Views: 518

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:11
Joined
Feb 19, 2002
Messages
43,233
Part 2:
 

Attachments

  • autoformatformsa2k.zip
    86.5 KB · Views: 461

Cosmos75

Registered User.
Local time
Today, 08:11
Joined
Apr 22, 2002
Messages
1,281
Thank you all for you wonderful examples.

I can't get it to work the way I want it to.

I have two forms
- frm1
- frmCreateControl

frm1 is the form I am tryin to add a label to.
frmCreateControl has code on the OnOpen event to add the control.

If I try to call frmCreateControl from frm1, the code fails.
If I just open frmCreateControl, it works.
:confused:

Any ideas why?
 

Cosmos75

Registered User.
Local time
Today, 08:11
Joined
Apr 22, 2002
Messages
1,281
Access 97 version
 

Attachments

  • createcontrol97.zip
    17.5 KB · Views: 341

Cosmos75

Registered User.
Local time
Today, 08:11
Joined
Apr 22, 2002
Messages
1,281
Access 2000 version
 

Attachments

  • createcontrol2000.zip
    29.7 KB · Views: 497

keirnus

Registered User.
Local time
Today, 22:11
Joined
Aug 12, 2008
Messages
99
I'm so into this thread now.

Q: How to create a customized control (e.g. checkbox) in a form?

I was able to add a while ago, but now, I can't.
I really dunno what I did.
I keep on having this error message.

Code:
Run-time error '29054'
 
Microsoft Office Access can't add, rename, or delete the control(s) you requested.

What's the reason behind this message?
How to add a control in a form programatically?
 

Oldsoftboss

AWF VIP
Local time
Today, 23:11
Joined
Oct 28, 2001
Messages
2,499
Just remember the form must be in design mode, even when adding a control programatically. Open it hidden, in design mode, add the control then save and close

HTH

Dave
 

Users who are viewing this thread

Top Bottom