Option Explicit
Private mBusy As Boolean
Private mSaved As Boolean

Private Sub UserForm_Initialize()
    Me.Caption = OrderText("title")
    txtItem.Value = ""
    txtItem.MaxLength = 200
    cboCategory.Clear
    cboCategory.Style = fmStyleDropDownList
    cboCategory.AddItem OrderText("office")
    cboCategory.AddItem OrderText("equipment")
    cboCategory.AddItem OrderText("service")
    cboCategory.ListIndex = 0
    chkUrgent.TripleState = False
    chkUrgent.Value = False
    chkUrgent.Caption = OrderText("urgent")
    btnSave.Caption = OrderText("save")
    btnCancel.Caption = OrderText("cancel")
End Sub

Private Sub btnSave_Click()
    Dim rowNumber As Long, receipt As Long, message As String
    Dim retrySafe As Boolean, result As Boolean
    If mBusy Or mSaved Then Exit Sub
    On Error GoTo UIError
    mBusy = True
    btnSave.Enabled = False
    result = TryAppendOrder(CStr(txtItem.Value), CStr(cboCategory.Value), _
        chkUrgent.Value, mSaved, rowNumber, receipt, message, retrySafe)
    mBusy = False
    If result Then
        MsgBox message, vbInformation, "Record written"
        Unload Me
    Else
        btnSave.Enabled = retrySafe
        MsgBox message, vbExclamation, "Check the input or record sheet"
    End If
    Exit Sub
UIError:
    mBusy = False
    btnSave.Enabled = False
    If mSaved Then
        MsgBox "The row was already committed. Do not save it again. Inspect the record sheet.", vbExclamation
    Else
        MsgBox "Form-state error. Inspect the record sheet before retrying: " & Err.Description, vbExclamation
    End If
End Sub

Private Sub btnCancel_Click()
    If mBusy Then Exit Sub
    Unload Me
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If mBusy Then Cancel = True
End Sub
