VB6:以某种格式验证日期 [英] VB6: validating date with a certain format
问题描述
现在我需要要验证日期字段,我需要输入的日期格式为:dd / mm / yyyy
我在做:
Private Sub txtMyText_Validate(Index As Integer,Cancel As Boolean)
如果IsDate(格式$(txtMyText(9).Text,dd / mm / yyyy ))或txtMyText(9).Text =然后
txtMyText(9).SetFocus
Else
txtMyText(9).SetFocus
MsgBox请输入有效的日期使用此格式:dd / mm / yyyy。
End If
End Sub
但是这个代码不工作。当我输入这个格式的日期dd / mm / yy时,流程跟随插入函数,我得到一个错误,导致它不是dd / mm / yyyy。
<你可以帮我修理这段代码吗?
非常感谢!
在插入日期字段的数据库行中使用此选项:
格式(txtMyText(9).Text,dd / mm / yyyy)
这将解决以dd / mm / yy或yyyy / mm / dd输入的日期
并且为了防止输入整数或字符串而不是日期:
Private Sub txtMyText_Validate(Index As Integer,Cancel As Boolean)
如果不是IsDate(txtMyText(9)然后
MsgBox输入这个格式的有效日期:dd / mm / yyyy
取消= True
如果
结束Sub
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!
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
这篇关于VB6:以某种格式验证日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!