Rosario
Rosario

Reputation: 47

VBA Do Loop and Input Box content not working

I'm new with VBA loops. What I'm trying to do is a do loop and input box procedure that asks for a product code until a valid one is input. The code should start with the letter P, and should be followed by four digits. For an invalid code input, it should display a message telling the user why it's invalid.

I have the following procedure coded. It works for when the user types p9887 for example.

However, if the user types o899876 or p877789, it gives the message "Product Code should have five characters" and then the user has to input again. In this second try, if user types p9876, while it meets all the criteria, the message that comes up from my procedure is "The last four characters should be digits" and it is stuck at that loop where the user has to type in their input again, and the same message comes up.

Any insight on what I'm doing wrong is greatly appreciated!

Option Explicit
Public Sub ProductCode()


Dim strInput As String

Dim intFrstLetter As Integer

Dim intLastFour As String

Dim strFrstLetter As String

Dim test As String

Dim blDone As Boolean


strInput = InputBox("Please enter product code")

intFrstLetter = InStr(1, strInput, "p")

intLastFour = Right(strInput, 4)

strFrstLetter = Left(strInput, 1)

Do

If strFrstLetter = "p" Then

    If Len(strInput) <> 5 Then
      MsgBox "Product code should have five characters."
     strInput = InputBox("Please enter product code")
    Else
       If IsNumeric(intLastFour) Then
       MsgBox "Thank You"
       blDone = True
       Exit Do
       Else
         MsgBox "The last four characters should be digits"
         strInput = InputBox("Please enter product code")
  If strFrstLetter <> "p" Then
    MsgBox "Product code should start with the letter P"
    strInput = InputBox("Please enter product code")
        End If
    End If
  End If
End If
Loop Until blDone = True
End Sub

************************ Here is another type of code that is cleaner, but still does the same issue.

Public Sub ProductCode()
Dim strInput As String
Dim intFrstLetter As Integer
Dim intLastFour As String
Dim strFrstLetter As String
Dim blDone As Boolean


strInput = InputBox("Please enter product code")

intFrstLetter = InStr(1, strInput, "p")
intLastFour = Right(strInput, 4)
strFrstLetter = Left(strInput, 1)
Do
    If strFrstLetter = "p" Then
        If Len(strInput) = 5 Then
           If IsNumeric(intLastFour) = True Then
            MsgBox "Thank You"
            Exit Do
           Else
            MsgBox "The last four characters should be digits"
            strInput = InputBox("Please enter product code")
           End If
       Else
         MsgBox "Product code should have five characters"
         strInput = InputBox("Please enter product code")
       End If
  Else
    MsgBox "Product code should start with the letter P"
    strInput = InputBox("Please enter product code")
 End If
Loop

Upvotes: 0

Views: 1000

Answers (3)

user3598756
user3598756

Reputation: 29421

Try this

Do While Not blDone
    blDone = InputBox("Please enter product code") Like "P####"
    If Not blDone Then MsgBox "the input didn't match the pattern 'P####' where:" _
                              & vbCrLf & vbCrLf & vbTab & "'P' must be the 'P' letter" _
                              & vbCrLf & vbTab & "'####' must be four integer digits"
Loop

A more "helping" input block code could be the following:

Dim strInput As String, msgStrng As String, defStrng As String
Dim blDone As Boolean

defStrng = "P####  [enter digits for each '#']"
Do While Not blDone
    strInput = InputBox("Please enter product code", "Product Code input", defStrng)
    blDone = strInput Like "P####"
    If Not blDone Then
        Select Case True
            Case Len(strInput) <> 5
                msgStrng = "Product code should have five characters"
                defStrng = Left(strInput, 5)
            Case Left(strInput, 1) <> "P"
                msgStrng = "Product code should start with letter 'P'"
                defStrng = "P" & Left(strInput, 4)
            Case Else
                msgStrng = "last four characters of Product code should be digits"
                defStrng = strInput
            End Select

            MsgBox msgStrng, vbCritical
    Else
        MsgBox "Thank you"
    End If
Loop

Upvotes: 1

Rosario
Rosario

Reputation: 47

I have found the issue and fixed it with the following:

strInput = InputBox("Please enter product code")

Do
If Left(strInput, 1) = "p" Then
    If Len(strInput) = 5 Then
        If IsNumeric(Right(strInput, 4)) = True Then
            MsgBox "Thank You!"
            blDone = True
            Exit Do
        Else
            MsgBox "The last four characters should be digits"
            strInput = InputBox("Please enter product code")
        End If
    Else
        MsgBox "Product code should have five characters"
        strInput = InputBox("Please enter product code")
    End If
Else
    MsgBox "Product code should start with the letter P"
    strInput = InputBox("Please enter product code")
End If

Loop Until blDone = True

The problem was having declared these variables and having set them like this and used in the code.

intFrstLetter = InStr(1, strInput, "p")
intLastFour = Right(strInput, 4)
strFrstLetter = Left(strInput, 1)

After removing those variables from the code, the loop works as it should! Thank you everyone for your responses!

Upvotes: -1

Jens Bushrod-Roose
Jens Bushrod-Roose

Reputation: 11

Entering a new product code after an error doesn't change 3 out of 4 of your variables. Change the line strInput = InputBox("Please enter product code") to call ProductCode. This way your variables will be changed according to the new input.

To figure out these bugs yourself you can use "Step into" from the "debug" menu and mouse over your variables while you move through your code. Or alternatively, during the design phase of your program, display your variables after a piece of code you want to verify. Then delete these unnecessary lines when you're sure it works.

Upvotes: 1

Related Questions