2013-05-07 56 views
1

我是一个VBA新手,试图将这两个子过程合并为一个过程 - 任何人都可以提供如何做到这一点?MS Access/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 
+0

您的代码示例只显示一个子程序。这是你的综合结果吗? – DeanOC 2013-05-07 22:56:55

+0

感谢Dean的回应,但我不明白你的意见。是的,上面只有一个子程序,但我需要将两个语句合并为一个(有两个分隔代码两部分的换行符),我不知道如何使这两个函数都运行,并且我相信您只能每个子程序有一个“Dim”语句?这有任何意义吗?谢谢! – user2360175 2013-05-08 14:12:05

+0

您只能“变暗”一次变量,但您可以根据需要多次重新初始化它。不要使用'Dim x,y,OK%',只需将x和y设置为“”。你仍然需要Dim'OK%'因为之前没有声明过。顺便说一下,现在声明类型的简写符号并不是很好的做法。最好使用'Dim x as string'等 – DeanOC 2013-05-08 18:01:58

回答

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 
+0

非常感谢HK1,这对我来说确实有用! – user2360175 2013-05-09 22:02:51