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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Entering date in DTPicker control 4

Status
Not open for further replies.

EwS

Programmer
Dec 30, 2002
398
0
0
US
I want to be able to highlight the whole field, enter "050803", and my input to be validated.

Problems with DTPicker:
1) It highlights only mm.
2) If I type "050803", it writes to the mm field 3 times. So, I need to enter 5, then press the right arrow (to get to dd), enter 8, then press the right arrow (to get to yy), and enter 3.

Is there a way for the control to except the input as I type and put it in the appropriate fields so that I don't have to press the arrows? If not, is it possible with any other control, such as maskededit control?

Thank you.
 
The mask edit control still has some limitations. Here is some code that used for a text box a couple of years ago. I gives a basic mask for the user to follow and allows you to type through quickly Basic validation is done in the lost focus event and would probably be better in the validation event. If it helps use the code and /or make adjustments as you wish.

Option Explicit

Private m_intDateEntered As Integer
Private m_bolDateDirty As Boolean

Private Sub txtDate_Change()
m_bolDateDirty = True
End Sub

Private Sub txtDate_GotFocus()
m_intDateEntered = 0
m_bolDateDirty = False
End Sub

Private Sub txtDate_KeyPress(KeyAscii As Integer)
If KeyAscii >= 48 And KeyAscii <= 57 Then ' numbers only
Select Case m_intDateEntered
Case 0
txtDate.Text = &quot;mmddyy&quot;
txtDate.SelStart = 0
txtDate.SelLength = 1
Case 1
txtDate.Text = Left(txtDate.Text, 1) & &quot;mddyy&quot;
txtDate.SelStart = 1
txtDate.SelLength = 1
Case 2
txtDate.Text = Left(txtDate.Text, 2) & &quot;ddyy&quot;
txtDate.SelStart = 2
txtDate.SelLength = 1
Case 3
txtDate.Text = Left(txtDate.Text, 3) & &quot;dyy&quot;
txtDate.SelStart = 3
txtDate.SelLength = 1
Case 4
txtDate.Text = Left(txtDate.Text, 4) & &quot;yy&quot;
txtDate.SelStart = 4
txtDate.SelLength = 1
Case 5
txtDate.Text = Left(txtDate.Text, 5) & &quot;y&quot;
txtDate.SelStart = 5
txtDate.SelLength = 1
Case 6
'// Do nothing.
End Select
m_intDateEntered = m_intDateEntered + 1
ElseIf KeyAscii = 8 Then '// Backspace key.
m_intDateEntered = m_intDateEntered - 1
Select Case m_intDateEntered
Case 0
txtDate.Text = Left(txtDate.Text, 1) & &quot;mmddyy&quot;
txtDate.SelStart = 0
txtDate.SelLength = 1
Case 1
txtDate.Text = Left(txtDate.Text, 2) & &quot;mddyy&quot;
txtDate.SelStart = 1
txtDate.SelLength = 1
Case 2
txtDate.Text = Left(txtDate.Text, 3) & &quot;ddyy&quot;
txtDate.SelStart = 2
txtDate.SelLength = 1
Case 3
txtDate.Text = Left(txtDate.Text, 4) & &quot;dyy&quot;
txtDate.SelStart = 3
txtDate.SelLength = 1
Case 4
txtDate.Text = Left(txtDate.Text, 5) & &quot;yy&quot;
txtDate.SelStart = 4
txtDate.SelLength = 1
Case 5
txtDate.Text = Left(txtDate.Text, 6) & &quot;y&quot;
txtDate.SelStart = 5
txtDate.SelLength = 1
Case 6
'// Do nothing.
End Select
Else
KeyAscii = 0
Beep
End If
End Sub

Private Sub txtDate_LostFocus()
If m_bolDateDirty Then
If Valid_DateNumbers(txtDate.Text) Then
If Left(txtDate.Text, 1) = &quot;0&quot; Then
txtDate.Text = Right(txtDate.Text, Len(txtDate.Text) - 1)
txtDate.Text = Left(txtDate.Text, 1) & &quot;/&quot; & Mid(txtDate.Text, 2, 2) & &quot;/&quot; & Mid(txtDate.Text, 4, 2)
Else
txtDate.Text = Left(txtDate.Text, 2) & &quot;/&quot; & Mid(txtDate.Text, 3, 2) & &quot;/&quot; & Mid(txtDate.Text, 5, 2)
End If

Else
MsgBox &quot;The date was entered incorrectly.&quot; & vbCrLf & &quot;Re-enter the date in &quot; & Chr(34) & &quot;mmddyy&quot; & Chr(34) & &quot; format.&quot;, vbOKOnly + vbCritical, &quot;Invalid Input&quot;
txtDate.Text = &quot;mmddyy&quot;
txtDate.SetFocus
txtDate.SelStart = 0
txtDate.SelLength = Len(txtDate.Text)
End If
End If
End Sub

Private Function Valid_DateNumbers(Entry As String) As Boolean
'// Determines if the entered date values are within acceptable ranges.
Dim l_bolOK As Boolean, l_intMonth As Integer, l_intDay As Integer, l_intYear As Integer
l_bolOK = True
If Len(Entry) <> 6 Then
l_bolOK = False
GoTo BadEntry
End If
l_intMonth = Val(Left(Entry, 2))
l_intDay = Val(Mid(Entry, 3, 2))
l_intYear = Val(Right(Entry, 2))
If l_intMonth <= 0 Or l_intMonth > 12 Then
l_bolOK = False
GoTo BadEntry
End If
Select Case l_intMonth
Case 1, 3, 5, 7, 8, 10, 12
If l_intDay <= 0 Or l_intDay > 31 Then
l_bolOK = False
GoTo BadEntry
End If
Case 2
If Right(Entry, 2) = &quot;00&quot; Or l_intYear Mod 4 = 0 Then
If l_intDay <= 0 Or l_intDay > 29 Then
l_bolOK = False
GoTo BadEntry
End If
Else
If l_intDay <= 0 Or l_intDay > 28 Then
l_bolOK = False
GoTo BadEntry
End If
End If
Case 4, 6, 9, 11
If l_intDay <= 0 Or l_intDay > 30 Then
l_bolOK = False
GoTo BadEntry
End If
End Select
BadEntry:
Valid_DateNumbers = l_bolOK
End Function


Thanks and Good Luck!

zemp
 
I'd only suggest consider using the Validate event instead of the lost focus event for the field.
 
whoa lotta code

if you want to put 050803 into a dtpicker, all your need to do is

progmatically
dtp_name.month = 5
dtp_name.day = 8
dtp_name.year = 2003

if you want the user to enter 050803 without tabbing then all you have to do is put code in the keypress event
 
after reading it again, i determined that you wish for the user to enter the information in 050803 without having to arrow, not programatically.

here is a snippet to help, obviously quick and nasty but should get you going in the right direction

Option Explicit
'
'declare variables
Private int_DtpKeyCount As Integer 'counter for current number in dtp
'


Private Sub DTPicker1_GotFocus()
'reset counter to 0
int_DtpKeyCount = 0
End Sub


Private Sub DTPicker1_KeyDown(KeyCode As Integer, Shift As Integer)
'count chars entered
int_DtpKeyCount = int_DtpKeyCount + 1
'skip forward for day and year
If int_DtpKeyCount = 3 Or int_DtpKeyCount = 5 Then KeyCode = 39
End Sub

 
Hi, hutchkc
I used your code in my project with a DTPicker, it is very simple and easy to use. I have two smal problems with the code which I couldn't solve: after I enter the year I want the cursor go automaticaly to day field or auto tab to another textbox. And the second is when I finished to enter the date by the keyboard, I am switching to some other textboxes to editing and then I return to the DTPicker to edit the date again but the code doesn't work. I think I must set the int_DtpKeyCount to zero but I couldn't find out how to do it. ý hope you help me.
Kind regards.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top