Solved

Creating objects on a form using Form_Load.....

Posted on 2001-06-20
9
157 Views
Last Modified: 2010-05-02
Ok, this one is a bit harder.

I want 31 new labels for my form, with the values 1 - 31. I know there must be an easier way to add them other then manually enter them in, so I thought I would ask the experts!!

I appreciate any help

Lewis Keen
a.k.a Yeggstry
0
Comment
Question by:yeggstry
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 6

Expert Comment

by:JonFish85
ID: 6211913
Dim lbl As Label
Dim cnt As Integer

LastTop = 0
For cnt = 1 to 31
  Set lbl = Me.Controls.Add("VB.Label", "lbl" & cnt)
  With lbl
    .Move 10, cnt
    .AutoSize = True
    .Visible = True
    .Caption = cnt
  End With
Next cnt


try that maybe?
0
 

Author Comment

by:yeggstry
ID: 6211923
cheers, i will try, but any other solutions are welcome!!
0
 
LVL 6

Expert Comment

by:JonFish85
ID: 6211933
also you can add one label to your form, name it lblNumbers and set its index property to 0. then:

Dim cnt as Integer

for cnt = 1 to 31
  Load lblNumbers(lblNumbers.UBound + 1)
  With lblNumbers(lblNumbers.UBound)
    .Top = lblNumbers(lblNumbers.UBound - 1).Top + .Height
    .Left = 10
    .Caption = cnt
    .Visible = True
  End With
Next cnt
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 15

Accepted Solution

by:
ameba earned 200 total points
ID: 6212637
To add labels at design time, use some add-in or create your own.
Simple sample:
Start new Add-In project (menu File, New Project, AddIn) - this will create Connect.Dsr and frmAddIn form.
Replace code in frmAddIn with this:

' frmaddIn, add one textbox (2 buttons are already on form)
Option Explicit
Public VBInstance As VBIDE.VBE
Public Connect As Connect

Private Sub CancelButton_Click()
    Connect.Hide
End Sub

Private Sub Form_Load()
    Caption = "Enter number of labels on new form"
    Text1.Text = "31"
End Sub

Private Sub OKButton_Click()
    Dim myC As VBControl, thisF As VBForm, i As Integer
    On Error Resume Next
   
    ' add new form
    Set thisF = VBInstance.VBProjects(1).VBComponents.Add(vbext_ct_VBForm).Designer
    For i = 1 To CInt(Text1.Text)
        Set myC = thisF.ContainedVBControls.Add("VB.Label")
        myC.Properties("Visible") = True
        myC.Properties("left") = 300
        myC.Properties("top") = i * 225
        myC.Properties("name") = "Label" & CStr(i)
        myC.Properties("height") = 225
        myC.Properties("width") = 2775
        myC.ControlObject.Caption = CStr(i)
    Next
End Sub
' -------------------------------------
To test addin, press F5, start new instance of VB, and click menu Add-Ins, My Addin.
0
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 6213138
ameba,
thats interesting.
0
 
LVL 15

Expert Comment

by:ameba
ID: 6453660
Why did you punish me with a B ???

If something wasn't clear, you should have asked for clarification or for more info.

Do NOT expect any further help from me!
0
 

Author Comment

by:yeggstry
ID: 6453672
grow up ameba

I gave you a B because your answer was sufficient, but could have been better.
0
 
LVL 15

Expert Comment

by:ameba
ID: 6454272
>grow up ameba

Some people really don't deserve ANY HELP!
0
 

Expert Comment

by:amp072397
ID: 6711598
Well, I've found my way to this question rather indirectly. However, B grades are not given if the answer was sufficient. Yeggstry, I'll gladly post the guidelines for grading. If you did not know the answer, how could you possibly know that the answer could have been better?

Changing grade to an A...

amp
Community Support Moderator
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question