Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date and Time Validation 1

Status
Not open for further replies.

bebig

Technical User
Oct 21, 2004
111
US
Hi,
I am trying to write Date and Time Validation

This is Date and Time format-based local computer.

Text2.Text = Format(Now, "mm/dd/yyyy hh:mm:ss")

if I want to type date and time manually on Text2.Text.

How to validate Date and Time?

Thank you in advance.

 
Hi,

You could use Format and IsDate. Something like this:
Code:
Private Sub Text_Date_Validate(Cancel As Boolean)
  Text_Date = Format(Text_Date, "dd/mm/yyyy hh:mm:ss")
  If Not IsDate(Text_Date) Then
    MsgBox "This is not a valid date"
    Cancel = True
  End If
End Sub

Hope this helps

Harleyquinn

---------------------------------
For tsunami relief donations
 
Believe it or not but the preferred syntax is:
Code:
Private Sub Text_Date_Validate(Cancel As Boolean)
  Text_Date = Format(Text_Date, "dd/mm/yyyy hh:mm:ss")
  If IsDate(Text_Date) = False Then
    MsgBox "This is not a valid date"
    Cancel = True
  End If
End Sub

I love small animals, especially with a good brown gravy....
 
Curious: Why is that the "preferred" syntax? The other is clearer, and uses a boolean as a boolean. That's my preferred syntax.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
IsDate(Text_Date) is actually shorthand for IsDate(Text_Date) = True. so Not IsDate(Text_Date) is actually "Not IsDate(Text_Date) = True" redundant.. why not just use IsDate(Text_Date) = False ?

It's proper syntax. The problem with VB6 is that you can type the same code in several different ways.. People switching to VB.NET are confused because of bad habits.

_______
I love small animals, especially with a good brown gravy....
 
I agree with harleyquinn/tsdragon.

I was told (can't back it up) that doing a Not on a boolean is more efficient that doing an equal comparrison.

Anyone got something to back that up or prove it wrong?
 
I agree with harleyquinn/tsdragon.

I was told (can't back it up) that doing a Not on a boolean is more efficient that doing an equal comparrison.

Anyone got something to back that up or prove it wrong?

>>People switching to VB.NET are confused because of bad habits

I don't know what you mean by this? Is there something in .NET that would stop you from using the syntax as harleyquinn provided?
 
Sorry for the double-post. It was taking forever to submit and I clicked stop. Then checked to see if my post was there and it wasn't.... so I reposted and added the last question.
 
Omitting the " = true" from Not "IsNumeric(Text_Date) = True" does not mean that VB isn't processing it as such.

IsNumeric is a boolean function. It will only return a True or False. Leaving anything of is the same is " = true"


_______
I love small animals, especially with a good brown gravy....
 
>>does not mean that VB isn't processing it as such

do you have anything documentation to back this up?

In my own mind this is what steps the comipler would take to evaluate 'IsDate(Text_Date) = True'

1. Eval. IsDate(Text_Date)
2. True = True - Returns True
3. If True

Vs. my assumption of

If IsDate(Text_Date)
1. Eval. IsDate(Text_Date)
2. If True

I haven't looked for documentation for this (and really can't right now). If anyone has some I would be interested.
 
I suggest that it really doesn't matter what the compiler is doing. What really matters is execution speed, right? So here's a little test....

Private Sub Command1_Click()

Dim dStart As Double
Dim dMiddle As Double
Dim dEnd As Double
Dim bResult As Boolean

Dim iLoopCount As Long
Dim i As Long

iLoopCount = 1000000

dStart = Timer

For i = 1 To iLoopCount
If Not IsDate(Date) Then
bResult = True
Else
bResult = False
End If
Next


dMiddle = Timer

For i = 1 To iLoopCount
If IsDate(Date) = False Then
bResult = True
Else
bResult = False
End If
Next

dEnd = Timer

MsgBox ("Not IsDate: " & Round(dMiddle - dStart, 4) & vbCrLf & "= False: " & Round(dEnd - dMiddle, 4))

End Sub

When I run this on my computer, there is virtually no difference in time. Notice that I am looping a million times. The difference in time is VERY small especially compared to the size of the loop.

So, the differnce between not IsDate and IsDate = False is coding preferences. I prefer Not IsDate().

There is no right or wrong (in this case), only preferences.
 
gmmastros, bjd4jc, tsdragon

I've been watching this thread since I originally replied and I have to agree. I have always used Not IsDate() and that is my preference. I was going to do a speed comparison (thanks gmmastros for saving me having to do it [wink] ) but after reading the posts I think it would have been redundant for me to do it to also. Totally down to programmer preference.

pkailas - I see and understand what you are demonstrating. However I don't see how I have coded it as a >>"bad habit".

Harleyquinn

---------------------------------
For tsunami relief donations
 
Thanks gmmastros for taking the time to do the testout. Have a star on me.
 
In programming, using shortcuts is considered a "bad habit". I think everyone is guilty of it to some extent. Consistancy and readability are the main issues to stay away from shortucts. An example is:

rs("var") = "shortcut"
rs("var").value = "long hand"

while both are absolutely correct...

In vb.net, you can't use the first method anymore, I believe. I'm not 100% certain, but my few forays into converting my vb6 into vb.net definately had issues with the first example....

So, IMHO, using "shortcuts" is a bad habit!

Some others are even more rigid with their coding to use "Hungarian Notation" to the extreme.

bVar = boolean
strVar = string
intVar = integer
lngVar = long
etc....

Is this necessary? Absolutely NOT! Is it a good idea, IMHO ABSOLUTELY! If you are rather new or intermediate in your talents, it's a good time to start this way.

And I have had mixed results using "Not IsNunric(var)" and "Not IsDate(var)"

But I've never had a problem with "IsNumeric(var) = false" nor with "IsDate(var) = false".

And IMHO if some poor slob of a programmer every has to go through my code, there really is no confusion in what I'm testing for if you see "IsDate(var) = True" or "IsDate(var) = False".

The bottom line is, if it works for you, do it.

Of course, that's what makes this world such an "interesting" place.. our differences. Or as the Vulcan IDIC states.."Infinite Diversity Infinate Combinations"

--Peace Out.

_______
I love small animals, especially with a good brown gravy....
 
>>I have had mixed results using "Not IsNunric(var)" and "Not IsDate(var)"

Could you explain this more. What do you mean by 'mixed results'

I have the opposite opinion - it is easier for me to read... "If Not IsDate(Text)" than the other.

>>that's what makes this world such an "interesting" place

agreed
 
if some poor slob of a programmer every has to go through my code, there really is no confusion in what I'm testing for if you see "IsDate(var) = True" or "IsDate(var) = False".
IMO if the programmer isn't going to take the second or so it will take to notice what the Not in front of the IsDate means how much effort do you think they would put into going through the more complex code that most if not all applications involve...

Got to agree with you on the Hungarian Notation side of this though. That is very useful.
Also I second bjd4jc's agreement with you in regard to making the world an "interesting" place.

Harleyquinn

---------------------------------
For tsunami relief donations
 
The "Not IsDate(var)" problem I've had mainly has only occured in vbScripting and web pages. I never did figure out why I wouldn't get a consistent problem/result. But when I changed from "Not Isdate(var)" to "IsDate(var) = False" the problem never again occurred. I didn't do any other changes to the code other than that.

And readability to one isn't necessarily readable to another.

Is "IsDate(var) = false" confusing to you?
IMO if the programmer isn't going to take the second or so it will take to notice what the Not in front of the IsDate means how much effort do you think they would put into going through the more complex code that most if not all applications involve...

Have you ever taken over a project before with several hunder thousand lines of code? Missing little things line a "Not" is not uncommon.

I think we've beaten this subject to death now.

Bottom line, whatever you're comfortable with is the right way to do it as long as it works well. And if someone else has to take over your job, why make it easy for them? :)

_______
I love small animals, especially with a good brown gravy....
 
pkailas, you wrote...

rs("var") = "shortcut"
rs("var").value = "long hand"

Wouldn't the correct long hand be...

rs.Fields.Item("var").Value = "Long Hand
 
absolulely.. like I said.
In programming, using shortcuts is considered a "bad habit". I think everyone is guilty of it to some extent.

I never said I wasn't guilty of it!

smarty pants!

_______
I love small animals, especially with a good brown gravy....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top