473,320 Members | 1,820 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,320 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
  2.  
  3. Dim myTb() As Class1
  4.  
  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
  24.  
2. Add the class module and write the following code in class1.
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Private WithEvents myTb As MSForms.TextBox
  4. Private myCkType As Long
  5.  
  6. Public Property Set Tb(setTb As MSForms.TextBox)
  7.     Set myTb = setTb
  8. End Property
  9.  
  10. Public Property Get Tb() As MSForms.TextBox
  11. End Property
  12.  
  13. Public Property Let ck(setCk As Long)
  14.     myCkType = setCk
  15. End Property
  16.  
  17. Public Property Get ck() As Long
  18. End Property
  19.  
  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
  30.  
  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
  53.  
Aug 18 '21 #1
0 2658

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

Similar topics

4
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...
3
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...
10
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...
2
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.
20
by: samean | last post by:
Hello, Could you explain me,In VB6 using control array,and how about VB.net. Thanks
14
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...
4
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,...
6
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...
7
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...
4
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.