2013-02-13 49 views
0

我有一个用VB6制作的表单,我需要检查日期字段以备将来使用。Visual basic 6:检查日期是否将来

如果输入的日期将来会显示错误消息,如果不是,验证应该完成。

我做:

Private Sub txtDate_Validate(Index As Integer, Cancel As Boolean) 
If Not IsDate(txtDate(9).Text) Then 'first I check if the data entered is a date 
'error message saying the field needs a valid date 
    Cancel = True 
     Else 
     If (txtDate(9).Text > Date) Then 'now I check if the date entered is bigger than today’s date 
       'error message saying the date is in the future 
       Cancel = True 
      Else 
       Exit Sub 
      End If 
     End If 
End If 
End Sub 

此代码不起作用,因为

txtDate(9).Text > Date 

始终是真实的

即使我做的:

Format(txtDate(9).Text, "dd/mm/yyyy") > Date 

总是真是太

我能做些什么来解决这个问题?我怎么知道输入的日期是否将来?

谢谢!

+4

将txtDate(9).text转换为日期。编程规则#356,不要使用字符串进行日期运算。 – 2013-02-13 09:13:39

+0

Cdate,我找到了!请发表您的评论,以便我可以接受。非常感谢,VB6学习者在这里! – user523129 2013-02-13 10:11:30

+0

奇怪的是,你[最后一次告诉](http://stackoverflow.com/questions/14558797/vb6-validating-date-with-a-certain-format#comment20315498_14558797):) – Deanna 2013-02-13 15:29:35

回答

1

使用CDate将文本框的内容转换为日期。

如果它有引号围绕它,这是不够的,它是一个字符串。

+0

没有引号在txtDate(9).Text :) – user523129 2013-02-14 07:49:53

+0

'.Text'种'意味着它是文本虽然:p – Deanna 2013-02-14 09:11:29