473,288 Members | 2,350 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,288 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 2646

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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
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: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
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: 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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.