Create TextBox and Label loop using code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I was wondering how I would go about creating a loop that would generate a
specified number of TextBoxes or Labels in a report.

Thanks for your help!!!
 
I would say the easiest was would be a Do While loop. This can create the
specified number of boxes and labels and can also be set as a variable based
 
Could you please give me a code sample, including the code to create the
textbox/label? I am pretty new at VB and this would really help.

Thanks for your input!!!!!
 
drewdavis1 said:
Could you please give me a code sample, including the code to create the
textbox/label? I am pretty new at VB and this would really help.


I have to say that if you are new to VB/VBA, then you are
getting off on the wrong foot about using controls.

The only time you would want to write a VBA procedure to
create controls is if you are writing a **design** time
wizard for yourself.

For a **runtime** operation, you should (manually or using
code) precreate all possible needed controls at **design**
time, then make them visible as needed at run time.

If you'll explain more about **what** you're trying to
accomplish (instead of the way you've thought to do it),
then maybe someone will be able to provide a better idea.
 
Good point Marshall, I just assumed it was something he wanted to do based on
an in put on a form someplace. i.e. how many children the person has and
would create text box for that number.
 
What i'm trying to do is create a loop that will create a label for each day
of the month (1,2,3,etc) based upon the number of days in a month and display
them in a line at the top of the report.
 
Then manually create 31 labels in the report's design. The
report's Open event can then make some of them invisible
depending on the month. For example,

Select Case Month(datevalue)
Case 4, 6, 9,11
Me("lbl31").Visible = False
Case 2
Me("lbl31").Visible = False
Me("lbl30").Visible = False
If Day(DateSerial(Year(datevalue), 2, 29)) <> 29 _
Then Me("lbl29").Visible = False
End If
End Select
 
Thanks for your input, but that not exactly what i'm looking for. I'm sorry,
i guess i wan't clear enough. I'm working on a Gantt style calendar to
display a schedule one month at a time. I have everything working but the
last thing I want to do is to make they days of the month dynamic based on
the number of days in the month. The labels for the days of the month width
vaires based upon how many days since the entire width of all the days is a
fixed width. Below is a copy and paste of the code that i have written that
works, but is cumbersome to write and i was wondering if i could use a loop
instead:

Private Sub PageHeaderSection_Format(Cancel As Integer, FormatCount As
Integer)
Dim lngNumDays As Long
Dim lngDayLength As Long
lngNumDays = Day(DateSerial(Year(Date), Month(Date) + 1, 0))
lngDayLength = Me.boxTimeLine2.Width / lngNumDays

Me.Date1.Height = Me.boxTimeLine2.Height
Me.Date1.Width = lngDayLength
Me.Date1.Top = Me.boxTimeLine2.Top
Me.Date1.Left = Me.boxTimeLine2.Left
Me.Date1.BorderStyle = 1
Me.Date1.TextAlign = 2

Me.Date2.Height = Me.boxTimeLine2.Height
Me.Date2.Width = lngDayLength
Me.Date2.Top = Me.boxTimeLine2.Top
Me.Date2.Left = Me.boxTimeLine2.Left + Me.Date1.Width
Me.Date2.BorderStyle = 1
Me.Date2.TextAlign = 2

Me.Date3.Height = Me.boxTimeLine2.Height
Me.Date3.Width = lngDayLength
Me.Date3.Top = Me.boxTimeLine2.Top
Me.Date3.Left = Me.boxTimeLine2.Left + Me.Date1.Width + Me.Date2.Width
Me.Date3.BorderStyle = 1
Me.Date3.TextAlign = 2


lngNumdays is the number of days for the current month.

lngDayLength is the size of each day label based upon the number of days.

Each label is inside of a invisable box called Me.boxTimeline2 which is why
all of the sizes of the labes is based upon that box.

As you can see, i have the exact same settings for each label except for the
Left which is based upon all of the previous lables sizes.

Thanks again for all of your help!
 
drewdavis1 said:
Thanks for your input, but that not exactly what i'm looking for. I'm sorry,
i guess i wan't clear enough. I'm working on a Gantt style calendar to
display a schedule one month at a time. I have everything working but the
last thing I want to do is to make they days of the month dynamic based on
the number of days in the month. The labels for the days of the month width
vaires based upon how many days since the entire width of all the days is a
fixed width. Below is a copy and paste of the code that i have written that
works, but is cumbersome to write and i was wondering if i could use a loop
instead:

Private Sub PageHeaderSection_Format(Cancel As Integer, FormatCount As
Integer)
Dim lngNumDays As Long
Dim lngDayLength As Long
lngNumDays = Day(DateSerial(Year(Date), Month(Date) + 1, 0))
lngDayLength = Me.boxTimeLine2.Width / lngNumDays

Me.Date1.Height = Me.boxTimeLine2.Height
Me.Date1.Width = lngDayLength
Me.Date1.Top = Me.boxTimeLine2.Top
Me.Date1.Left = Me.boxTimeLine2.Left
Me.Date1.BorderStyle = 1
Me.Date1.TextAlign = 2

Me.Date2.Height = Me.boxTimeLine2.Height
Me.Date2.Width = lngDayLength
Me.Date2.Top = Me.boxTimeLine2.Top
Me.Date2.Left = Me.boxTimeLine2.Left + Me.Date1.Width
Me.Date2.BorderStyle = 1
Me.Date2.TextAlign = 2

Me.Date3.Height = Me.boxTimeLine2.Height
Me.Date3.Width = lngDayLength
Me.Date3.Top = Me.boxTimeLine2.Top
Me.Date3.Left = Me.boxTimeLine2.Left + Me.Date1.Width + Me.Date2.Width
Me.Date3.BorderStyle = 1
Me.Date3.TextAlign = 2


lngNumdays is the number of days for the current month.

lngDayLength is the size of each day label based upon the number of days.

Each label is inside of a invisable box called Me.boxTimeline2 which is why
all of the sizes of the labes is based upon that box.

As you can see, i have the exact same settings for each label except for the
Left which is based upon all of the previous lables sizes.


Here's a shot at it:

Private Sub PageHeaderSection_Format(Cancel As Integer,
FormatCount As Integer)
Dim lngNumDays As Long
Dim lngDayLength As Long
Dim lngLeft As Long
Dim k As Integer

lngNumDays = Day(DateSerial(Year(Date), Month(Date) + 1, 0))
lngDayLength = Me.boxTimeLine2.Width / lngNumDays
lngLeft = Me.boxTimeLine2.Left

For k = 1 To lngNumDays
With Me("Date" & k)
.Height = Me.boxTimeLine2.Height
.Width = lngDayLength
.Top = Me.boxTimeLine2.Top
.Left = lngLeft + (k - 1) * lngDayLength
.BorderStyle = 1
.TextAlign = 2
End Wih
Next k
 
It worked great! Thank you so much!!!

Marshall Barton said:
Here's a shot at it:

Private Sub PageHeaderSection_Format(Cancel As Integer,
FormatCount As Integer)
Dim lngNumDays As Long
Dim lngDayLength As Long
Dim lngLeft As Long
Dim k As Integer

lngNumDays = Day(DateSerial(Year(Date), Month(Date) + 1, 0))
lngDayLength = Me.boxTimeLine2.Width / lngNumDays
lngLeft = Me.boxTimeLine2.Left

For k = 1 To lngNumDays
With Me("Date" & k)
.Height = Me.boxTimeLine2.Height
.Width = lngDayLength
.Top = Me.boxTimeLine2.Top
.Left = lngLeft + (k - 1) * lngDayLength
.BorderStyle = 1
.TextAlign = 2
End Wih
Next k
 
Drew,
I have been trying to create gantt charts in Access but I haven't
been sucessful. If you can send me a sample of what you did you will be my
hero. My email is (e-mail address removed). Thank you
 
Back
Top