Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 优化报告工具_Vba_Ms Access - Fatal编程技术网

Vba 优化报告工具

Vba 优化报告工具,vba,ms-access,Vba,Ms Access,嗨,伙计们,我在浏览so,其中一个主题是“如何判断学生是否对其进行了编码”。。我是一名学生,在一家相当大的公司实习。我最近在Access中为他们编写了一个报告工具,并对一段代码提出了一个问题 'get the dates' 'check if both date fields are filled out' If A_AfterDateTxt.Value <> "" And A_BeforeDateTxt.Value <> "" Then 'check i

嗨,伙计们,我在浏览so,其中一个主题是“如何判断学生是否对其进行了编码”。。我是一名学生,在一家相当大的公司实习。我最近在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
“获取日期”
'检查两个日期字段是否都已填写'
如果一个_AfterDateTxt.Value“”和一个_BeforeDateTxt.Value“”,则
'检查它们是否有效(即,一个日期不大于另一个日期)'
如果CDate(A_AfterDateTxt.Value)>CDate(A_BeforeDateTxt.Value),则
MsgBox“您选择的日期无效。请重试。”
GetSQLForActiveRecords=False'这是函数名
退出函数“退出函数”
其他的
'这将获取这两个字段并将它们附加到sql语句'

strSql=strSql&“和([Submitted]>=#“&A_AfterDateTxt.Value&“#和[Submitted]我对代码进行了一些清理。您可以将文本框中的值放入变量中,这样代码使用这些值就不那么麻烦了

'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

代码可以重写以重用添加条件的部分,这样在三个地方就没有了。但是这会使代码更加复杂,因此值得怀疑是否值得。我对代码进行了一些清理。您可以将文本框中的值放入变量中,这样代码使用的值就更少了笨重的

'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

代码可以重写以重用添加条件的部分,这样在三个地方就不会有这样的情况。但这会使代码更复杂,因此值得怀疑是否值得。只有4种可能的“状态”:

  • 都是空的
  • 有效的
  • b有效
  • 两者都是有效的
考虑到这一点,您可以简化您的逻辑:

    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,因为只有4种可能的“状态”:

  • 都是空的
  • 有效的
  • b有效
  • 两者都是有效的
考虑到这一点,您可以简化您的逻辑:

    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一般来说,将多个布尔计算组合成一个变量通常可以提高可读性

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 ....
如果有一个“\u AfterDateTxt.Value”和一个“\u BeforeDateTxt.Value”,那么。。。。。
变成
Dim DATE VALUES显示为布尔值=A_AfterDateTxt.Value“”和A_BeforeDateTxt.Value“”
如果存在日期值,则。。。。
如果CDate(A_AfterDateTxt.Value)>CDate(A_BeforeDateTxt.Value),那么。。。。
变成
Dim areValidDates为布尔值=CDate(A_AfterDateTxt.Value)>CDate(A_BeforeDateTxt.Value)
如果是有效期,那么。。。。

一般来说,将多个布尔计算组合成一个变量通常可以提高可读性

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 ....
如果有一个“\u AfterDateTxt.Value”和一个“\u BeforeDateTxt.Value”,那么。。。。。
变成
Dim DATE VALUES显示为布尔值=A_AfterDateTxt.Value“”和A_BeforeDateTxt.Value“”
如果存在日期值,则。。。。
如果CDate(A_AfterDateTxt.Value)>CDate(A_BeforeDateTxt.Value),那么。。。。
变成
Dim areValidDates为布尔值=CDate(A_AfterDateTxt.Value)>CDate(A_BeforeDateTxt.Value)
如果是有效期,那么。。。。

使用以下功能:

我建议如下:

公共函数GetSQLForActiveRecords()作为布尔值 Dim WHERE子句作为字符串,badDate作为布尔值 在As日期之前变暗,在As日期之后变暗 如果不是IsNullOrZLS(A_BeforeDateTxt),则 如果不是IsDate(A_BeforeDateTxt),则 MsgBox“无法分析日期!” GetSQLForActiveRecords=False 退出功能 如果结束 before=CDate(A_BeforeDateTxt) whereClause=“[Submitted]=#”&A_AfterDateTxt.value&“#” 如果结束 如果在0之后和之前>之后,则 MsgBox“无效日期!” GetSQLForActiveRecords=False 退出功能 如果结束 GetSQLForActiveRecords=True 如果Len(whereClause)=0,则退出函数 strsql=strsql&“和(“&whereClause&”) 端函数
一些注意事项:

  • 这将处理无效日期的可能性
  • 它在VBA中,而不是在VB.NET中
  • 一旦检测到错误,立即退出该函数。这有助于避免嵌套的If-Then
  • 未初始化的日期变量的值为0,对应于1899年12月30日12:00 AM。如果在A_AfterDateTxt文本框中输入该日期,则代码会将其视为空
  • 必须有某种方法避免重复日期解析消息

使用以下功能:

我建议如下:

公共函数GetSQLForActiveRecords()作为布尔值 Dim WHERE子句作为字符串,badDate作为布尔值 在As日期之前变暗,在As日期之后变暗 如果不是IsNullOrZLS(A_BeforeDateTxt),则 如果不是IsDate(A_BeforeDateTxt),则 MsgBox“无法分析日期!” GetSQLForActiveRecords=False 退出功能 如果结束 before=CDate(A_BeforeDateTxt) whereClause=“[Submitted]=#”&A_AfterDateTxt.value&“#” 如果结束 如果在0之后和之前>之后,则 MsgBox“无效日期!” GetSQLForActiveRecords=False 退出功能 如果结束 GetSQLForActiveRecords=True 如果Len(whereClause)=0,则退出函数 strsql=strsql&“和(“&whereClause&”) 端函数 一些注意事项:

  • 这将处理无效日期的可能性
  • 它在VBA中,而不是在VB.NET中
  • 一旦检测到错误,立即退出该函数。这有助于避免嵌套的If-Then
  • 未初始化的日期变量的值为0,对应于1899年12月30日12:00 AM。如果在A_AfterDateTxt文本框中输入该日期,则代码会将其视为空
  • 必须有某种方法避免重复日期解析消息

    • 您想要可读性吗?如下所示:

      Select Case True
      Case Not IsDate(A_BeforeDateTxt.Value) And Not IsDate(A_AfterDateTxt.Value)
          MsgBox "You have selected invalid dates. Try again."
          GetSQLForActiveRecords = False   'this is the function name
      
      Case A_AfterDateTxt.Value = ""
          strSql = strSql & " AND ([Submitted] <= #" & A_BeforeDateTxt.Value & "#)"
      
      Case A_BeforeDateTxt.Value = ""
          strSql = strSql & " AND ([Submitted] <= #" & A_BeforeDateTxt.Value & "#)"
      
      Case CDate(A_AfterDateTxt.Value) > CDate(A_BeforeDateTxt.Value)
          MsgBox "You have selected invalid dates. Try again."
          GetSQLForActiveRecords = False   'this is the function name
      
      Case Else
          strSql = strSql & " AND ([Submitted] >= #" & A_AfterDateTxt.Value & "# and
            [Submitted] <= #" & A_BeforeDateTxt.Value & "#)"
      
      End Select
      
      选择Case True
      Case Not IsDate(A_BeforeDateTxt.Value)和Not IsDate(A_AfterDateTxt.Value)
      MsgBox“您选择了无效的d