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!

Allowing only two / / for dates

Status
Not open for further replies.

KGB007

MIS
Jun 15, 2005
7
GB

Dear experts

Could you help me to write the vb code that only allows two // for date entry into the txtDateBox upon key_press?

The normal valid entry is dd/mm/yy.

Thanks in advance



 
Hi KGB007,

Try This:-

Private Sub txtDateBox_KeyPress(KeyAscii As Integer)

Dim vStringOccurs
Dim sText As String

'Assign Text in Textbox to String
sText = txtDateBox.Text

'Get Occurance of "/" using Ubound of Zero-based Array
vStringOccurs = UBound(Split(sText, "/"))

'Test Chr Pressed on Keyboard
Select Case KeyAscii
Case Asc("/")
If vStringOccurs > 1 Then KeyAscii = 0
Case Else
End Select

End Sub

Hope this helps,

Codefish
 
Try using the maskEditbox. Add the Microsoft Mask control to your toolbar. Place the maskEditbox on your form.

Set the Mask property to ##/##/####




This should help you

reidfl
 
You can also just check the value by using the IsDate() method. This will tell you if the entry is a valid date or not.
 
hai

masked edit method is a right method
and best method

shanmugham

shanmugham@hotmail.com

 
Hello Again,

The aim is to restrict the order of entry as dd/mm/yy.
If for example the first character entered is / then this will not register until the first two digits are entered. Then / can be accepted and so on. In the end only two / / is allowed without using IsDate() to check.

Thanks

KGB007
 
An easy algorithm to solve your problem creates a valid set and tests for inclusion in that set. It adjusts the set based on properties of the input.


For example:
[tt]
ValidList = "0123456789" ' Default to satify most cases
if 2 = len(InputData) or 5 = len(inputData) then ValidList = "/" ' Change for exceptions
if 0 = instr(1, ValidList, chr(KeyAscii), ) then KeyAscii = 0 ' toss out bad keypress
[/tt]
This does assume the code is in teh keypress event.

Of course this only assures that digits and characters fall where you want. To create the date algorithm you'll need to make it more complicated. For example, only digits 0, 1, 2, and 3 are valid when input length is 0. For input input value = "3", only 0 and 1 are valid. Get the idea?

Be careful, complex algorithms for setting the valid string can impact the user experience during data entry.


If you give a man a fish, you feed him for a day. When you teach him to fish, you feed his family a lot longer.

Wil Mead
wmead@optonline.net

 
Hi All,

Yes masked edit is probably the best method - however I don't like using the control mainly because of the "_/_/_" in the box.

All down to personal preference!

Codefish
 
Hi,

try to use Regular Expressions to create your one control, or use them to control the data of the TextBox.

You have to set the reference Microsoft VBScript Regular Expressions (vbscript.dll -> System32) at the project references.

Let's assume that you use the RE
Code:
"^[0-9]{2}\/[0-9]{2}\/[0-9]{2}$"
.

Then create a
Code:
VBScript_RegExp.RegExp
object and set the RE to the
Code:
Pattern
property:
Code:
      Dim objRE As VBScript_RegExp.RegExp

      Set objRE = New VBScript_RegExp.RegExp

      objRE.Pattern = "^[0-9]{2}\/[0-9]{2}\/[0-9]{2}$"
To test if the data on your TextBox is correct just use the Test method like this:
Code:
      If objRE.Test(txtDate.Text) Then
        Call MsgBox("Date is Correct.")
      Else
        Call MsgBox("Date is Incorrect.")
      End If
You can do a ActiveX Control that controls the date input, but that requires two distinct expressions (one for the change event and another for the final test) and other types of data control. If you want, I may e-mail you an example (let me know at brloureiro@hotmail.com).
regards,

Bruno Loureiro.

 
Hmm. I'm a big fan of Regular Expressions - but I can't help feeling that their use in this scenario is a slight bit of overkill...
 
Hi Bruno,

I am interested in your example.

Please, send one for me. I will be waiting for it.

My email: kgimb@yahoo.com.au

Many thanks

KGB007


 
Hi Strongm,

why is that?

I don't know the project that KGB007 is working with, but if that project uses txtDateBox in many forms, perhaps the better solution is to build an ActiveX control to deal with dates (by the way KGB007, why don't you use a control like DTPicker - Microsoft Windows Common Controls-2 6.0?).

For prevent the user to write more that two "/", there's no need for regular expressions, but if we want to control all the writing, like control that the user only write 2 digits, then a "/", then more two digits, another "/" and the two final digits, regular expressions are a good choice.

If we use a regular expression on the Change event, we can control the sequence of the writing, and using another regular expression for the final date format, we can grant that only a valid date is written in the control.

Clearly that it isn't the only way to do it, but my point is to give others the chance to know that regular expressions do the job. May be that's because the first time I heard about regular expressions, I had no idea of its capacities not even a common use for them. Now where's a valid example for using regular expressions.

May be you can e-mail me or post in the forum, others uses for regular expressions, at least I will appreciate it. regards,

Bruno Loureiro.

 
Dear Experts,

I am not doing any project at all. I am just practicing how to limit users making entry into textboxes.

For examples:
- a textbox that limits only digits 0 to 9, delete, backspace using Key_Press.
- a textbox that limits the format for amount eg 5.09
here allow ony digits 0 to 9 and a single dot, delete, backspace using Key_Press

and so on

But when I came to do the date, I am stuck and wondered on how to do it. That is all.

Thanks.

KBG007
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top