Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Dates in TextBoxes

Hi

I have a Form with a TextBox in which a user is supposed
to enter a date, to be used in a calculation later on.
This is giving me a couple of headaches:

(a) How do I go about providing validation for the
TextBox.Value? Currently I have a line that looks like:

If TextBox.Value = 1/04/2004 And _
TextBox.Value <= 31/3/2005 Then

This doens't work, since something like 1/04/02 returns
TRUE for the above If statement

(b) When I use the TextBox.Value to update a cell on my
worksheet, the value returned is a text value, ie. can't
use it in the calculation. What am I doing wrong?

Thanks in advance,

James
(new to VBA)
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Dates in TextBoxes

dim dt as Date
Dim sStr as String
sStr = Textbox1.Text
if isdate(sStr) then
dt = cDate(sStr)
ActiveCell.Value = dt
if dt = #4/1/2004# and dt <=#3/31/2005" then
ActiveCell.Formula = "dd/mm/yyyy"
Else
msgbox "Out of bound"
end if
Else
msgbox "Not a date"
End if

Generally in VBA, you should work with US formatted dates. Cdate should
convert (interpret) the string to a date serial consistent with your
regional settings.

--
Regards,
Tom Ogilvy

"James" wrote in message
...
Hi

I have a Form with a TextBox in which a user is supposed
to enter a date, to be used in a calculation later on.
This is giving me a couple of headaches:

(a) How do I go about providing validation for the
TextBox.Value? Currently I have a line that looks like:

If TextBox.Value = 1/04/2004 And _
TextBox.Value <= 31/3/2005 Then

This doens't work, since something like 1/04/02 returns
TRUE for the above If statement

(b) When I use the TextBox.Value to update a cell on my
worksheet, the value returned is a text value, ie. can't
use it in the calculation. What am I doing wrong?

Thanks in advance,

James
(new to VB



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Dates in TextBoxes

James,
I suggest you convert the input string into a date
using the DateValue function and appropriate error handling
to cope with invalid input.

Consider the test subroutine below. Ive used an inputbox
rather than a form to get the date, but it is similar to
what you describe. You probably can use the same logic to
validate the entry in your form.

Regards
Jacques
================================================== ========

Sub Test()
Dim rrr As Long
Dim xxx As String
Dim DDD As Date

On Error GoTo ErrHdlr
xxx = InputBox("Enter date ")
rrr = 1
DDD = DateValue(xxx)
If DDD < #4/1/04# Then
rrr = 2
GoTo ErrHdlr
ElseIf DDD #3/31/05# Then
rrr = 3
GoTo ErrHdlr
End If
On Error GoTo 0
GoTo OKContinue

ErrHdlr:
MsgBox Choose(rrr, "Invalid Date format", _
"Date is too Old", _
"Date is too far") & " : " & xxx

Exit Sub

OKContinue:
MsgBox "Good date " & xxx
End Sub

-----Original Message-----
Hi

I have a Form with a TextBox in which a user is supposed
to enter a date, to be used in a calculation later on.
This is giving me a couple of headaches:

(a) How do I go about providing validation for the
TextBox.Value? Currently I have a line that looks like:

If TextBox.Value = 1/04/2004 And _
TextBox.Value <= 31/3/2005 Then

This doens't work, since something like 1/04/02 returns
TRUE for the above If statement

(b) When I use the TextBox.Value to update a cell on my
worksheet, the value returned is a text value, ie. can't
use it in the calculation. What am I doing wrong?

Thanks in advance,

James
(new to VBA)
.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 391
Default Dates in TextBoxes

Dim textboxdate As Date
Dim firstdate As Date
Dim seconddate As Date

firstdate = DateSerial(2004, 4, 1)
seconddate = DateSerial(2005, 3, 31)

If IsDate(TextBox.Text) Then
textboxdate = CDate(TextBox.Text)

If textboxdate = firstdate _
And textboxdate <= seconddate Then

' process
TextBox1.Text = "ok"

End If

End If


note...date literals are entered this way
date1 = #1/4/04#
this is "dangerous" since often the date style is
American...try it
enter a line like
date1 = #1-apr-2004#
and the compiler wil change this
#4/1/04#
while in the uK I'd expect #1/4/04#
hence the use of the dateserial function. It's
unambiguous!


Patrick Molloy
Microsoft Excel MVP

-----Original Message-----
Hi

I have a Form with a TextBox in which a user is supposed
to enter a date, to be used in a calculation later on.
This is giving me a couple of headaches:

(a) How do I go about providing validation for the
TextBox.Value? Currently I have a line that looks like:

If TextBox.Value = 1/04/2004 And _
TextBox.Value <= 31/3/2005 Then

This doens't work, since something like 1/04/02 returns
TRUE for the above If statement

(b) When I use the TextBox.Value to update a cell on my
worksheet, the value returned is a text value, ie. can't
use it in the calculation. What am I doing wrong?

Thanks in advance,

James
(new to VBA)
.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Textboxes SAL Excel Discussion (Misc queries) 2 July 13th 07 12:24 AM
tab between several textboxes Kim Excel Worksheet Functions 0 May 9th 05 04:08 PM
Textboxes Marcotte A Excel Programming 2 July 7th 04 12:55 AM
textboxes libby Excel Programming 5 April 13th 04 06:32 PM
userform textboxes Jo[_6_] Excel Programming 4 October 21st 03 07:15 PM


All times are GMT +1. The time now is 12:41 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"