TextBox will not clear date???????????

T

Tdp

THi,
I have a Textbox when entered a date, it adds 90 days and 120 days to that
date and enteres these dates to two more textboxes.
The trouble I am having is when a date is entered in error in the first
Textbox I can not clear it. The cursor freezes. Any ideas?
The code I am using is as follows:

Private Sub TextBox36_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
TextBox36.Value = Format(TextBox36.Value, "dd-mmm-yy")
If IsDate(Me.TextBox36.Value) Then
Me.TextBox37.Value _
= Format(CDate(Me.TextBox36.Value) + 90, "dd-mmm-yy")
Me.TextBox38.Value _
= Format(CDate(Me.TextBox36.Value) + 120, "dd-mmm-yy")
Else
Beep
Cancel = True
End If
End Sub

Tdp
 
T

Tdp

The codes I have are as follows:

To insert the dates in data sheet:

LastRow.Offset(1, 48).Value = TextBox36.Text
LastRow.Offset(1, 49).Value = TextBox37.Text
LastRow.Offset(1, 50).Value = TextBox38.Text

To look up data from data sheet:

Me.TextBox36.Value = Format(FoundCell.Offset(0, 48).Value, "dd-mmm-yy")
Me.TextBox37.Value = Format(FoundCell.Offset(0, 49).Value, "dd-mmm-yy")
Me.TextBox38.Value = Format(FoundCell.Offset(0, 50).Value, "dd-mmm-yy")

Else
Me.TextBox36.Value = ""
Me.TextBox37.Value = ""
Me.TextBox38.Value = ""

If you need more of the code let me know.
Thank you
 
T

Tdp

Thats correct. I included it in with TextBox36 as follows:

Private Sub TextBox36_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
TextBox36.Value = Format(TextBox36.Value, "dd-mmm-yy")
If IsDate(Me.TextBox36.Value) Then
Me.TextBox37.Value _
= Format(CDate(Me.TextBox36.Value) + 90, "dd-mmm-yy")
Me.TextBox38.Value _
= Format(CDate(Me.TextBox36.Value) + 120, "dd-mmm-yy")
Else
Beep
Cancel = True
End If
End Sub

The code (cut down) looks like this:

Private Sub CommandButton1_Click()
Dim LastRow As Range
Dim iRow As Long
Dim FirstRow As Long
Dim LastRowNumber As Long
Dim wks As Worksheet
Application.DisplayAlerts = False
Application.EnableEvents = False

Set LastRow = Sheet2.Range("a100").End(xlUp)
LastRow.Offset(1, 48).Value = TextBox36.Text
LastRow.Offset(1, 49).Value = TextBox37.Text
LastRow.Offset(1, 50).Value = TextBox38.Text
MsgBox ("Data has been entered")
For Each wks In Worksheets(Array("customers"))
With wks
FirstRow = 2 'headers in row 1
'Changed LastRow to LastRowNumber
'Dim LastRowNumber as Long added above
'LastRow is declared as Range
'We need it to be Long
LastRowNumber = .Cells(.Rows.Count, "A").End(xlUp).Row

For iRow = FirstRow To LastRowNumber Step 1
If Application.CountIf(.Range("a2").EntireColumn, _
.Cells(iRow, "A").Value) > 1 Then
'it's a duplicate
MsgBox .Cells(iRow, "A").Value
.Rows(iRow).Delete
End If
Next iRow
End With
Next wks
Application.EnableEvents = True
Application.DisplayAlerts = True
End Sub

Private Sub CommandButton2_Click()
'Search button
Dim FoundCell As Range
Application.DisplayAlerts = False
Application.EnableEvents = False

If Me.ComboBox1.ListIndex = -1 Then
'nothing filled in
Beep
Exit Sub
End If
With Worksheets("customers").Range("A:A")
Set FoundCell = .Cells.Find(what:=Me.ComboBox1.Value, _
After:=.Cells(1), _
LookIn:=xlValues, _
lookat:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
End With
If FoundCell Is Nothing Then
'this shouldn't happen!
Beep
Else
Me.TextBox1.Value = FoundCell.Offset(0, 0).Value
Me.TextBox3.Value = FoundCell.Offset(0, 2).Value
If IsDate(FoundCell.Offset(0, 1).Value) Then
Me.TextBox36.Value = Format(FoundCell.Offset(0, 48).Value, "dd-mmm-yy")
Me.TextBox37.Value = Format(FoundCell.Offset(0, 49).Value, "dd-mmm-yy")
Me.TextBox38.Value = Format(FoundCell.Offset(0, 50).Value, "dd-mmm-yy")
Else
Me.TextBox36.Value = ""
Me.TextBox37.Value = ""
Me.TextBox38.Value = ""
End If


Application.EnableEvents = True
Application.DisplayAlerts = True

End Sub

Private Sub TextBox36_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
TextBox36.Value = Format(TextBox36.Value, "dd-mmm-yy")
If IsDate(Me.TextBox36.Value) Then
Me.TextBox37.Value _
= Format(CDate(Me.TextBox36.Value) + 90, "dd-mmm-yy")
Me.TextBox38.Value _
= Format(CDate(Me.TextBox36.Value) + 120, "dd-mmm-yy")
Else
Beep
Cancel = True
End If
End Sub

Obviously there are alot more textboxes but that is the general outline of
the code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Textbox date formatting 14
Textbox font colour change? 6
Textbox date format. 2
Compile error:.......?? 1
code crashing my programme??????? 4
Please check my code!!!! 1
Date format textbox 2
date format problem 8

Top