在Visual Basics中,我有一个直到循环,以确保以正确的方式输入日期。如何在输入框中输入用户输入并将其另存为日期变量?
Private Sub cmdbtn3_Click()
'create a booleanvariable
Dim blDone As Boolean
'ask for there name
strName = InputBox("Please Enter Your Name", "Step 1")
'create a do while not because we want a repeat until blDone is True
Do While Not blDone
'set the value of the input box with the product list equal to the boolean
'ask for the date of the start, have a do loop until date1
blDone = InputBox("Please Enter the Estimated Starting Date of CU" & vbNewLine & "Example 05/24/2014") Like "##/##/####"
'Create a msg that explains what the input must be
If Not blDone Then MsgBox "the input didn't match the pattern '01/23/2014' where:" _
& vbNewLine & vbTab & "'01' must bethe month" _
& vbNewLine & vbTab & "'23' must be the date" _
& vbNewLine & vbTab & "'2014' must be the year"
'I need to save the value of the date as
Loop
End Sub
答案 0 :(得分:2)
存储输入,然后对其进行比较。如果dateInput
设置为Date
类型,那么您的检查会失败,因为01/01/2000
已更改为1/1/2000
。我通过在Variant
中捕获它来检查它,然后转换它并在最后存储在dateOutput
中来解决这个问题。
Private Sub cmdbtn3_Click()
'create a booleanvariable
Dim blDone As Boolean, dateInput As Variant, dateOutput As Date
'ask for there name
strName = InputBox("Please Enter Your Name", "Step 1")
'create a do while not because we want a repeat until blDone is True
Do While Not blDone
'set the value of the input box with the product list equal to the boolean
'ask for the date of the start, have a do loop until date1
dateInput = InputBox("Please Enter the Estimated Starting Date of CU" & vbNewLine & "Example 05/24/2014")
blDone = dateInput Like "##/##/####"
'Create a msg that explains what the input must be
If Not blDone Then MsgBox "the input didn't match the pattern '01/23/2014' where:" _
& vbNewLine & vbTab & "'01' must bethe month" _
& vbNewLine & vbTab & "'23' must be the date" _
& vbNewLine & vbTab & "'2014' must be the year"
'I need to save the value of the date as
Loop
dateOutput = CDate(dateInput)
End Sub