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

Creating a new label using code (VBA)

P: 59
I asked a question before on how to duplicate a label.. and everyone said there isn't such option.

but I know there is an option to create a new one.
this is what I'm trying...

Dim b As New Label
b.Caption = "test"
b.BackColor = vbRed
b.Left = "50000"
b.Top = "4000"
b.Width = "5000"
b.Height = "5000"
b.Visible = True


any ideas why this doesn't work ?
I search on google, and saw something like
Me.Controls.add (b)

but doesn't work either...
Aug 31 '07 #1
Share this Question
Share on Google+
3 Replies


Rabbit
Expert Mod 10K+
P: 12,389
As far as I know, it only works in design view. You can't do it on an object that's already loaded.
Aug 31 '07 #2

ADezii
Expert 5K+
P: 8,669
I asked a question before on how to duplicate a label.. and everyone said there isn't such option.

but I know there is an option to create a new one.
this is what I'm trying...

Dim b As New Label
b.Caption = "test"
b.BackColor = vbRed
b.Left = "50000"
b.Top = "4000"
b.Width = "5000"
b.Height = "5000"
b.Visible = True


any ideas why this doesn't work ?
I search on google, and saw something like
Me.Controls.add (b)

but doesn't work either...
zivon, here is how it is done. As Rabbit pointed out, the process of Control Creation must be done in Design View of the Form. Afterwards, however, the results of your efforts can be seen by opening the Form.
Expand|Select|Wrap|Line Numbers
  1. Dim ctl As Control, intLeft As Integer, intTop As Integer
  2. Dim intWidth As Integer, intHeight As Integer, frm As Form
  3. Dim frmctl As Control
  4.  
  5. 'Must be opened in Disign Mode
  6. DoCmd.OpenForm "frmCreateControl", acDesign, , , acFormEdit, acWindowNormal
  7.  
  8. intLeft = 1440      '1 inch from Left border
  9. intTop = 1440       '1 inch from Top of Form
  10. intWidth = 2160     '1.5 inches in Width
  11. intHeight = 360     '1/4 of an inch in Height
  12.  
  13. 'Create an Unbound Label in the Detail Section of frmCreateControl with the specified dimensions
  14. Set ctl = CreateControl("frmCreateControl", acLabel, acDetail, , , intLeft, intTop, intWidth, intHeight)
  15.  
  16. 'Let's view your creation
  17. DoCmd.OpenForm "frmCreateControl", acNormal, , , acFormEdit, acWindowNormal
  18.  
  19. 'Format our newly created Label
  20. For Each frmctl In Forms!frmCreateControl.Controls
  21.   If frmctl.ControlType = acLabel Then
  22.     frmctl.SpecialEffect = 2    'Sunken
  23.     frmctl.BackStyle = 1        'Solid
  24.     frmctl.BackColor = vbRed
  25.   End If
  26. Next
NOTE: If you keep executing the code, you will keep creating the exact Label, sequentially numbered, and stacked on top of each other at exactly the same location on the Form. This is the reasoning behind the Control Loop.
Aug 31 '07 #3

ADezii
Expert 5K+
P: 8,669
zivon, here is how it is done. As Rabbit pointed out, the process of Control Creation must be done in Design View of the Form. Afterwards, however, the results of your efforts can be seen by opening the Form.
Expand|Select|Wrap|Line Numbers
  1. Dim ctl As Control, intLeft As Integer, intTop As Integer
  2. Dim intWidth As Integer, intHeight As Integer, frm As Form
  3. Dim frmctl As Control
  4.  
  5. 'Must be opened in Disign Mode
  6. DoCmd.OpenForm "frmCreateControl", acDesign, , , acFormEdit, acWindowNormal
  7.  
  8. intLeft = 1440      '1 inch from Left border
  9. intTop = 1440       '1 inch from Top of Form
  10. intWidth = 2160     '1.5 inches in Width
  11. intHeight = 360     '1/4 of an inch in Height
  12.  
  13. 'Create an Unbound Label in the Detail Section of frmCreateControl with the specified dimensions
  14. Set ctl = CreateControl("frmCreateControl", acLabel, acDetail, , , intLeft, intTop, intWidth, intHeight)
  15.  
  16. 'Let's view your creation
  17. DoCmd.OpenForm "frmCreateControl", acNormal, , , acFormEdit, acWindowNormal
  18.  
  19. 'Format our newly created Label
  20. For Each frmctl In Forms!frmCreateControl.Controls
  21.   If frmctl.ControlType = acLabel Then
  22.     frmctl.SpecialEffect = 2    'Sunken
  23.     frmctl.BackStyle = 1        'Solid
  24.     frmctl.BackColor = vbRed
  25.   End If
  26. Next
NOTE: If you keep executing the code, you will keep creating the exact Label, sequentially numbered, and stacked on top of each other at exactly the same location on the Form. This is the reasoning behind the Control Loop.
I realized last night that the Properties assigned to the newly created Label would not persist. I modified the code, and this is the corrected Version - use this:
Expand|Select|Wrap|Line Numbers
  1. Dim ctl As Control, intLeft As Integer, intTop As Integer
  2. Dim intWidth As Integer, intHeight As Integer, frm As Form
  3. Dim frmctl As Control
  4.  
  5. 'Must be opened in Disign Mode
  6. DoCmd.OpenForm "frmCreateControl", acDesign, , , acFormEdit, acWindowNormal
  7.  
  8. intLeft = 1440      '1 inch from Left border
  9. intTop = 1440       '1 inch from Top of Form
  10. intWidth = 2160     '1.5 inches in Width
  11. intHeight = 360     '1/4 of an inch in Height
  12.  
  13. 'Create an Unbound Label in the Detail Section of frmCreateControl with the specified dimensions
  14. Set ctl = CreateControl("frmCreateControl", acLabel, acDetail, , , intLeft, intTop, intWidth, intHeight)
  15. With ctl
  16.   .SpecialEffect = 2    'Sunken
  17.   .BackStyle = 1        'Solid
  18.   .BackColor = vbRed
  19. End With
  20.  
  21. 'Close and Save chamges to frmCreateControl
  22. DoCmd.Close acForm, "frmCreateControl", acSaveYes
  23.  
  24. 'Let's view your creation
  25. DoCmd.OpenForm "frmCreateControl", acNormal, , , acFormEdit, acWindowNormal
Sep 1 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.