【问题标题】:MS Access / VBA Code: How to combine these 2 proceduresMS Access / VBA 代码:如何结合这两个程序
【发布时间】:2020-07-13 22:02:12
【问题描述】:

我是一名 VBA 新手,试图将这两个子程序组合成一个程序 - 谁能提供如何做到这一点?

基本上,我正在尝试将图像添加到 Access 报告中(在第二个代码块中 - 它正在检查/创建图像路径) - 已经检查了数据库产品记录中的其他信息。

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)

Dim x$, y$, i%

x = ""
For i = 1 To 10
    y = Me("txtOp" & i) & ""
    If y > "" Then
        If x > "" Then x = x & "  "
        x = x & "Option " & i & ": " & y
    End If
Next
If x > "" Then x = CR & x
Me.txtProduct = Me.txtItem & "" & x

If Me.Adjustment Then
    Me.txtShowSKU = ""
Else
    Me.txtShowSKU = Me.txtSKU
End If



Dim x, y, OK%
OK = False
x = Me.txtImage & ""
If x > "" Then
    y = getparm("ImagePath")
    If y > "" Then
        If Right$(y, 1) <> "\" Then y = y & "\"
        If Left$(x, 1) = "\" And Len(x) > 1 Then x = Mid$(x, 2)
        If FileExists(y & x) Then OK = True: x = y & x
    End If

    If OK Then
        Me.imgProd.visible = True
        Me.imgProd.Picture = x
    Else
        Me.imgProd.visible = False
    End If
End If

End Sub

【问题讨论】:

  • 您的代码示例只显示了一个子例程。这是您的综合结果吗?
  • 感谢院长的回复,但我不明白您的评论。是的,上面只有一个子程序,但我需要将两个语句合并为一个(两部分代码之间有3个换行符),我不知道如何让两个函数都运行,我相信你只能每个子程序有一个“Dim”语句?这有任何意义吗?谢谢!
  • 你只能Dim一个变量一次,但你可以重新初始化它多次。不要使用Dim x, y, OK%,而是尝试将 x 和 y 设置为“”。你仍然需要 Dim OK% 因为之前没有声明过。顺便说一句,现在声明类型的简写符号并不是很好的做法。最好使用Dim x as string等。
  • 感谢 Dean,我非常感谢您和 HK1 的帮助 - 这些信息教会了我如何让它发挥作用。正如您所提到的,只需更改以下内容也可以使其工作: Dim x, y, OK% into: Dim OK% y = ""

标签: ms-access vba


【解决方案1】:

我认为这应该可行:

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)

    Dim x as String
    Dim y as String
    Dim i as Integer

    For i = 1 To 10
        y = Me("txtOp" & i) & ""
        If y > "" Then
            If x > "" Then x = x & "  "
            x = x & "Option " & i & ": " & y
        End If
    Next
    If x > "" Then x = CR & x
    Me.txtProduct = Me.txtItem & "" & x

    If Me.Adjustment Then
        Me.txtShowSKU = ""
    Else
        Me.txtShowSKU = Me.txtSKU
    End If

    Dim OK as Boolean
    y = ""

    x = Me.txtImage & ""
    If x > "" Then
        y = getparm("ImagePath")
        If y > "" Then
            If Right$(y, 1) <> "\" Then y = y & "\"
            If Left$(x, 1) = "\" And Len(x) > 1 Then x = Mid$(x, 2)
            If FileExists(y & x) Then OK = True: x = y & x
        End If

        If OK Then
            Me.imgProd.visible = True
            Me.imgProd.Picture = x
        Else
            Me.imgProd.visible = False
        End If
    End If

End Sub

【讨论】:

    猜你喜欢
    • 2012-03-03
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-17
    相关资源
    最近更新 更多