2013-02-13 48 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