2013-05-07 89 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