473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,396 developers and data experts.

How to implement control array with vba.

272 256MB
The control array is not implemented in vba.
Class modules are used to achieve this artificially.
The example below shows a form with three text boxes that allow you to enter numbers only, three text boxes that allow you to enter only uppercase letters, and three text boxes that allow you to enter numbers and uppercase letters.

1. Place 9 text boxes on UserForm1 and write the following code.
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  3. Dim myTb() As Class1
  5. Private Sub UserForm_Initialize()
  6.     Dim i As Long
  7.     ReDim myTb(1 To 9)
  8.     For i = 1 To 3
  9.         Set myTb(i) = New Class1
  10.         Set myTb(i).Tb = Me.Controls("TextBox" & CStr(i))
  11.         myTb(i).ck = 1 'Allows only numeric
  12.     Next
  13.     For i = 4 To 6
  14.         Set myTb(i) = New Class1
  15.         Set myTb(i).Tb = Me.Controls("TextBox" & CStr(i))
  16.         myTb(i).ck = 2 'Allows only Uppercase letters
  17.     Next
  18.     For i = 7 To 9
  19.         Set myTb(i) = New Class1
  20.         Set myTb(i).Tb = Me.Controls("TextBox" & CStr(i))
  21.         myTb(i).ck = 3 'Allows numeric and Uppercase letters
  22.     Next
  23. End Sub
2. Add the class module and write the following code in class1.
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  3. Private WithEvents myTb As MSForms.TextBox
  4. Private myCkType As Long
  6. Public Property Set Tb(setTb As MSForms.TextBox)
  7.     Set myTb = setTb
  8. End Property
  10. Public Property Get Tb() As MSForms.TextBox
  11. End Property
  13. Public Property Let ck(setCk As Long)
  14.     myCkType = setCk
  15. End Property
  17. Public Property Get ck() As Long
  18. End Property
  20. Private Sub myTb_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  21.     Select Case myCkType
  22.         Case 1
  23.             MsgBox "Allows you to enter numbers only.", vbOKOnly + vbCritical, "Error"
  24.         Case 2
  25.             MsgBox "Allows you to enter uppercase letters only.", vbOKOnly + vbCritical, "Error"
  26.         Case 3
  27.             MsgBox "Allows you to enter numeric and uppercase letters only.", vbOKOnly + vbCritical, "Error"
  28.      End Select
  29. End Sub
  31. Private Sub myTb_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  32.     Select Case myCkType
  33.         Case 1
  34.             'Allows only numeric
  35.             If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
  36.                 KeyAscii = 0
  37.             End If
  38.         Case 2
  39.             'Allows only uppercase letters
  40.             If KeyAscii >= Asc("A") And KeyAscii <= Asc("Z") Then
  41.             Else
  42.                 KeyAscii = 0
  43.             End If
  44.         Case 3
  45.             'Allows numeric and uppercase letters
  46.             If (KeyAscii >= Asc("0") And KeyAscii <= Asc("9")) Or _
  47.                 (KeyAscii >= Asc("A") And KeyAscii <= Asc("Z")) Then
  48.             Else
  49.                 KeyAscii = 0
  50.             End If
  51.     End Select
  52. End Sub
Aug 18 '21 #1
0 2676

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

Similar topics

by: The Mess | last post by:
I would like to pass a Control array of OptionButtons that I created at run time to a Sub. Say I have Opt(0), Opt(1)......Opt(5) as OptionButtons, is there a way to pass Opt to a function and...
by: Mani | last post by:
Hi, can anyone help me on how to create a control array in C++ builder like we create in VB. I have another question regarding controls. I want to clear a group of text boxes in the form on a...
by: James McGivney | last post by:
I have a project in ASP.NET using C#. I have a large number of Button Controls and a large number of ImageButton Controls. It would be convienent if I could refer to a specific control by it's...
by: RBohannon | last post by:
Is it possible to create a control array on an unbound form? I would like to be able to loop through a series of unbound text boxes. Thanks.
by: samean | last post by:
Hello, Could you explain me,In VB6 using control array,and how about VB.net. Thanks
by: Jim Burns | last post by:
I just started with vb.net and I don't understand why there are no control arrays. what replaced them. I was trying to do drag and drop(What happened with that) and it looked like with no control...
by: Rich | last post by:
Hello, I have 3 textboxes and 1 combobox on a form. On entering the control I want to select all the text. I can make an array of textboxes like this: Dim arrTxt As TextBox() = {txt1, txt2,...
by: Rich | last post by:
Hello, I have an application that contains several checkboxes. I originally created this app in VB.Net 2003 and upgraded the app to VB.Net 2005. I understand the vb2005 supports control...
by: Igor | last post by:
Can I create control array (like TextBox array) in design mode. In vb6 I drow some control at the form and then I copy/paste controls. After that every control have the same name, but they have...
by: coldude | last post by:
Hi, i have been playing with some code that creates Control array labels that display dates down in a column in a Windows Form, but i am having trouble with removing Control array labels if a...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.