在VBA / MS-ACCESS中使用备注和文本字段进行参数化INSERT?

时间:2013-06-03 18:23:25

标签: sql vba ms-access sql-insert

我尝试使用MS-Access 2007进行简单的参数化插入,我不断从ADODB.Parameters收到以下错误(3708):

Parameter object is improperly defined. Inconsistent or incomplete information was provided. s'

我有3个参数插入3个字段。它们具有以下类型:

  • query_name:TEXT(50)
  • db_id:Number(Long Integer)
  • query_text:备忘录

db_id插入就好了,但是其他两个文本字段会导致上面的错误。

以下是我的简单示例代码:

Sub testz()

    Dim conn As ADODB.Connection
    Set conn = CurrentProject.Connection

    Dim adoCMD As ADODB.Command
    Dim adoRS As ADODB.Recordset
    Dim strSQL As String
    Dim lRecordsAffected As Long

    On Error GoTo Err_Insert

    strSQL = "INSERT INTO queries (query_name, db_id, query_text) VALUES (?, ?, ?)"

    Set adoCMD = New ADODB.Command

    With adoCMD
        Dim test As String
        test = "tetws"
        .ActiveConnection = conn
        .CommandType = adCmdText
        .CommandText = strSQL

        .Parameters.Append .CreateParameter("x1", adVarChar, adParamInput) ' Doesn't work - MS ACCESS Text field
        .Parameters.Append .CreateParameter("x2", adInteger, adParamInput) ' This works - Numeric field
        .Parameters.Append .CreateParameter("x3", adLongVarChar, adParamInput) ' This doesn't work MS ACCESS Memo field.

        .Parameters("x1").Value = test
        .Parameters("x2").Value = 56
        .Parameters("x3").Value = test

        adoCMD.Execute adExecuteNoRecords
    End With

    If lRecordsAffected = 0 Then
        Debug.Print ("------------------------")
        Debug.Print ("ERROR QUERY not inserted:")
        Debug.Print ("database id: " & id)
        Debug.Print ("query name: " & tblName)
        Debug.Print ("strSQL: " & qryTxt)
        Debug.Print ("------------------------")
    Else

    End If

Exit_Insert:
    Set adoCMD = Nothing
    Set adoRS = Nothing

    Exit Sub

Err_Insert:
    Debug.Print "----------------"
    Debug.Print "BEGIN: Err"
    If err.Number <> 0 Then
        Msg = "Error # " & Str(err.Number) & " was generated by " _
         & err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & err.Description
        'MsgBox Msg, , "Error", err.HelpFile, err.HelpContext
        Debug.Print Msg
    End If
    Resume Exit_Insert


End Sub

following reference表示数据库类型MEMO的字段的参数类型应为adLongVarChar,而类型数据库类型TEXT的字段应为{ {1}}。

1 个答案:

答案 0 :(得分:4)

我认为这里有两个问题:

  1. 我很确定随着Jet 4.0(以及随后的ACE)的发布,所需类型已更改为adVarWChar TextadLongVarWChar Memo(用于支持多字节字符)。

  2. 文本参数必须定义(最大)长度。我没有在.CreateParameter()来电中看到这些内容。