How to use a public function in vba access

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ezevibe
    New Member
    • Aug 2021
    • 1

    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
  • SioSio
    Contributor
    • Dec 2019
    • 272

    #2
    If you use the class module to make nine textboxes into a control array, you can process them with one event function.

    Comment

    • cactusdata
      Recognized Expert New Member
      • Aug 2007
      • 223

      #3
      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:

      Code:
      Private Sub YourTextbox_KeyPress(KeyAscii As Integer)
      
          ValidateKeyPress KeyAscii
      
      End Sub
      
      
      Private Sub ValidateKeyPress(ByRef KeyAscii As Integer)
      
          Const KeyNone   As Integer = 0
          
          Select Case KeyAscii
              Case vbKeyBack
                  ' Accept backspace.
              Case Is < vbKey0
                  ' Not a number key.
                  KeyAscii = KeyNone
              Case Is > vbKey9
                  ' Not a number key.
                  KeyAscii = KeyNone
          End Select
      
      End Sub
      Likewise, accept the Null and replace it when updating the record:

      Code:
      Private Sub Form_BeforeUpdate(Cancel As Integer)
      
          Me!Textbox1.Value = Nz(Me!Textbox1.Value, 0)
          Me!Textbox2.Value = Nz(Me!Textbox2.Value, 0)
          ' ...
          Me!Textbox9.Value = Nz(Me!Textbox9.Value, 0)
          
      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.

      Comment

      • SioSio
        Contributor
        • Dec 2019
        • 272

        #4
        Create nine text boxes in the UserForm and code the following:
        Code:
        Option Explicit
        Dim myTb() As Class1
        Private Sub UserForm_Initialize()
            Dim i As Long
            ReDim myTb(1 To 9)
            For i = 1 To 9
                Set myTb(i) = New Class1
                Set myTb(i).Tb = Me.Controls("TextBox" & CStr(i))
            Next
        End Sub
        Add a Class1 module and code the following:
        Code:
        Option Explicit
        Private WithEvents myTb As MSForms.TextBox
        Public Property Set Tb(setTb As MSForms.TextBox)
            Set myTb = setTb
        End Property
        Public Property Get Tb() As MSForms.TextBox
        End Property
        Private Sub myTb_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
            MsgBox "Allows you to enter numbers only.", vbOKOnly + vbCritical, "Error"
            myTb.Text = "0"
        End Sub
        Private Sub myTb_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
            If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
               KeyAscii = 0
            End If
        End Sub
        I'm not sure if the myTb_MouseUp() event fits your needs, so delete it if you don't need it.

        Comment

        Working...