VBA INSERT INTO带有ADO参数

时间:2016-11-05 11:06:02

标签: vba foreach insert ado access

我在ACCESS VBA工作。我有一个sub,它将给定的集合插入到不同的表中,这取决于当前元素。我刚认识并希望使用这个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

我收到错误:运行时错误&#39; 3001&#39;参数类型错误,超出可接受的范围或彼此冲突 在

.parameters.Append .CreateParameter("pTitle", adVarChar, 100)

也许整个代码都错了,我不知道如何正确使用ADO参数,但你可以看到我想要做什么。我给这个函数一个集合。我遍历元素并将它们放在正确的表格中,我也有一个开关(addDae)。

请帮帮我。

1 个答案:

答案 0 :(得分:0)

您还可以编写一个逃避数据库单引号的函数。通常,单引号会通过加倍来“转义”。它会是这样的:

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) + "');"

(注意:尚未编译此代码;可能存在一些拼写错误。)