2017-10-18 20 views
0

我借用了這段代碼,它似乎工作正常。但是,我希望它顯示一條消息,如果存儲過程返回沒有記錄?非常感謝您的幫助。VBA代碼調用存儲過程 - 如果沒有記錄返回

Private Sub CommandButton1_Click() 

Dim property_id As Double 

property_id = Sheets("Pro Forma Input").Range("AD3").Value 

With ActiveWorkbook.Connections("MyDataConnection").OLEDBConnection 
.CommandText = "EXEC dbo.my_stored_proc '" & property_id & "'" 
ActiveWorkbook.Connections("MyDataConnection").Refresh 

End With 
Application.DisplayAlerts = True 

End Sub 
+0

該連接附加到某個事物(查詢表,列表對象,可轉換表)。檢查刷新後他們有什麼數據。 – GSerg

回答

0

試試這個:

property_id = Sheets("Pro Forma Input").Range("AD3").Value 

If property_id = "" Then 
    MsgBox "There is nothing here!" 
Else 
End If 

希望這有助於!

+1

我認爲他的意思是SQL存儲過程沒有返回任何行 – KacireeSoftware

0

感謝解決方案的其他海報,由於某種原因,他/她的帖子被刪除。這是我在遇到別人需要時所做的。

With ActiveWorkbook.Connections("FirstKey_One_Pager_connection").OLEDBConnection 
.CommandText = "EXEC dbo.usp_FirstKey_One_Pager '" & property_id & "'" 
ActiveWorkbook.Connections("FirstKey_One_Pager_connection").Refresh 

If Sheets("DataSource").Range("B9").Value = "" Then 
    MsgBox "No property found using the RR ID entered" 
Else 
End If 
相關問題