How can a reference a control Programmatically

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tasawer
    New Member
    • Aug 2009
    • 106

    How can a reference a control Programmatically

    Hi,

    I am opening a recordset to read booking times, converting them to twips and storing them in array to display some boxes that represent a timescale.
    My current method used below, will display just six boxes.

    How can i adapt this to display any number of boxes and of course, reduce the coding. (of course, I could continue to use my method, but not favourable)

    I am wondering if I could use some kind of control method to reference the Boxes in this manner:
    Code:
    "BOX" & A = .....
    Code:
        For A = 1 To NoofBookings
        'MsgBox TimeSlot(A, 0) & " " & TimeSlot(A, 1) & " " & TimeSlot(A, 2) & " " & TimeSlot(A, 3)
        Select Case A
            Case 1
                box1.Visible = True
                box1.Left = ((((TimeSlot(A, 0) + (TimeSlot(A, 1) / 60) - 9) * 2) + LeftPos)) * tw
                box1.Width = ((TimeSlot(A, 2) + (TimeSlot(A, 3) / 60))) * tw * 2
                box1.Caption = TimeReg(A)
            
            Case 2
                Box2.Visible = True
                Box2.Left = ((((TimeSlot(A, 0) + (TimeSlot(A, 1) / 60) - 9) * 2) + LeftPos)) * tw
                Box2.Width = ((TimeSlot(A, 2) + (TimeSlot(A, 3) / 60))) * tw * 2
                Box2.Caption = TimeReg(A)
            
            Case 3
                Box3.Visible = True
                Box3.Left = ((((TimeSlot(A, 0) + (TimeSlot(A, 1) / 60) - 9) * 2) + LeftPos)) * tw
                Box3.Width = ((TimeSlot(A, 2) + (TimeSlot(A, 3) / 60))) * tw * 2
                Box3.Caption = TimeReg(A)
            
            Case 4
                Box4.Visible = True
                Box4.Left = ((((TimeSlot(A, 0) + (TimeSlot(A, 1) / 60) - 9) * 2) + LeftPos)) * tw
                Box4.Width = ((TimeSlot(A, 2) + (TimeSlot(A, 3) / 60))) * tw * 2
                Box4.Caption = TimeReg(A)
            
            Case 5
                Box5.Visible = True
                Box5.Left = ((((TimeSlot(A, 0) + (TimeSlot(A, 1) / 60) - 9) * 2) + LeftPos)) * tw
                Box5.Width = ((TimeSlot(A, 2) + (TimeSlot(A, 3) / 60))) * tw * 2
                Box5.Caption = TimeReg(A)
                
            Case 6
                Box6.Visible = True
                Box6.Left = ((TimeSlot(A, 0) + (TimeSlot(A, 1) / 60) - 9 + LeftPos)) * tw
                Box6.Width = ((TimeSlot(A, 2) + (TimeSlot(A, 3) / 60))) * tw * 2
                Box6.Caption = TimeReg(A)
                
            End Select
            Next A
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Let's assume that you have 10 Text Boxes sequentially numbered Box1 thru Box10. The general Logic for accessing the Properties of these Text Boxes within a single Loop wouold be:
    Code:
    Dim TBox As TextBox
    Dim intBoxCtr As Integer
    
    For intBoxCtr = 1 To 10
      Set TBox = Me.Controls("Box" & CStr(intBoxCtr))
        TBox.BackColor = QBColor(intBoxCtr)
    Next
    As a side note:
    1. Converting Booking Times to Twips (1/1,440 in.) makes absolutely no sense to me.
    2. Boxes do not have a Caption Property, and attempting to assign one will generate a Runtime Error.

    Comment

    Working...