日期验证:IF与案例 [英] Date Validation: IF's vs Case

查看:90
本文介绍了日期验证:IF与案例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行数据验证,需要检查一个字段是否包含一个

的有效日期,格式为YYYY-MM-DD


我想知道是否可以将这一系列的If''s

转换为Select Case。我在整合各种

函数(len(),isdate()时遇到了问题.IFF的工作,但我知道这可能是更好更灵活的
任何建议都将不胜感激。



''----------------------- ---------

如果Len(str_CheckDate)<> 10那么

''null check,字段必须包含的内容

fun_CheckDateFormat = False


ElseIf str_CheckDate =""然后

''null check,字段必须包含的内容

fun_CheckDateFormat = False


ElseIf IsDate(str_CheckDate)然后

''检查一个有效的日期

'' - 如果无效格式,需要指出''格式''错误

某种


''必须包含'' - ''作为分隔符\\ br / >
如果Mid(str_CheckDate,5,1)<>" - " Or _

Mid(str_CheckDate,8,1)<>" - "然后

好​​玩_CheckDateFormat = False


Else


str_Year = Left(str_CheckDate,4)

str_Month = Mid(str_CheckDate, 6,2)

str_Day =右(str_CheckDate,2)


''检查有效年份

如果str_Year< =年(现在()) - 25然后

fun_CheckDateFormat = False

结束如果


''检查有效月份

如果str_Month< 1或str_Month> 12然后

fun_CheckDateFormat = False

结束如果


''检查每月有效

如果不是(str_Day> 0和str_Day< = Day(DateSerial(str_Year,

str_Month + 1,0)))那么

fun_CheckDateFormat = False
结束如果

结束如果


ElseIf不是IsDate(str_CheckDate)那么

fun_CheckDateFormat = False

结束如果


''----------------------------- ---


基本上,你能用这些功能做一个CASE吗?怎么样?:

选择案例str_CheckDate

案例Len()<> 10 ...

....

结束选择

I am doing so data validation, and need to check if a filed contains a
valid date in the format of "YYYY-MM-DD"

I would like to know if it is possible to convert this series of If''s
to a Select Case. I am having trouble incorporating the various
functions (len(), isdate(). The IF''s work, but I know this could be
better and more flexible. Any suggestions would be greatly appreciated.
:
''--------------------------------
If Len(str_CheckDate) <> 10 Then
''null check, field must contain something
fun_CheckDateFormat = False

ElseIf str_CheckDate = "" Then
''null check, field must contain something
fun_CheckDateFormat = False

ElseIf IsDate(str_CheckDate) Then
''Check if a valid date
'' - If invalid format, need to indicate a ''formatting'' error of
some kind

''must contain ''-'' as seperator character
If Mid(str_CheckDate, 5, 1) <> "-" Or _
Mid(str_CheckDate, 8, 1) <> "-" Then
fun_CheckDateFormat = False

Else

str_Year = Left(str_CheckDate, 4)
str_Month = Mid(str_CheckDate, 6, 2)
str_Day = Right(str_CheckDate, 2)

''check for valid year
If str_Year <= Year(Now()) - 25 Then
fun_CheckDateFormat = False
End If

''check for valid month
If str_Month < 1 Or str_Month > 12 Then
fun_CheckDateFormat = False
End If

''check for valid day in month
If Not (str_Day > 0 And str_Day <= Day(DateSerial(str_Year,
str_Month + 1, 0))) Then
fun_CheckDateFormat = False
End If
End If

ElseIf Not IsDate(str_CheckDate) Then
fun_CheckDateFormat = False
End If

''--------------------------------

Basically, can you do a CASE with the functions? How?:
Select Case str_CheckDate
Case Len() <> 10 ...
....
End Select

推荐答案

Daron写道:
我正在进行数据验证,需要检查一个字段是否包含格式为YYYY-MM-DD的有效日期
I am doing so data validation, and need to check if a filed contains a
valid date in the format of "YYYY-MM-DD"




你可能在这里工作太辛苦了。你是否在

字段中使用DateTime类型?如果您输入无效日期是不可能的,

格式将与输入日期无关。只会

控制它的显示方式。


-

我没有查看附加的电子邮件帐户

来自此消息。发送给... ...

在Hunter dot com的RBrandt



You might be working too hard here. Are you using a DateTime type for the
field? If you do the entry of an invalid date would be impossible and the
formatting will have nothing to do with how the date is entered. It will only
control how it is displayed.

--
I don''t check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com


我在这里做的工作是验证一个后来会<的文本文件br />
在单独的进程中导入Oracle数据仓库。所有字段

都以文本类型引入,然后根据各种

标准(长度,格式,查找表等等)进行检查。 />

我可以在程序中更改为日期类型变量,但我仍然需要检查数据格式是否正确,然后检查

违反其他标准。精选案例仍然是一个很好的方式,

如果我还可以检查长度,IsDate等。


TIA


Daron

The work I am doing here is validating a text file that will later be
imported into an Oracle datawarehouse in a seperate process. All fields
are brought in as Text type, and then checked against a variety of
criteria (length, format, lookup tables, etc, etc...)

I could change to a date type variable in the procedure, but I still
need to check that the data is in the proper format, and then check
against other criteria. A Select Case would still be a great way to go,
if I can also check for length, IsDate, etc.

TIA

Daron


" Daron" <沓********** @ gmail.com>在消息中写道

news:11 ********************* @ o13g2000cwo.googlegro ups.com ...
"Daron" <Da**********@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
我在这里所做的工作是验证一个文本文件,该文件稍后将在单独的进程中导入Oracle数据仓库。所有字段
都以文本类型的形式引入,然后根据各种
标准(长度,格式,查找表等等)进行检查......

我可以更改为过程中的日期类型变量,但我仍然需要检查数据是否格式正确,然后根据其他条件检查
。精选案例仍然是一个很好的方式,
如果我还可以检查长度,IsDate等。
The work I am doing here is validating a text file that will later be
imported into an Oracle datawarehouse in a seperate process. All fields
are brought in as Text type, and then checked against a variety of
criteria (length, format, lookup tables, etc, etc...)

I could change to a date type variable in the procedure, but I still
need to check that the data is in the proper format, and then check
against other criteria. A Select Case would still be a great way to go,
if I can also check for length, IsDate, etc.




那么你所需要的只是检查IsDate()。如果通过则设置

格式,无论使用哪一种格式。

如果是IsDate(str_CheckDate)那么

str_CheckDate =格式(CDate(str_CheckDate," YYYY-MM-DD))

否则

''处理错误

结束如果


-

我没有查看电子邮件帐户附上

来发送此消息。转而发送给...

来自Hunter dot的RBrandt



Then all you need is to check against IsDate(). If that passes then set the
format regardless of which one was used.

If IsDate(str_CheckDate) Then
str_CheckDate = Format(CDate(str_CheckDate, "YYYY-MM-DD")
Else
''Handle error
End If

--
I don''t check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com


这篇关于日期验证:IF与案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆