2016-02-19 19 views
1

我想在圖片框中顯示選定的圖片。以下是代碼:其他如果不在Visual Basic中工作6

Private Sub Drive1_Change() 
Dir1.Path = Drive1.Drive 
End Sub 

Private Sub Dir1_Change() 
File1.Path = Dir1.Path 
End Sub 

Private Sub File1_Click() 
On Error GoTo lab 
lab: 
If Err.Number = 481 Then 
    MsgBox ("Please select a valid Picture") 
Else 
    If Err.Number = 68 Then 
     MsgBox ("Device not ready") 
    End If 
End If 
Resume Next 
Picture1.Picture = LoadPicture(File1.Path + "\" + File1.FileName) 
End Sub 

情況481工作得很好,但第二種情況,錯誤68根本不起作用。它顯示了運行時錯誤68.以下是輸出:

enter image description here

請讓我知道在上面的代碼中的錯誤。

+0

需要更多的信息。哪一行是拋出錯誤?如果File1_Click裏面有一個你的錯誤管理是無效的,因爲它可以讓你在任何情況下得到'Picture1.Picture ...' – user3598756

+0

如果一起寫入 – Kathara

+0

如果這是VB6,爲什麼是VBA標籤? VBA和VB6不盡相同,即使它們共享(某些)功能。這是什麼?如果是VB6,你想更改標籤,以便你可以確定合適的人看到這個...... –

回答

1

你平時把你的錯誤處理程序之前的代碼可能會拋出錯誤嗎?我無法通過我自己的簡單案例來複制此內容,但此代碼的結構似乎有些可疑。你也有一個沒有循環的Resume Next。試試這個:

Private Sub File1_Click() 
On Error GoTo lab 
Picture1.Picture = LoadPicture(File1.Path + "\" + File1.FileName) 
Exit Sub 

lab: 
Select Case Err.Number 
    Case 481 Then 
     MsgBox ("Please select a valid Picture") 
    Case 68 Then 
     MsgBox ("Device not ready") 
End Select 

End Sub 

聽起來你可能需要在其他過程的其他錯誤處理程序,例如:

Private Sub Dir1_Change() 
    On Error Resume Next 
    File1.Path = Dir1.Path 
    If Err.Number = 0 Then Exit Sub 
    Select Case Err.Number 
     Case 68 
      Msgbox ("Device not ready") 
     Case Else 
      MsgBox ("Error " & Err.NUmber) '# Modify as needed... 
    End Select 
End Sub 

Private Sub Drive1_Change() 
    On Error Resume Next 
    Dir1.Path = Drive1.Drive 
    If Err.Number = 0 Then Exit Sub 
    Select Case Err.Number 
     Case 68 
      Msgbox ("Device not ready") 
     Case Else 
      MsgBox ("Error " & Err.NUmber) '# Modify as needed... 
    End Select 
End Sub 
+0

試過上面的代碼,仍然是同一個問題 – user3382203

+0

哪條線提高了錯誤? –

+0

Dir1.Path = Drive1.Drive – user3382203