2016-07-08 133 views
0

我得到一個「自動化錯誤」就行rstLgn_details.Open SqlCmdExcel自動化錯誤

這是代碼。

Call ConnectSS_Param(ThisWorkbook.FullName)  

Set rstLgn_details = CreateObject("ADODB.Recordset") 
Set SqlCmd = CreateObject("ADODB.Command") 
Set SqlparamUserID = SqlCmd.CreateParameter("@UserID", 129, 1, 52, txtUsername.Value) 

StrSql = "SELECT Capabilityname,SLAA_EID FORM $Login_Details WHERE [email protected]" 

SqlCmd.ActiveConnection = conndb 
SqlCmd.CommandText = StrSql 
SqlCmd.Parameters.Append SqlparamUserID    

rstLgn_details.Open SqlCmd 

If rstLgn_details.RecordCount > 0 Then   
    UserName = rstLgn_details(1) 
    CapHlded = rstLgn_details(0) 
    ThisWorkbook.Sheets("Serach").Select 
Else 
    MsgBox "Sorry You are Not Authorized for this..... ", vbInformation + vbOKOnly, "Invaild User" 
End If 
+4

對於一件事你有FORM而不是'FROM'。 –

回答

0

當您重新連接到Excel工作簿時,原始文件關閉,並且設置爲原始文件的所有引用不再有效。當您嘗試使用其中一個無效引用時,您將獲得Automation Error

這裏是Automation Error Using Excel Object in Visual Basic Procedure

的使出,當您在Visual Basic程序訪問一個Microsoft Excel OLE自動化對象,而Microsoft Excel中關閉,則會出現此問題。例如,如果您使用CreateObject函數創建工作表對象的引用,並且在過程中使用GetObject函數創建另一個Microsoft Excel對象的引用,那麼如果您隨後設置了Microsoft Excel工作表對象(Excel.Sheet)等於沒有,如果您嘗試訪問其他對象,則會收到OLE自動化錯誤消息。