Results 1 to 4 of 4

Thread: VBA - Control Array

  1. #1

    Thread Starter
    Frenzied Member agmorgan's Avatar
    Join Date
    Dec 2000
    Location
    Lurking
    Posts
    1,383

    VBA - Control Array

    Remember this is using VBA in Excel!

    I have a form with 5 RefEdit controls on it and 5 associated labels.
    There is also a textbox with a spinbutton (UpDown control?)
    which regulates how many of the RefEdit controls are enabled.

    A control array would be better than this select case, but control
    arrays aren't available in VBA.
    How can I improve?
    VB Code:
    1. Private Sub spnLines_Change()
    2.  
    3. ' Change the Value of the textbox with
    4. ' the UpDown control's Value property.
    5. txtLines.Text = Format(spnLines.Value, "0")
    6.  
    7.  
    8. End Sub
    9.  
    10.  
    11.  
    12.  
    13.  
    14. Private Sub txtLines_Change()
    15. 'Make sure entry is a number, if not, beep and put in last value.
    16.     If Not IsNumeric(txtLines.Text) Then
    17.         Beep
    18.         txtLines.Text = Format(spnLines.Value, "0")
    19.         Cancel = True
    20.         Exit Sub
    21.     End If
    22. 'Check values are within accepted range, exit if not.
    23.     If Val(txtLines.Text) > spnLines.Max Or Val(txtLines.Text) < spnLines.Min Then
    24.         MsgBox "Number Of Lines Must Be Between 1 and 5"
    25.     Exit Sub
    26.     End If
    27.  
    28.     spnLines.Value = Val(txtLines.Text)
    29.    
    30. Select Case CInt(txtLines.Text)
    31.  
    32.     Case 1
    33.         RefEdit2.Enabled = False
    34.         RefEdit3.Enabled = False
    35.         RefEdit4.Enabled = False
    36.         RefEdit5.Enabled = False
    37.         lblLine2.Enabled = False
    38.         lblLine3.Enabled = False
    39.         lblLine4.Enabled = False
    40.         lblLine5.Enabled = False
    41.     Case 2
    42.         RefEdit2.Enabled = True
    43.         RefEdit3.Enabled = False
    44.         RefEdit4.Enabled = False
    45.         RefEdit5.Enabled = False
    46.         lblLine2.Enabled = True
    47.         lblLine3.Enabled = False
    48.         lblLine4.Enabled = False
    49.         lblLine5.Enabled = False
    50.     Case 3
    51.         RefEdit2.Enabled = True
    52.         RefEdit3.Enabled = True
    53.         RefEdit4.Enabled = False
    54.         RefEdit5.Enabled = False
    55.         lblLine2.Enabled = True
    56.         lblLine3.Enabled = True
    57.         lblLine4.Enabled = False
    58.         lblLine5.Enabled = False
    59.     Case 4
    60.         RefEdit2.Enabled = True
    61.         RefEdit3.Enabled = True
    62.         RefEdit4.Enabled = True
    63.         RefEdit5.Enabled = False
    64.         lblLine2.Enabled = True
    65.         lblLine3.Enabled = True
    66.         lblLine4.Enabled = True
    67.         lblLine5.Enabled = False
    68.     Case 5
    69.         RefEdit2.Enabled = True
    70.         RefEdit3.Enabled = True
    71.         RefEdit4.Enabled = True
    72.         RefEdit5.Enabled = True
    73.         lblLine2.Enabled = True
    74.         lblLine3.Enabled = True
    75.         lblLine4.Enabled = True
    76.         lblLine5.Enabled = True
    77.        
    78. End Select
    79.  
    80. End Sub
    81.  
    82. Private Sub txtLines_Enter()
    83. ' Start highlight before first character.
    84. txtLines.SelStart = 0
    85. ' Highlight to end of text.
    86. txtLines.SelLength = Len(txtLines.Text)
    87. End Sub

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    VB Code:
    1. [size=1]Private Sub spnLines_Change()
    2.     ' Change the Value of the textbox with the UpDown control's Value property.
    3.     txtLines.Text = CStr(spnLines.Value)
    4. End Sub
    5.  
    6.  
    7. Private Sub txtLines_Change()
    8.     'Make sure entry is a number, if not, beep and put in last value.
    9.     If Not IsNumeric(txtLines.Text) Then
    10.         Beep
    11.         txtLines.Text = Format(spnLines.Value, "0")
    12.         Cancel = True
    13.    
    14.     'Check values are within accepted range, exit if not.
    15.     ElseIf Val(txtLines.Text) > spnLines.Max Or Val(txtLines.Text) < spnLines.Min Then
    16.         MsgBox "Number Of Lines Must Be Between 1 and 5"
    17.  
    18.     ' Call below procedure to enable/disable relavent form controls.
    19.     Else
    20.         spnLines.Value = Val(txtLines.Text)
    21.         DisableFormControls (txtLines.Text)
    22.     End If
    23. End Sub
    24.  
    25.  
    26. Private Sub DisableFormControls(ByVal strNumberEntered As String)
    27.     Dim ctlFormControl As Control
    28.    
    29.     For Each ctlFormControl In UserForm1.Controls
    30.         If Not (Right(ctlFormControl.Name, 1) = strNumberEntered) And _
    31.         Not (ctlFormControl.Name = "txtLines") Then
    32.             If (Mid(ctlFormControl.Name, 4, 4) = "Edit") Or _
    33.             (Mid(ctlFormControl.Name, 4, 4) = "Line") Then
    34.                 ctlFormControl.Enabled = False
    35.             End If
    36.         Else
    37.             ctlFormControl.Enabled = True
    38.         End If
    39.     Next ctlFormControl
    40. End Sub
    41.  
    42.  
    43. Private Sub txtLines_Enter()
    44.     ' Start highlight before first character, & highlight to end of text.
    45.     With txtLines
    46.         .SelStart = 0
    47.         .SelLength = Len(.Text)
    48.     End With
    49. End Sub[/size]

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3

    Thread Starter
    Frenzied Member agmorgan's Avatar
    Join Date
    Dec 2000
    Location
    Lurking
    Posts
    1,383
    Genius!
    I modified the disable procedure slightly
    VB Code:
    1. Private Sub DisableFormControls(ByVal strNumberEntered As String)
    2.     Dim ctlFormControl As Control
    3.    
    4.     For Each ctlFormControl In frmMain.Controls
    5.         If Not (Right(ctlFormControl.Name, 1) <= strNumberEntered) And _
    6.         Not (ctlFormControl.Name = "txtLines") And _
    7.         Not (ctlFormControl.Name = "lblLines") And _
    8.         Not (ctlFormControl.Name = "spnLines") Then
    9.             If (Mid(ctlFormControl.Name, 4, 4) = "Edit") Or _
    10.             (Mid(ctlFormControl.Name, 4, 4) = "Line") Then
    11.                 ctlFormControl.Enabled = False
    12.             End If
    13.         Else
    14.             ctlFormControl.Enabled = True
    15.         End If
    16.     Next ctlFormControl
    17. End Sub
    Now there is <= and also a couple of other controls excluded

    Thanks!

  4. #4

    Thread Starter
    Frenzied Member agmorgan's Avatar
    Join Date
    Dec 2000
    Location
    Lurking
    Posts
    1,383
    OK. I have tried to take Alexs example and modify another piece of my code
    Have I done it right or is there still some tweaking?
    I was using the following to check if all the data ranges were full
    VB Code:
    1. Private Sub cmdOK_Click()
    2.  
    3. If RefEdit1.Value = "" Then
    4.     MsgBox "Don't forget to select a data range!"
    5.     Exit Sub
    6. End If
    7.  
    8. If RefEdit2.Enabled = True And RefEdit2.Value = "" Then
    9.     MsgBox "Don't forget to select a data range for the second line!"
    10.     Exit Sub
    11. End If
    12.  
    13. If RefEdit3.Enabled = True And RefEdit3.Value = "" Then
    14.     MsgBox "Don't forget to select a data range for the third line!"
    15.     Exit Sub
    16. End If
    17.  
    18. If RefEdit4.Enabled = True And RefEdit4.Value = "" Then
    19.     MsgBox "Don't forget to select a data range for the fourth line!"
    20.     Exit Sub
    21. End If
    22.  
    23. If RefEdit5.Enabled = True And RefEdit5.Value = "" Then
    24.     MsgBox "Don't forget to select a data range for the fifth line!"
    25.     Exit Sub
    26. End If
    27.  
    28. '......
    29. '......
    30.  
    31. End Sub
    Now I have changed it to this
    VB Code:
    1. Private Sub cmdOK_Click()
    2.  
    3. Dim blnRange As Boolean
    4. Call CheckControls(blnRange)
    5. If blnRange = False Then
    6.     Exit Sub
    7. End If
    8.  
    9. '......
    10. '......
    11.  
    12. End Sub
    13.  
    14.  
    15. Private Sub CheckControls(ByRef blnRange As Boolean)
    16.     Dim ctlFormControl As Control
    17.    
    18.     blnRange = True
    19.     For Each ctlFormControl In frmMain.Controls
    20.         If Left(ctlFormControl.Name, 7) = "RefEdit" Then
    21.             If ctlFormControl.Enabled = True And _
    22.                     ctlFormControl.Value = "" Then
    23.                 MsgBox "Don't forget to select a data range for line " & Right(ctlFormControl.Name, 1) & "!"
    24.                 blnRange = False
    25.                 Exit Sub
    26.             End If
    27.  
    28.         End If
    29.     Next ctlFormControl
    30. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width