Greetings, below is a code that I am using on a userform and it works just fine. Basically it is being used to find equipment that needs inspection, and pass those inspection values onto a WB. The boss just asked me to add in an additional function where if the device does not pass inspection (so the "Fail" button gets clicked) then a second userform is generated to create a work order for repairs. That part is very straight forward and I have that in place (in this sub it is referred to as "CorrectiveActionForm").
The issue that I am having is that if the user inputs the wrong DeviceId, I get the messagebox telling them this, but the sub continues to run and it continues on and launches the corrective action form. What I would rather have it do is just go back to the beginning of the sub and start over (I think - lol) I have to leave the initial form open because I cycle it over and over as well as pass information from this form to the CA form, so I can't just close it and relaunch. And I don't want the user to have any other option on the messagebox other than "OK" so I think that I have to stick with that style of messagebox.
Is there a string of code that I can use (I guess right after the messagebox?) that will either stop the sub from running or restart it and go back to the beginning?
I am probably doing this completely wrong in the first place :/ I appreciate any input - thanks, Rick
The issue that I am having is that if the user inputs the wrong DeviceId, I get the messagebox telling them this, but the sub continues to run and it continues on and launches the corrective action form. What I would rather have it do is just go back to the beginning of the sub and start over (I think - lol) I have to leave the initial form open because I cycle it over and over as well as pass information from this form to the CA form, so I can't just close it and relaunch. And I don't want the user to have any other option on the messagebox other than "OK" so I think that I have to stick with that style of messagebox.
Is there a string of code that I can use (I guess right after the messagebox?) that will either stop the sub from running or restart it and go back to the beginning?
I am probably doing this completely wrong in the first place :/ I appreciate any input - thanks, Rick
Code:
Private Sub FailButton_click()
Dim Found As Range
Dim i As Long
Dim Lastrow As Long
'This string finds the device that you are inspecting on the WS, confirms that you have the correct one,
'that the bar code was read correctly, and enters in the inspection observations for that device.
If Me.TextBox1.Value = "" Then
MsgBox "Emergency Light ID was not read, rescan and try again. ", , "Rescan and try again"
Else
Set Found = Sheets("Emergency Lighting Log").Range("L:L").Find(What:=Me.TextBox1.Value, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Found Is Nothing Then
MsgBox "No match for " & Me.TextBox1.Value, , "No Match Found"
[COLOR=#ff0000] ' I think that this is where I want the sub to "restart". [/COLOR]
Else
Found.Offset(0, 7).Value = Me.TextBox2.Value
Found.Offset(0, 3).Value = Now()
Found.Offset(0, 8).Value = "FAIL"
If CheckBox1.Value = True Then
Found.Offset(0, 4) = "X"
End If
If CheckBox2.Value = True Then
Found.Offset(0, 5) = "X"
End If
If CheckBox3.Value = True Then
Found.Offset(0, 6) = "X"
End If
End If
End If
'CheckBox1.Value = False
'CheckBox2.Value = False
'CheckBox3.Value = False
'TextBox1.Value = ""
'TextBox2.Value = ""
'This string gives me the remaining amount of devices left to be inspected.
Me.TextBox4.Text = CStr(ThisWorkbook.Sheets("Emergency Lighting Log").Range("M1").Value)
'This string gives me the device names of the remaining inspections.
ListBox1.Clear
Lastrow = Cells(Rows.Count, "L").End(xlUp).Row
For i = 1 To Lastrow
If Cells(i, 15).Value = "" Then ListBox1.AddItem Cells(i, 12).Value
Next i
TextBox1.SetFocus
CorrectiveActionForm.Show
End Sub