Link to home
Start Free TrialLog in
Avatar of hxb
hxb

asked on

How to input date and time quickly

VB6, win98

I want to let user input date and
time in textbox,
format is "YYYY-MM-DD HH:MM:SS"
near the textbox, I set a command button,
and let user click on it and pop
up a form to input Date and time
using the Mouse.
I think the setting date and time
in win98 (double click the time at
right and down of the win98) is ok,

Can anybody give me a sample code
about this, let user to input date
and time quickly?

Avatar of brice123
brice123

Why don't you use the Datetime picker control provided with vb6?

Components -> Microsoft Windows Common Controls-3 6.0 (COMCT332.OCX)
You can do what you asked for using this code:

'I assumed the user will type the date and time in TextBox Text1
Dim strTime As String
strTime = Format(Text1, "YYYY-MM-DD HH:MM:SS")
TheMask, I think his problem is that he wants to validate the date/time entered before it hits the system.

Myself, for dates I pop the M$Calendar control when the user enters a date field on the screen. Then they can just click on the date and what is returned is automatically both a valid date and correctly formatted.

Assume we have a field called (in this example) ArrestDate. Then there are two event support routines that I need code for:

Private Sub ArrestDate_Click()
'
' Here when operator clicks on the date field.
'
NoFill = True
Me.Hide     'Vanish
DateFinder.Show vbModal     'Show date picker and wait
ArrestDate.Text = Format(DateFinder.When.Value, "dd-mmm-yyyy")    'Get date from picker and format
Me.Show     'Reappear
'
End Sub

Private Sub ArrestDate_KeyPress(KeyAscii As Integer)
'
' Fields are locked to prevent editing...
'
Select Case Chr(KeyAscii)
    Case vbCr, " "       'Treat <Enter> same as click
        Call ArrestDate_Click
        '
End Select
'
End Sub


Now you'll notice a reference to a DateFinder form. This is a plain form with just the M$Calendar control on it. The form is called DateFinder and the calendar control is called When. This is *all* the code on THAT form:

'
' Form to allow selection of dates
' Part of BOND-O application
' Copyright 2000 - Networking Experts, Inc. - All Rights Reserved
' By: Mark M. Lambert on February 28, 2000
'
' V1.0 - 28 Feb 00 - MML - Initial Code
'
Option Explicit

Private Sub Form_Activate()
'
' Default to today when shown
'
When.Value = Date
'
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
'
' Ignore clicks in [X]
'
If UnloadMode <> vbFormCode Then Cancel = True
'
End Sub

Private Sub When_Click()
'
' Vanish after selection is made
'
Me.Hide
'
End Sub

So this pops a calendar on the screen as then enter the date field and returns a properly formatted, legitimate date.

Now you could do essentially the same thing with the time or you can have a set of combo boxes, one with hours, one with minutes. Again this prevents the operator from having to key in your format or from entering garbage into the system (30-Feb, etc.) - Not only that, but it looks cool!

M
TheMask changed the proposed answer to a comment
I just want to add a suggestion.

If you are using VB5/6, why do you not use the Mask control (MSMASK32.OCX)?  

With this control, you can pre-format the input.  Meaning, place a mask of HH:MM:SS into one, place YY-MM-DD into the other.

The user will get something like so:
__:__:__    __-__-__

After completion (Lostfocus) test it using the Isdate(sTIME) = True and IsDate(sDATE) = true to verify they enter valid time and date formats.

This way the user doesn't have to enter the dash (-) or the colons (:) - they are already present.

Hope this helps!  Good luck
The best that the mask control can do is enter the -'s or /'s or :'s for you. It simply isn't smart enough to make sure the dates/times are valid.

While your suggestion of using isdate is valid, I think that it's conceptually incorrect to allow the operator to make an error and nag instead of just preventing the error to begin with.

If you pop a calendar control or have a list of hours/minutes then it's simply not possible for the operator to enter garbage data. This simplifies your code as you can go ahead assured that the data is clean and you don't have to put error checking in if the errors simply cannot occur. You'll find that user resistence and complaints will be reduced if the system prevents the errors from occurring instead of allowing them to make the error and them complaining to them.

FWIW

M
I never mentioned the smarts of the mask control - I mentioned the purpose (it is a formatted container).  People like to have an idea that ohhh time belongs here and date belongs there.  The programmer can have simple and small code to verify valid date and time - if it is invalid, clear the text and set focus to it.  Where is the nag in that?

It is all a matter of opinion.  Some people (like myself) do not like to have a list of 60 items to choose from (your suggestion of time).

I like your idea about the date picker, but I would never have a combo box filled with 24 hours and 60 minutes to choose from.

hxb - Take and combine any of the suggestions here.  
Actually the way I do time is with a slider. You can scroll up and down until the time shows what you want - no 60 item drop down list.

"if it is invalid, clear the text and set focus to it. Where is the nag in that?"

The nag is that you're rejecting the operators input and making them try again until they get it 'right'. If you refuse to accept invalid input initially the operator only enters it once - correctly.

Got a program around here that someone else wrote. Data entry screen with 1/2 dozen or so assorted fields and a [Next] button. [Next] is enabled all the time. If you click on it without any entry it pops an error dialog "You must enter this" and puts you back. So you enter 'this' and try again, it responds "You must enter that' and puts you back. So you enter 'that' and try again and it responds "You must enter something else..." this cycle goes on for about six fields. How much less frustrating it would be if the !@#$%^ [Next] button didn't *enable* until AFTER all the inputs are correct?

Your program should never lie to a user - don't show that some function is available when it's really not. Don't force the user to double/triple enter things until they get it right - guide them, gently, to the correct values. Don't assume that they understand that something is implied.

I spend a *LOT* of time on "human factors" and the difference between the two approaches outlined above is often the difference between a usable and unusable program.

M
I agree with you fully about nagging the user.  I try my best to never plop a message box upon the screen, if I must send some info (or provide guidence) - I do it within the statusbar or within a label.

But, once again, I feel as a programmer you must know your clients.  Are these kids playing games or are they data entry folks (who would rather use the keyboard anyday over grabbing the mouse)??

Some people don't like to switch back and forth from the keyboard and mouse.  I have spent time dealing with people factors also.

All this web page is for, is to provide solutions to problems.  When it comes to programming, there is usually ten different ways to accomplish the same thing.  I am just giving hxb other suggestion to try - not downplaying anybody's idea.  It is up to hxb to determine the proper solution for his/her users.
Agreed, different strokes for different folx. I don't like to make the user hop back and forth from the mouse to the keyboard either. Slows data entry rates way down, but if you can "click-n-pick" instead of having to manually enter esp. where you're concerned about formatting you should do so.

m
Avatar of hxb

ASKER

Thank for your help.

brice123,
  where is the the Datetime picker control in vb6?

Components -> Microsoft Windows Common Controls-3 6.0 (COMCT332.OCX)  SP3
only one control:  coolbar

what my crital question is  input
date and time quickly,
not the format of the date and time.
The pop up form can return the
date and time, I will check the
date and time, and change to the
correct format.
ASKER CERTIFIED SOLUTION
Avatar of brice123
brice123

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hxb

ASKER

Thank everybody,
but Brice123  is good.
I have completed the code.