Dim ws As Worksheet Dim r As Long
Dim EventsEnable As Boolean
Const startRow As Long = 6
Private Sub ImageClose_Click()
'close the form (itself)
Unload Me
End Sub
Private Sub CloseUserForm_Click()
'close the form (itself)
Unload Me
End Sub
Private Sub ComboBoxCustomersNames_Change()
If Not EventsEnable Then Exit Sub
'get record
r = Me.ComboBoxCustomersNames.ListIndex + startRow - 1
Navigate Direction:=0
End Sub
Private Sub ComboBoxCustomersNames_Update()
With ComboBoxCustomersNames ' change as required
.RowSource = ""
.Clear
.List = ws.Range("A6:A" & ws.Range("A" & Rows.Count).End(xlUp).Row).Value
End With
End Sub
Private Sub DeleteRecord_Click()
Dim C As Range
With Sheets("DATABASE")
Set C = .Range("A:A").Find(What:=txtCustomer.Value, _
After:=.Range("A5"), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
End With
If Not C Is Nothing Then
If MsgBox("Are you sure you want to delete the record for " & txtCustomer.Text & "?", vbYesNo + vbCritical) = vbYes Then
Rows(C.Row).EntireRow.Delete
MsgBox "The record for " & txtCustomer.Text & " has been deleted!"
Else
MsgBox "The record containing customer " & txtCustomer.Text & " was not deleted!"
End If
Else
MsgBox "There were no records containing customer " & txtCustomer.Text & " to be deleted"
End If
Set C = Nothing
Unload Me
End Sub
Private Sub NewRecord_Click()
Dim i As Integer
Dim IsNewCustomer As Boolean
IsNewCustomer = CBool(Me.NewRecord.Tag)
Navigate Direction:=IIf(IsNewCustomer, xlNone, xlPrevious)
'if new customer, add Date
If IsNewCustomer Then
Me.txtJobDate.Text = Format(Date, "dd/mm/yyyy")
Me.txtCustomer.SetFocus
End If
ResetButtons IsNewCustomer
End Sub
Private Sub NextRecord_Click()
Navigate Direction:=xlNext
End Sub
Private Sub PrevRecord_Click()
Navigate Direction:=xlPrevious
End Sub
Private Sub txtBlankUsed_Change()
End Sub
Private Sub txtCustomer_Change()
End Sub
Private Sub txtVehicle_Change()
End Sub
Private Sub UpdateRecord_Click()
Dim C As Range
Dim i As Integer
Dim Msg As String
Dim IsNewCustomer As Boolean
'New Part
Dim ctrl As MSForms.Control
For Each ctrl In Me.Controls
If TypeOf ctrl Is MSForms.TextBox Then ctrl.BackColor = RGB(255, 255, 255)
Next ctrl
'End New part
If Me.NewRecord.Caption = "CANCEL" Then
With Sheets("DATABASE")
Set C = .Range("A:A").Find(What:=txtCustomer.Value, _
After:=.Range("A5"), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
End With
If Not C Is Nothing Then
MsgBox "Customer already Exists, file did not update"
Exit Sub
End If
End If
IsNewCustomer = CBool(Me.UpdateRecord.Tag)
Msg = "CHANGES SAVED SUCCESSFULLY"
If IsNewCustomer Then
'New record - check all fields entered
If Not IsComplete(Form:=Me) Then Exit Sub
r = startRow
Msg = "NEW CUSTOMER SAVED TO DATABASE"
ws.Range("A6").EntireRow.Insert
ResetButtons Not IsNewCustomer
Me.NextRecord.Enabled = True
End If
On Error GoTo myerror
Application.EnableEvents = False
'Add / Update Record
For i = 1 To UBound(ControlNames)
With Me.Controls(ControlNames(i))
'check if date value
If IsDate(.Text) Then
ws.Cells(r, i).Value = DateValue(.Text)
ElseIf i = 15 Then
ws.Cells(r, i).Value = CDbl(.Text)
Else
ws.Cells(r, i).Value = UCase(.Text)
End If
ws.Cells(r, i).Font.Size = 11
End With
Next i
If IsNewCustomer Then
Call ComboBoxCustomersNames_Update
Range("A6:P6").Interior.ColorIndex = 6
With Sheets("DATABASE")
If .AutoFilterMode Then .AutoFilterMode = False
x = .Cells(.Rows.Count, 1).End(xlUp).Row
.Range("A5:P" & x).Sort Key1:=.Range("A6"), Order1:=xlAscending, Header:=xlGuess
Range("A6:P6").Borders.LineStyle = xlContinuous
Range("A6:P6").Borders.Weight = xlThin
End With
End If
ThisWorkbook.Save
'tell user what happened
MsgBox Msg, 48, Msg
Set C = Nothing
myerror:
Application.EnableEvents = True
'something went wrong tell user
If Err > 0 Then MsgBox (Error(Err)), 48, "Error"
Unload Me
End Sub
Sub ResetButtons(ByVal Status As Boolean)
With Me.NewRecord
.Caption = IIf(Status, "CANCEL", "ADD NEW CUSTOMER TO DATABASE")
.BackColor = IIf(Status, &HFF&, &H8000000F)
.ForeColor = IIf(Status, &HFFFFFF, &H0&)
.Tag = Not Status
Me.ComboBoxCustomersNames.Enabled = CBool(.Tag)
End With
With Me.UpdateRecord
.Caption = IIf(Status, "SAVE NEW CUSTOMER TO DATABASE", "SAVE CHANGES FOR THIS CUSTOMER")
.Tag = Status
End With
End Sub
Private Sub Userform_Initialize()
Set ws = ThisWorkbook.Worksheets("Database")
ComboBoxCustomersNames_Update
ResetButtons False
'start at first record
Navigate Direction:=xlFirst
End Sub
Sub Navigate(ByVal Direction As XlSearchDirection)
Dim i As Integer
Dim LastRow As Long
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
r = IIf(Direction = xlPrevious, r - 1, r + xlNext)
'ensure value of r stays within data range
If r < startRow Then r = startRow
If r > LastRow Then r = LastRow
'get record
For i = 1 To UBound(ControlNames)
Me.Controls(ControlNames(i)).Text = IIf(Direction = xlNone, "", ws.Cells(r, i).Text)
Next i
Me.Caption = "Database"
'set enabled status of next previous buttons
Me.NextRecord.Enabled = IIf(Direction = xlNone, False, r < LastRow)
Me.PrevRecord.Enabled = IIf(Direction = xlNone, False, r > startRow)
EventsEnable = False
Me.ComboBoxCustomersNames.ListIndex = IIf(Direction = xlNone, -1, r - startRow)
EventsEnable = True
End Sub