View Single Post
 
Old 12-09-2022, 08:48 AM
Javi78503 Javi78503 is offline Windows 10 Office 2019
Novice
 
Join Date: Jul 2021
Posts: 12
Javi78503 is on a distinguished road
Default Verify Textbox Data is on Array

Hi Everyone.

I am trying to validate the data the user inputs into a textbox field before it is populated onto the spreadsheet. The data in the field is an object number, of which there are 33 correct object numbers. I am trying to make it so if the data the user inputs into the textbox is not one of those numbers it will ask them to please verify and try again. My issue is that once it alerts them that it is incorrect and clears out the box it goes into a msgbox loop. Not sure how to fix it, but I have included my code below. Please bear with me as I don't code often and am pretty basic with my knowledge. Thank you

Code:
Private Sub ObjectCode_AfterUpdate()

Dim ObjectArray As Variant
ObjectArray = Array("6119", 6121, 6129, 6139, 6141, 6142, 6413, 6145, 6146, 6148, 6149, 6223, 6237, 6238, 6239, 6249, 6256, 6259, 6267, 6268, 6269, 6291, 6293, 6294, 6299, 6329, 6339, 6395, 6399, 6410, 6411, 6497, 6499)

Dim ObjectFound As Boolean

Reset_If:
ObjectFound = False

For i = LBound(ObjectArray) To UBound(ObjectArray)
    If ObjectArray(i) = Me.ObjectCode.Value Then
        ObjectFound = True
    End If
Next

If ObjectFound = False Then
    Application.EnableEvents = False
    MsgBox "Not a valid Object Code. Please verify and try again", vbOKOnly, "Invalid Input"
    Me.ObjectCode.Value = Null
    Application.EnableEvents = True
    GoTo Reset_If
End If

End Sub
Reply With Quote