VBA SQL Server插入查询

时间:2017-09-09 02:49:36

标签: sql-server vba insert

我收到错误:

enter image description here

执行此代码时:

Private Sub EditMethodAdd_Click()
MsgBox ("Add Click")

Dim rs As ADODB.Recordset
Dim introw
Dim strState As String
Dim strsql1 As String
Dim strsql2 As String
Dim all As String

Dim strConn As String
Dim conn As ADODB.Connection

MsgBox ("EditM1.Value:" & EditM1.value)


strConn = "DRIVER=SQL Server;SERVER=CHU-AS-0004;DATABASE=RTC_LaplaceD_DEV;Trusted_Connection=Yes;"

strsql1 = " INSERT INTO dbo.Method(MethodID, MethodClass, Category, Description, Description2, MSA, ReqType, Equipment, Location, Spec1, Spec2, Spec3, Spec4, Spec5, Spec6, PilotingYN) "
strsql2 = "   VALUES( " & EditM1.value & ", Piloting, " & EditM3.value & ", Null, Null, Null, Null," & EditM2.value & ", " & EditM4.value & ", " & EditM5.value & ", " & EditM6.value & ", " & EditM7.value & ", " & EditM8.value & ", " & EditM9.value & ", " & EditM10.value & ", " & Null & " )"

all = strsql1 & strsql2
MsgBox ("ALL" & all)


Set conn = New ADODB.Connection
conn.Open strConn

Set rs = New ADODB.Recordset
rs.Open all, conn
MsgBox ("Insert Success")

EditMethodList.Requery

conn.Close
Set rs = Nothing
Set conn = Nothing

MsgBox "Data has been updated"
EditMethodList.Requery

End Sub

有没有人可以解决这个问题?

我认为我的查询很好,但是这个错误让我感到烦恼。

任何评论都会非常感激。

MsgBox(“全部”和“全部”)显示此结果。 enter image description here

1 个答案:

答案 0 :(得分:0)

正如您的MessageBox所示,您尝试插入错误的值9.4.5V(LCO)+ICPqwer

MethodID的数据类型是什么?

  • 如果它是varchar并且包含+ICPqwer作为字符串的一部分,那么你 应该传递整个字符串引用,就像这样 '9.4.5V(LCO)+ICPqwer'

  • 如果它是一个字符串,但格式为9.4.5V(LCO) +其他字符串 (包含在ICPqwer中),使用'9.4.5V(LCO)' + ICPqwer

  • 如果ICPqwer是一个数字,请使用以下数字:'9.4.5V(LCO)' + cast(ICPqwer as varchar(100))

此外,所有其他值似乎也是字符串,也应该引用:

'Piloting', 'Internal Analysis', ...

最后一件事:你传递的“空”值是什么,,,? 明确地传递NULL,或者如果它们是空字符串,则将它们传递为''