Hi
I have a userform which has validated text box's, when trying to input invalid data a msg box appears.
Issue that i am having is that when i click my update command button and data is exported to my sheet it displays the all of the msg box messages for each text box. I presume that this is caused by the text box's being cleared after they have been copied to my excel sheet but I dont know how to fix it.
This is my Update command code
Private Sub CommandButton1_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("CVdata")
'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'check for a Name number
If Trim(Me.CVdate.Value) = "" Then
Me.CVdate.SetFocus
MsgBox "Please complete the form"
Exit Sub
End If
'copy the data to the database
ws.Cells(iRow, 1).Value = Me.Calendar1.Value
ws.Cells(iRow, 2).Value = Me.ComboBox1.Value
ws.Cells(iRow, 3).Value = Me.TextBox1.Value
ws.Cells(iRow, 4).Value = Me.TextBox2.Value
ws.Cells(iRow, 5).Value = Me.TextBox3.Value
ws.Cells(iRow, 6).Value = Me.TextBox4.Value
ws.Cells(iRow, 7).Value = Me.TextBox6.Value
ws.Cells(iRow, 8).Value = Me.TextBox8.Value
ws.Cells(iRow, 9).Value = Me.TextBox9.Value
ws.Cells(iRow, 10).Value = Me.TextBox7.Value
ws.Cells(iRow, 11).Value = Me.TextBox10.Value
MsgBox "Data added", vbOKOnly + vbInformation, "Data Added"
'clear the data
Me.CVdate.Value = ""
Me.ComboBox1.Value = ""
Me.TextBox1.Value = ""
Me.TextBox2.Value = ""
Me.TextBox3.Value = ""
Me.TextBox4.Value = ""
Me.TextBox6.Value = ""
Me.TextBox8.Value = ""
Me.TextBox9.Value = ""
Me.TextBox7.Value = ""
Me.TextBox10.Value = ""
Me.CVdate.SetFocus
End Sub
Private Sub CVdate_Change()
CVdate.Value = Format(CVdate.Value, "dd-mm-yyyy")
If Not IsDate(CVdate) Then CVdate = ""
If Not IsDate(Me.CVdate) Then
MsgBox "Select Date from Calender"
Cancel = True
End If
End Sub
This is the code i use to validate my textboxs
Private Sub TextBox2_Change()
Dim Num As Variant
Num = TextBox2.Value
If Not IsNumeric(TextBox2) Then TextBox2 = ""
If Not IsNumeric(Num) Then
MsgBox "You must enter a number"
Exit Sub
End If
End Sub
Have attached the spreadsheet as well
Please can you help?
Bookmarks