CreateControl Help (1 Viewer)

riti90

Registered User.
Local time
Today, 03:44
Joined
Dec 20, 2017
Messages
44
Hi Everyone,

I'm trying to built up a Form where I can create Controls for other forms.

Now, I managed to do this but I cannot figure out how to change the "Control Source" or the Control that I created.

Can You please help me with that?

Here's the Code of Command Click And a sample Database
Code:
    Dim frm As Form
    Dim ctl As Control
    Dim FrmName As String
    Dim CtlName As String
    Dim CtlType As Long
    Dim CtlSource As String
    
    
    FrmName = Me.cboForms.Value
    CtlName = Me.txtFrmFieldName
    CtlSource = Me.cboTableFields
    DoCmd.OpenForm FormName:=FrmName, View:=acDesign
    Set frm = Forms(FrmName)
    CtlType = Me.txtCtlType.Value
    Set ctl = CreateControl(FormName:=FrmName, ControlType:=CtlType, _
        Left:=1440, Top:=2160, Width:=2880, Height:=288)
    ctl.Name = CtlName
    
' Switch to form view
    [COLOR="red"]Forms(FrmName).Controls(ctl).ControlSource = CtlSource[/COLOR]
    'RunCommand acCmdFormView
' Or save the form, close and reopen it
    DoCmd.Close ObjectType:=acForm, ObjectName:=FrmName, Save:=acSaveYes
    DoCmd.OpenForm FormName:=FrmName, View:=acNormal
    'Forms(strForm).Controls(strCtl).Value = "Hello World"
 

Attachments

  • TestDatabase.accdb
    672 KB · Views: 96

Ranman256

Well-known member
Local time
Yesterday, 22:44
Joined
Apr 9, 2015
Messages
4,337
why wouldnt the forms and controls already have been built?
why build on the fly?
 

riti90

Registered User.
Local time
Today, 03:44
Joined
Dec 20, 2017
Messages
44
why wouldnt the forms and controls already have been built?
why build on the fly?

Because later on during the work we may need to add an extra field or control so we can do it via code...
because we're not supposed to make changes and the database will be locked
 

JHB

Have been here a while
Local time
Today, 04:44
Joined
Jun 17, 2012
Messages
7,732
You specify the control source when you create the control.
Code:
    Set ctl = CreateControl(FormName:=FrmName, ControlType:=CtlType, [B][COLOR=Red]columnname:=CtlSource[/COLOR][/B], _
        Left:=1440, Top:=2160, Width:=2880, Height:=288)
But the source of control must of course be included in the field list for the form, (the form's record source either a table or a query)!
Therefore I think you first have to choose a form and then find the fields there are available.
Because later on during the work we may need to add an extra field or control so we can do it via code...
because we're not supposed to make changes and the database will be locked
I'm not sure you are able to make changes when the database is locked?
 

Attachments

  • TestDatabase3.accdb
    568 KB · Views: 82

riti90

Registered User.
Local time
Today, 03:44
Joined
Dec 20, 2017
Messages
44
Thank You JHB,

That's a great Help.

When the database is locked I haven't tried it but I thing it should be fine because it opens the form in Design View to make the changes.

But that's another step of the process for me.

Until then we'll see,
Thanks a Lot . :)
 

missinglinq

AWF VIP
Local time
Yesterday, 22:44
Joined
Jun 20, 2003
Messages
6,423
...Because later on during the work we may need to add an extra field or control so we can do it via code...
because we're not supposed to make changes
...

So someone, presumably management, doesn't want you, the developer, to be able to make changes...but you want to allow yourself (and possibly all end users) to do so?

This kind of raises an ethical question, doesn't it?

Linq ;0)>
 

riti90

Registered User.
Local time
Today, 03:44
Joined
Dec 20, 2017
Messages
44
So someone, presumably management, doesn't want you, the developer, to be able to make changes...but you want to allow yourself (and possibly all end users) to do so?

This kind of raises an ethical question, doesn't it?

Linq ;0)>

That's not the point really. The management doesn't want the back end users to make any changes because of policy restrictions during the project work.
But all projects are different from each other so someone sometimes may need to add an extra Field or Command.
That's all I could think of for the moment.
I'm sure it may be another much better solution for that but I've got a bit of a limited time for that really.

:rolleyes: :rolleyes:
 

missinglinq

AWF VIP
Local time
Yesterday, 22:44
Joined
Jun 20, 2003
Messages
6,423
...management doesn't want the back end users to make any changes because of policy restrictions during the project work...

...someone sometimes may need to add an extra Field or Command...

You're saying the same thing! Management doesn't want back end users to make any changes...and you're trying to facilitate them being able to do just that! And as most people here will tell, allowing end users to make design changes to a database is very, very dangerous!

Linq ;0)>
 

Users who are viewing this thread

Top Bottom