VBA 使用 ADO 参数插入
VBA INSERT INTO with ADO parameters
我在 ACCESS VBA 工作。我有一个 sub,它将给定的集合插入不同的 tables,这取决于当前元素。我刚刚遇到并想使用这个 ADO 参数,因为我对简单的 docmd.runsql "insert into..."
有疑问。问题是有些字符串包含“'
”字符,所以我在这些记录中出错。我搜索并找到了 ADO 参数,但没有好的教程,现在我厌倦了错误代码,这些代码也没有帮助(来自 java)。
我的代码片段:
Dim adoCMD As Object
Set adoCMD = CreateObject("ADODB.Command")
With adoCMD
.ActiveConnection = CurrentProject.Connection
.CommandType = adCmdText
.parameters.Append .CreateParameter("pKey", adInteger)
.parameters.Append .CreateParameter("pTitle", adVarChar, 100)
.parameters.Append .CreateParameter("pid", adInteger)
.parameters.Append .CreateParameter("pAired", adDate)
.parameters.Append .CreateParameter("pmtype", adInteger)
.parameters.Append .CreateParameter("pSyear", adSmallInt)
.parameters.Append .CreateParameter("pSeason", adInteger)
If addDate Then
.parameters.Append .CreateParameter("pAdded", adDate)
End If
For Each elem In ac
If elem.aired >= seasonStart And elem.aired <= seasonEnd Then
key = Nz(DMax("table_Akey", "table_A"), 0) + 1
.parameters("pKey").Value = key
.parameters("pTitle").Value = elem.title
.parameters("pid").Value = elem.id
.parameters("pAired").Value = elem.aired
.parameters("pmtype").Value = elem.mtype
.parameters("pSyear").Value = syear
.parameters("pSeason").Value = season
If addDate Then
.parameters("pAdded").Value = Date
.CommandText = "INSERT INTO table_A(table_Akey,title,id,aired,media,seasonyear,season,added) VALUES(pKey,pTitle,pid,pAired,pmtype,pSyear,pSeason,pAdded);"
Else
.CommandText = "INSERT INTO table_A(table_Akey,title,id,aired,media,seasonyear,season) VALUES(pKey,pTitle,pid,pAired,pmtype,pSyear,pSeason);"
End If
Else
wCounter = wCounter + 1
key = Nz(DMax("wskey", "ws"), 0) + 1
.parameters("pKey").Value = key
.parameters("pTitle").Value = elem.title
.parameters("pid").Value = elem.id
.parameters("pAired").Value = elem.aired
.parameters("pmtype").Value = elem.mtype
.parameters("pSyear").Value = syear
.parameters("pSeason").Value = season
.CommandText = "INSERT INTO ws(wskey,title,id,aired,media,wsyear,season) VALUES(pKey,pTitle,pid,pAired,pmtype,pSyear,pSeason);"
End If
.Execute
Next elem
End With
我收到错误:运行时错误“3001”参数类型错误,超出接受table范围或相互冲突
在行
.parameters.Append .CreateParameter("pTitle", adVarChar, 100)
也许整个代码是错误的,我不知道如何正确处理ADO参数,但你可以看到我想做什么。我给这个函数一个集合。我循环遍历元素并将它们放在右边 table 并且我还有一个开关 (addDae
).
请帮助我。
您还可以编写一个转义 DB 单引号的函数。通常,单引号是 "escaped" 加倍。它会是这样的:
Function escapeDBquote(s As String) As String
Dim sOut As String
Dim i As Integer
i = 1
While (i <= len(s))
If (Mid(s, i, 1) = "'") Then sOut = sOut +"'"
sOut = sOut + Mid(s, i, 1)
i = i + 1
Wend
escapeDBquote = sOut
End Function
并这样称呼它:
Dim szSQL As String
szSQL = "insert into myTable (sfield1, ..) values ('" + escapeDBquote(myString) + "');"
(注意:此代码尚未编译;可能有一些错别字。)
我在 ACCESS VBA 工作。我有一个 sub,它将给定的集合插入不同的 tables,这取决于当前元素。我刚刚遇到并想使用这个 ADO 参数,因为我对简单的 docmd.runsql "insert into..."
有疑问。问题是有些字符串包含“'
”字符,所以我在这些记录中出错。我搜索并找到了 ADO 参数,但没有好的教程,现在我厌倦了错误代码,这些代码也没有帮助(来自 java)。
我的代码片段:
Dim adoCMD As Object
Set adoCMD = CreateObject("ADODB.Command")
With adoCMD
.ActiveConnection = CurrentProject.Connection
.CommandType = adCmdText
.parameters.Append .CreateParameter("pKey", adInteger)
.parameters.Append .CreateParameter("pTitle", adVarChar, 100)
.parameters.Append .CreateParameter("pid", adInteger)
.parameters.Append .CreateParameter("pAired", adDate)
.parameters.Append .CreateParameter("pmtype", adInteger)
.parameters.Append .CreateParameter("pSyear", adSmallInt)
.parameters.Append .CreateParameter("pSeason", adInteger)
If addDate Then
.parameters.Append .CreateParameter("pAdded", adDate)
End If
For Each elem In ac
If elem.aired >= seasonStart And elem.aired <= seasonEnd Then
key = Nz(DMax("table_Akey", "table_A"), 0) + 1
.parameters("pKey").Value = key
.parameters("pTitle").Value = elem.title
.parameters("pid").Value = elem.id
.parameters("pAired").Value = elem.aired
.parameters("pmtype").Value = elem.mtype
.parameters("pSyear").Value = syear
.parameters("pSeason").Value = season
If addDate Then
.parameters("pAdded").Value = Date
.CommandText = "INSERT INTO table_A(table_Akey,title,id,aired,media,seasonyear,season,added) VALUES(pKey,pTitle,pid,pAired,pmtype,pSyear,pSeason,pAdded);"
Else
.CommandText = "INSERT INTO table_A(table_Akey,title,id,aired,media,seasonyear,season) VALUES(pKey,pTitle,pid,pAired,pmtype,pSyear,pSeason);"
End If
Else
wCounter = wCounter + 1
key = Nz(DMax("wskey", "ws"), 0) + 1
.parameters("pKey").Value = key
.parameters("pTitle").Value = elem.title
.parameters("pid").Value = elem.id
.parameters("pAired").Value = elem.aired
.parameters("pmtype").Value = elem.mtype
.parameters("pSyear").Value = syear
.parameters("pSeason").Value = season
.CommandText = "INSERT INTO ws(wskey,title,id,aired,media,wsyear,season) VALUES(pKey,pTitle,pid,pAired,pmtype,pSyear,pSeason);"
End If
.Execute
Next elem
End With
我收到错误:运行时错误“3001”参数类型错误,超出接受table范围或相互冲突 在行
.parameters.Append .CreateParameter("pTitle", adVarChar, 100)
也许整个代码是错误的,我不知道如何正确处理ADO参数,但你可以看到我想做什么。我给这个函数一个集合。我循环遍历元素并将它们放在右边 table 并且我还有一个开关 (addDae
).
请帮助我。
您还可以编写一个转义 DB 单引号的函数。通常,单引号是 "escaped" 加倍。它会是这样的:
Function escapeDBquote(s As String) As String
Dim sOut As String
Dim i As Integer
i = 1
While (i <= len(s))
If (Mid(s, i, 1) = "'") Then sOut = sOut +"'"
sOut = sOut + Mid(s, i, 1)
i = i + 1
Wend
escapeDBquote = sOut
End Function
并这样称呼它:
Dim szSQL As String
szSQL = "insert into myTable (sfield1, ..) values ('" + escapeDBquote(myString) + "');"
(注意:此代码尚未编译;可能有一些错别字。)