VB6: validating date with a certain format

て烟熏妆下的殇ゞ 提交于 2019-12-08 02:03:33

问题


I have built a form using Visual Basic 6. Everything goes great, the form inserts the data in my database and no problems here at all.

Now I need to validate the date field, I need the dates entered to have this format: dd/mm/yyyy

I'm doing:

Private Sub txtMyText_Validate(Index As Integer, Cancel As Boolean)
If IsDate(Format$(txtMyText(9).Text, "dd/mm/yyyy")) Or txtMyText(9).Text = "" Then
txtMyText(9).SetFocus
Else
txtMyText(9).SetFocus
MsgBox "Please enter a valid date with this format: dd/mm/yyyy."
End If
End Sub

But this code is not working. When I enter a date with this format dd/mm/yy the flow follows to the inserting function and I get an error there cause it is not a dd/mm/yyyy.

Can you please help me to fix this code?

Thanks a lot!


回答1:


Use this in your insert into database line for the date field:

Format(txtMyText(9).Text, "dd/mm/yyyy") 

That will solve dates entered as dd/mm/yy or yyyy/mm/dd

And to prevent from entering integers or strings instead of dates:

Private Sub txtMyText_Validate(Index As Integer, Cancel As Boolean)
    If Not IsDate(txtMyText(9).Text) Then
    MsgBox "Enter a valid date with this format: dd/mm/yyyy"
    Cancel = True
    End If
End Sub


来源:https://stackoverflow.com/questions/14558797/vb6-validating-date-with-a-certain-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!