ipbr21054
Well-known Member
- Joined
- Nov 16, 2010
- Messages
- 5,832
- Office Version
- 2007
- Platform
- Windows
Hi,
The code below has a message that pops up saying Msg = "CHANGES SAVED SUCCESSFULLY"
It has OK on it & when i click it the message closes & i am then back to the form.
Please can you advise how when i click OK not only does the message box close but also the form.
A separate button on the form has a code to close the form as supplied below but im not sure how to add it to my large code below to close it.
The code below has a message that pops up saying Msg = "CHANGES SAVED SUCCESSFULLY"
It has OK on it & when i click it the message closes & i am then back to the form.
Please can you advise how when i click OK not only does the message box close but also the form.
A separate button on the form has a code to close the form as supplied below but im not sure how to add it to my large code below to close it.
Code:
Private Sub CloseUserForm_Click() 'close the form (itself)
Unload Me
End Sub
Code:
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"
End Sub