In this video we will cover VBA validation functions like:
Validating Text Strings
Check the string for null values.
IsNull function
Check the length of a string.
Len Function
Check the presence of characters present in a string.
InStr function
Inpect a portion of a string.
Left, Right, Mid functions
Removing the leading or trailing spaces, or both, from a string of text.
Trim, LTrim, RTrim functions
Validating Numbers
IsNumeric function
IsDate function
Here is the video:
Private Sub btnNull_Click() 'tests if the passed value is blank (nothing). If IsNull(Me.txtEmail) Then MsgBox "The value is blank." Else MsgBox "The value is not blank." End If End Sub Private Sub btnIsDate_Click() 'tests if the passed value is understood as a date. If IsDate(Me.txtEmail) Then MsgBox "The value is a date." Else MsgBox "The value is not a date." End If End Sub Private Sub btnIsNumeric_Click() 'test if the passed value is a number If IsNumeric(Me.txtEmail) Then MsgBox "The value is a number." Else MsgBox "The value is not a number." End If End Sub Private Sub btnLeft_Click() Dim strPart As String 'gets the 3 characters from the beginning of the string strPart = Left(Me.txtEmail, 3) MsgBox "The left 3 characters are: " & strPart End Sub Private Sub btnMid_Click() Dim strPart As String 'gets three characters from the text, starting at the fourth character strPart = Mid(Me.txtEmail, 4, 3) MsgBox "The Mid 3 characters from fourth character are: " & strPart End Sub Private Sub btnRight_Click() Dim strPart As String 'gets the 4 characters from the end of the string strPart = Right(Me.txtEmail, 4) MsgBox "The Right 4 characters are: " & strPart End Sub Private Sub btnLen_Click() Dim intLen As Integer 'tests the length of the string intLen = Len(Me.txtEmail) MsgBox "The length is: " & intLen & " characters" End Sub Private Sub btnTrim_Click() Dim strPart As String 'removes unwanted spaces from the beginning and _ end of a string of text strPart = Trim(Me.txtEmail) Me.txtEmail = strPart MsgBox "The text has been cleaned. " End Sub Private Sub btnInstr_Click() 'check for a value in a string. Good for email validation If IsEmailAddress(Me.txtEmail) Then MsgBox "This is a valid email address" Else MsgBox "Please enter a valid email address" End If End Sub Function IsEmailAddress(email_address) As Boolean 'is there the presence of an @ sign? If InStr(1, email_address, "@") Then 'is there a ".com"? If InStr(1, email_address, ".com") Then IsEmailAddress = True Else IsEmailAddress = False End If Else IsEmailAddress = False End If End Function
<< MS Access Validation – PT2 (explained) | How To Make A VBA Function>>
Learn Access VBA: Understand Tables, Queries, Forms, and Reports
Learn Access VBA: From Zero to Database Hero If you’ve ever opened Microsoft Access and wondered how all the pieces fit together — tables, queries, forms, and reports — this tutorial is made for you. In just a few minutes, you’ll understand how Access works behind the scenes and see how VBA (Visual Basic for […]
How To Make An Access Form Time Picker
Here is a relatively easy way to select times for your time entry text boxes. It’s a reusable form that allows you to pick a time from an Access form. There are probably different ways to do this but here is the way I would do it. On the form that has the time fields, […]
How To Parse A Flat File In Excel VBA
In another post I demonstrated how to access a file on your computer using the MS Office Library. Here it is if you don’t know what I’m talking about. In this post, I am going to show you how to access the file and load it into your spreadsheet. I will do the same thing […]
How to pick a file to load In VBA
How to Pick a File in VBA: FileDialog & GetOpenFilename Explained When building Excel VBA applications, you’ll often need to let users pick a file to load in VBA. Instead of hard-coding file paths, you can use built-in dialogs that make file selection easy and user-friendly. VBA offers two main approaches: FileDialog object (flexible, customizable) […]
Support these sponsors: