大家好,我浏览了一下,其中一个主题是“如何判断学生是否编码了这个代码”。嗯,我是一名学生,在一家很大的公司实习。我最近在Access中为他们编写了一个报告工具,并对一段代码有一个问题
'get the dates'
'check if both date fields are filled out'
If A_AfterDateTxt.Value <> "" And A_BeforeDateTxt.Value <> "" Then
'check if they are valid (ie, one date is not bigger then the other)'
If CDate(A_AfterDateTxt.Value) > CDate(A_BeforeDateTxt.Value) Then
MsgBox "You have selected invalid dates. Try again."
GetSQLForActiveRecords = False 'this is the function name
Exit Function 'exit the function'
Else
'this takes both fields and appends them to the sql statement'
strSql = strSql & " AND ([Submitted] >= #" & A_AfterDateTxt.Value & "# and [Submitted] <= #" & A_BeforeDateTxt.Value & "#)"
End If
Else
'one or both of them are blank, select the one thats entered
If (SF_isNothing(A_AfterDateTxt.Value) And Not SF_isNothing(A_BeforeDateTxt.Value)) Then
strSql = strSql & " AND ([Submitted] <= #" & A_BeforeDateTxt.Value & "#)"
ElseIf (SF_isNothing(A_BeforeDateTxt.Value) And Not SF_isNothing(A_AfterDateTxt.Value)) Then
strSql = strSql & " AND ([Submitted] >= #" & A_AfterDateTxt.Value & "#)"
End If
End If注意:SI_isnothing只是一个检查空/空的函数,但是因为他们的数据来自文本框,所以它永远不会是空的,对吧?
因此有两个日期文本框(AfterDate和BeforeDate)。我根据填写的内容构建SQL语句(例如,一个是输入的,另一个是空的)
那么我如何修改它才能使它更具可读性呢?
发布于 2010-07-14 21:28:55
我清理了一下代码。您可以将文本框中的值放入变量中,这使得使用这些值的代码不那么麻烦。
'get the dates
Dim before As String = A_BeforeDateTxt.Value
Dim after As String = A_AfterDateTxt.Value
'check if both date fields are filled out
If after.Length > 0 And before.Length > 0 Then
'check if they are valid (ie, one date is not bigger then the other)
If CDate(after) > CDate(before) Then
MsgBox "You have selected invalid dates. Try again."
GetSQLForActiveRecords = False
Exit Function
Else
'this takes both fields and appends them to the sql statement
strSql = strSql & " AND ([Submitted] >= #" & after & "# and [Submitted] <= #" & before & "#)"
End If
Else
'one or both of them are blank, select the one thats entered
If (after.Length = 0 And before.Length > 0 Then
strSql = strSql & " AND ([Submitted] <= #" & before & "#)"
ElseIf before.Length = 0 And after.Length > 0 Then
strSql = strSql & " AND ([Submitted] >= #" & after & "#)"
End If
End If您说得对,始终是字符串的值不可能是空的。检查不能发生的条件只会使代码更加混乱,因为这意味着值可能是它实际上不能发生的东西。
我使用Length属性检查字符串是否为空。比较数字比比较字符串的效率稍高一些,而且也不容易出错。你可能会不小心写上"'“而不是"",这并不容易被发现。
我删除了一些无意义的评论。注释应该解释代码中需要解释的内容,一个只从字面上告诉代码正在做什么的注释只会弄乱代码,就像这样:
Exit Function 'exit the function'可以重写代码以重用您添加条件的部分,这样您就不会在三个地方拥有这些条件。但这会使代码变得更复杂,所以值得怀疑。
发布于 2010-07-14 21:37:16
只有4种可能的“状态”:
考虑到这一点,你可以这样简化你的逻辑:
Dim dx As Integer = 0
If Not String.IsNullOrEmpty(txtBefore.Text) Then
If IsDate(txtBefore.Text) Then
dx += 1
End If
End If
If Not String.IsNullOrEmpty(txtAfter.Text) Then
If IsDate(txtAfter.Text) Then
dx += 2
End If
End If
Select Case dx
Case 1
'only before date is not empty and a valid date
Case 2
'only after date is not empty and a valid date
Case 3
'both are valid and not empty
End Select请注意,这是vb.NET,我不确定其中有多少可以转换为VBA
发布于 2010-07-14 21:47:00
通常,将多个布尔求值组合到单个变量中通常可以提高可读性。
If A_AfterDateTxt.Value <> "" And A_BeforeDateTxt.Value <> "" Then .....
becomes
Dim dateValuesPresent as Boolean = A_AfterDateTxt.Value <> "" And A_BeforeDateTxt.Value <> ""
If dateValuesPresent Then ....
If CDate(A_AfterDateTxt.Value) > CDate(A_BeforeDateTxt.Value) Then ....
becomes
Dim areValidDates as Boolean = CDate(A_AfterDateTxt.Value) > CDate(A_BeforeDateTxt.Value)
If areValidDates Then ....https://stackoverflow.com/questions/3246386
复制相似问题