everyColorDumb
New Member
- Joined
- Jan 11, 2022
- Messages
- 5
- Office Version
- 2016
- Platform
- Windows
Hello there!
Currently, I have a Userform with a textfield that is validated once the "Submit" button is clicked. There are two possible outcomes: 1) The textfield value is valid and the form closes. 2) The textfield is not valid, and the user must continue filling or editing the form. Scenario 2 is where my bug lies. My intention with the 'Do...Loop Until isClicked = True' line was to have the program not check values until the user has clicked "Submit" again. Otherwise, it would be in this infinite loop of generating the error MsgBox and never actually give the user a chance to edit that field.
P.S This is my first post here, so please feel free to provide feedback on best practices or how I can better layout the problem. Many thanks in advance!!!
Currently, I have a Userform with a textfield that is validated once the "Submit" button is clicked. There are two possible outcomes: 1) The textfield value is valid and the form closes. 2) The textfield is not valid, and the user must continue filling or editing the form. Scenario 2 is where my bug lies. My intention with the 'Do...Loop Until isClicked = True' line was to have the program not check values until the user has clicked "Submit" again. Otherwise, it would be in this infinite loop of generating the error MsgBox and never actually give the user a chance to edit that field.
P.S This is my first post here, so please feel free to provide feedback on best practices or how I can better layout the problem. Many thanks in advance!!!
VBA Code:
Dim isClicked As Boolean
Dim isValid As Boolean
Private Sub UserForm_Initialize()
'Set Submit button as unclicked
isClicked = False
'Empty BarcodeTextBox
BarcodeTextBox.Value = ""
'Empty Conc Unit ComboBox
ConcUnitComboBox.Clear
'Empty Vol Unit ComboBox
VolUnitComboBox.Clear
'Fill CityListBox
With ConcUnitComboBox
.AddItem "ug/mL"
.AddItem "ug/uL"
.AddItem "mg/mL"
End With
'Fill VolUnitListBox
With VolUnitComboBox
.AddItem "uL"
.AddItem "mL"
.AddItem "L"
End With
End Sub
Private Sub SubmitButton_Click()
isClicked = True
Debug.Print "Submit Button clicked --> isClicked Value: ", isClicked
Dim NumDestPlates As Integer
Dim isValid As Boolean: isValid = False
Dim BarcodeTextString As String
Dim BarcodeArray() As String
Dim NumBarcodes As Integer
Dim LandingSheet As Worksheet
Dim RunSheet As Worksheet
Set LandingSheet = ThisWorkbook.Sheets("FormSheet")
Set RunSheet = ThisWorkbook.Sheets("Run Info")
Call CheckForm(RunSheet, LandingSheet)
'Will only proceed to this if validated'
Unload Me
End Sub
Private Sub CheckForm(ByRef RunInfo As Worksheet, ByRef FormInfo As Worksheet)
Dim formImport As New UserForm2
Dim isComplete As Boolean: isComplete = False
Dim BarcodeTextString As String
Do
CheckBeginning:
Debug.Print "Checking the form..."
BarcodeTextString = CStr(BarcodeTextBox.Value)
'Remove all whitespaces in Barcode Input and covert all chars to uppercase
BarcodeTextString = UCase(BarcodeTextString)
BarcodeTextString = Replace(BarcodeTextString, " ", "")
'Parse Barcode String
BarcodeArray = Split(BarcodeTextString, ",")
'Activate Runsheet
RunInfo.Activate
'Check that number of submitted barcodes equals number of destination plates
NumDestPlates = WorksheetFunction.Max(Range("I3", Range("I3").End(xlDown)))
UserNumBarcodes = UBound(BarcodeArray) - LBound(BarcodeArray) + 1
'Number of user input barcodes must match number of dest plates
If NumDestPlates = UserNumBarcodes Then
isValid = True
Debug.Print "isValid:", isValid
MsgBox ("Number of barcodes match!")
Exit Do
Else
isClicked = False
Debug.Print "isClicked Value: ", isClicked
MsgBox ("Number of barcodes don't match! Please check and try again.")
BarcodeTextBox.SetFocus
Debug.Print "isClicked Value: ", isClicked
Do
'Don't do anything until user clicks Submit button again
Loop Until isClicked = True
GoTo CheckBeginning
End If
Loop Until isComplete = True
End Sub