VBA - textbox check for specific format

gdesruis

New member
Local time
Today, 04:51
Joined
May 22, 2013
Messages
8
I would like to run a check on a textbox to ensure it contains a proper format.

format:
starts with: 0-
contains: 7 digits after.
 
I assume you mean that you have an unbound text field control, yes?

You will need to safely read that text field control, so see this post:

Safely read form field text controls
http://www.access-programmers.co.uk/forums/showthread.php?p=1131039#post1131115

Once read into a VBA string variable, then you will need to process the string character by character to insure it meets the business logic requirements... since it is not more simple rule logic, like "must be a valid integer value".

Actually, since you say you need a certain portion of the string to be numeric only, then you could use this function to insure numeric datatype:

Code:
Public Function datatypevalidation_RequiredNumericOnly(ByRef vntExpression As Variant) As Boolean
  On Error GoTo Err_datatypevalidation_RequiredNumericOnly

  Dim lngMaxStep As Long
  Dim lngStep As Long
  Dim intThisChar As Integer

  'Assume invalid data
  datatypevalidation_RequiredNumericOnly = False

 'Size up how many characters / digits in the variable we were passed
  lngMaxStep = Len(vntExpression)

  'Go through one character at a time, validate 0-9
  For lngStep = 1 To lngMaxStep
    'Grab the next character...
    intThisChar = Asc(Mid(vntExpression, lngStep, 1))

    'Perhaps it is a number...
    If (intThisChar >= 48) And (intThisChar <= 57) Then
      'Valid character, do nothing...
    Else
      'Out of valid options, must not be valid
      GoTo Exit_datatypevalidation_RequiredNumericOnly
    End If
  Next lngStep

  'Checks out valid
  datatypevalidation_RequiredNumericOnly = True

Exit_datatypevalidation_RequiredNumericOnly:
  Exit Function

Err_datatypevalidation_RequiredNumericOnly:
  Call errorhandler_MsgBox("Module: modshared_datatypevalidation, Function: datatypevalidation_RequiredNumericOnly()")
  Resume Exit_datatypevalidation_RequiredNumericOnly

End Function
 
Wow thanks, I will look into all of this. I might be way out of my league here. and yes an unbound textbox.
 

Users who are viewing this thread

Back
Top Bottom