473,382 Members | 1,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How to use a public function in vba access

I have 9 text boxes in my form that should accept only numbers. I want to warn users anytime text is entered into those text boxes. I also want to warn users that none of the text boxes should be null on editing any value in any of the text boxes with a message that give them the only option to return the boxes to zero.

Now, I want to use a function (either private or public) that does this once. I don't want to code each text boxes separately. Thanks

Thanks
Aug 4 '21 #1
3 4715
SioSio
272 256MB
If you use the class module to make nine textboxes into a control array, you can process them with one event function.
Aug 19 '21 #2
cactusdata
214 Expert 128KB
Be careful with all these warnings, it's a nuisance to users. Let the computer do the work, that's what they are for.
So, check the key press and accept only number input by, for each textbox, validating the key press:

Expand|Select|Wrap|Line Numbers
  1. Private Sub YourTextbox_KeyPress(KeyAscii As Integer)
  2.  
  3.     ValidateKeyPress KeyAscii
  4.  
  5. End Sub
  6.  
  7.  
  8. Private Sub ValidateKeyPress(ByRef KeyAscii As Integer)
  9.  
  10.     Const KeyNone   As Integer = 0
  11.  
  12.     Select Case KeyAscii
  13.         Case vbKeyBack
  14.             ' Accept backspace.
  15.         Case Is < vbKey0
  16.             ' Not a number key.
  17.             KeyAscii = KeyNone
  18.         Case Is > vbKey9
  19.             ' Not a number key.
  20.             KeyAscii = KeyNone
  21.     End Select
  22.  
  23. End Sub
Likewise, accept the Null and replace it when updating the record:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2.  
  3.     Me!Textbox1.Value = Nz(Me!Textbox1.Value, 0)
  4.     Me!Textbox2.Value = Nz(Me!Textbox2.Value, 0)
  5.     ' ...
  6.     Me!Textbox9.Value = Nz(Me!Textbox9.Value, 0)
  7.  
  8. End Sub
However, even that can be too much coding if more than a few textboxes (or other controls) are to be handled.
If so, turn to WithEvents. "How", is a story longer than to be told here, but study an example as this: https://github.com/GustavBrock/VBA.ModernTheme.
Aug 19 '21 #3
SioSio
272 256MB
Create nine text boxes in the UserForm and code the following:
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Dim myTb() As Class1
  3. Private Sub UserForm_Initialize()
  4.     Dim i As Long
  5.     ReDim myTb(1 To 9)
  6.     For i = 1 To 9
  7.         Set myTb(i) = New Class1
  8.         Set myTb(i).Tb = Me.Controls("TextBox" & CStr(i))
  9.     Next
  10. End Sub
Add a Class1 module and code the following:
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Private WithEvents myTb As MSForms.TextBox
  3. Public Property Set Tb(setTb As MSForms.TextBox)
  4.     Set myTb = setTb
  5. End Property
  6. Public Property Get Tb() As MSForms.TextBox
  7. End Property
  8. Private Sub myTb_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  9.     MsgBox "Allows you to enter numbers only.", vbOKOnly + vbCritical, "Error"
  10.     myTb.Text = "0"
  11. End Sub
  12. Private Sub myTb_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  13.     If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
  14.        KeyAscii = 0
  15.     End If
  16. End Sub
  17.  
I'm not sure if the myTb_MouseUp() event fits your needs, so delete it if you don't need it.
Aug 19 '21 #4

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

Similar topics

4
by: MLH | last post by:
The following function declaration confuses me... Public Function IsEMailAddress(ByVal sEmail As String, Optional ByRef sReason As String) As Boolean I tried pasting it and its code into an...
15
by: Keith | last post by:
A97, NT4. I've seen an example of this posted here but I can't for the life of me find it. If I have a lot of controls on a form which all require the same After Update action, what syntax do...
4
by: Chris | last post by:
Hello, I'm just getting started with VB and am new to the group, so please excuse what may seem to be a rudimentary question. I've been writing basic programs and have noticed that the...
2
by: Jon Paal | last post by:
In a 2.0 vb code class, I have a private property and a shared public function The property value has already been passed into the class, now I am trying to use a public finction which also needs...
3
by: ragtopcaddy via AccessMonster.com | last post by:
I have a couple of questions regarding the use of the following function: Public Function dbLocal(Optional ysnInitialize As Boolean = True) As DAO. Database 1. Do I just replace all the...
5
by: =?Utf-8?B?V0o=?= | last post by:
If I have a class A that has a virtual public function DoSomething() Class A { public: virtual void DoSomething(); } then I override this function to protected is class B Class B: public...
12
by: juengelj | last post by:
Ok, pros... I have like 80 rectangles on a form that i want to kinda act like check boxes. I have already figured out how to get them to appear/dissappear when the list box value that corresponds to...
3
by: dolphin | last post by:
Hello everyone! Can a static member function access non-static member? I think it is illegal.Is it right?
7
by: blublu123 | last post by:
Hi all, I have a number of fields on various forms that need data validation. If fields are empty (null), the form should not save and close, and the cursor should go to the appropriate field. I...
9
by: brittaff | last post by:
I am writing code in VBA in Access. I need to filtered a data set. I am brand new to this so I apologize in advance if this isn't clear. qryFilter is the query that creates a table "Filter" that...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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 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.