【问题标题】:Parameterized INSERT with Memo and Text Fields in VBA / MS-ACCESS?VBA / MS-ACCESS中带有备注和文本字段的参数化INSERT?
【发布时间】:2013-06-03 18:23:25
【问题描述】:

我正在尝试使用 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:数字(长整数)
  • 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 表示数据库类型为MEMOs 的字段应具有adLongVarChar 的参数类型,而该类型的数据库类型TEXT 应具有adVarChar 的类型。

【问题讨论】:

  • 你试过adLongVarWCharadVarWChar吗?
  • 我都试过了,假设LongVarWChar 是备注字段,adVarWChar 是文本字段,但是没有用。

标签: sql vba ms-access sql-insert


【解决方案1】:

我认为这里有两个问题:

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

  2. 文本参数必须定义一个(最大)长度。我在你的.CreateParameter() 电话中没有看到这些。

【讨论】:

猜你喜欢
  • 2017-08-14
  • 2020-02-13
  • 1970-01-01
  • 2017-06-12
  • 2018-08-14
  • 2016-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多