我正在製作一個程序,在該程序中,我必須在嘗試將值保存在另一個表中之前檢查另一個表中的某些列值。兩個表都在SQL中。使用MSSql時運行時錯誤'91'
我盡我所能自己做,但我得到突出顯示的行附近的錯誤。
rs.open(insert into testreport_tb1...
Private Sub Command1_Click()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim BrdSrNo As String
Dim Result As Boolean
Dim machineName As String
machineName = Environ("computername")
' Ready objects for use.
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
BrdSrNo = BoardSrNo.Text
Result = False
' Connect.
cn.Open "{Here I give the connection string}"
' Fetch a recordset.
rs.Open "select * from testreport_tb1 where board_SrNo = '" & BrdSrNo & "' order by test_DateTime desc", cn, adOpenStatic, adLockReadOnly
' Display value, and total recordcount.
MsgBox rs.Fields(3)
MsgBox rs.Fields(8)
'MsgBox rs.RecordCount
stage_Status = rs.Fields(3)
stage_Id = rs.Fields(8)
rs.Close
cn.Close
If stage_Status = "C" Then
If stage_Id = "True" Then
rs.Open "insert into testreport_tb1 values('" & BrdSrNo & "',3,GETDATE(),'" & Result & "',NULL,'" & machineName & "',' KO ','A','D')", cn, adOpenDynamic, adLockBatchOptimistic
MsgBox "saved"
End If
End If
' Close and release objects.
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub
爲什麼在失敗的行中有<! - language:lang-js - > ***'? – Sebas
我試圖突出顯示錯誤行,雖然張貼..多數民衆贊成在它.. @Sebas – suvi
這是一段時間,我還沒有做任何VB ...但我很奇怪,你使用插入語句的記錄集。我認爲你需要使用'cn.Execute sql,,adCmdText','sql'作爲你的查詢。 – Sebas