首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >优化报表工具

优化报表工具
EN

Stack Overflow用户
提问于 2010-07-14 21:07:18
回答 5查看 274关注 0票数 1

大家好,我浏览了一下,其中一个主题是“如何判断学生是否编码了这个代码”。嗯,我是一名学生,在一家很大的公司实习。我最近在Access中为他们编写了一个报告工具,并对一段代码有一个问题

代码语言:javascript
复制
    '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语句(例如,一个是输入的,另一个是空的)

那么我如何修改它才能使它更具可读性呢?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2010-07-14 21:28:55

我清理了一下代码。您可以将文本框中的值放入变量中,这使得使用这些值的代码不那么麻烦。

代码语言:javascript
复制
'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属性检查字符串是否为空。比较数字比比较字符串的效率稍高一些,而且也不容易出错。你可能会不小心写上"'“而不是"",这并不容易被发现。

我删除了一些无意义的评论。注释应该解释代码中需要解释的内容,一个只从字面上告诉代码正在做什么的注释只会弄乱代码,就像这样:

代码语言:javascript
复制
Exit Function  'exit the function'

可以重写代码以重用您添加条件的部分,这样您就不会在三个地方拥有这些条件。但这会使代码变得更复杂,所以值得怀疑。

票数 1
EN

Stack Overflow用户

发布于 2010-07-14 21:37:16

只有4种可能的“状态”:

  • a
  • b valid
  • 两者都是有效的

考虑到这一点,你可以这样简化你的逻辑:

代码语言:javascript
复制
    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

票数 3
EN

Stack Overflow用户

发布于 2010-07-14 21:47:00

通常,将多个布尔求值组合到单个变量中通常可以提高可读性。

代码语言:javascript
复制
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 ....
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3246386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档