VBA - Run Time Error 3271 using DAO object

家住魔仙堡 提交于 2019-11-28 14:03:12
HansUp

You're facing a limitation of Access SQL text parameters. They can not accommodate string values longer than 255 characters.

Here is a simple example which demonstrates the problem.

Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strUpdate As String
Dim strLongString As String
strLongString = String(300, "x")
strUpdate = "UPDATE tblFoo SET memo_field = [pLongString] WHERE id=2;"
Set db = CurrentDb
Set qdf = db.CreateQueryDef(vbNullString, strUpdate)
qdf.Parameters("pLongString").Value = strLongString
qdf.Execute dbFailOnError

That code triggers error #3271, "Invalid property value." ... the same error you're seeing.

If I change the UPDATE statement to include a PARAMETERS clause like this ...

strUpdate = "PARAMETERS [pLongString] LongText;" & vbCrLf & _
    "UPDATE tblFoo SET memo_field = [pLongString] WHERE id=2;"

... the outcome is still error #3271.

I don't believe there is any way to overcome that Access SQL limitation.

So if the length of your text parameter value is greater than 255 characters, you need a different method.

A DAO.Recordset approach is a simple alternative to store long text strings in a field.

Dim rs As DAO.Recordset
Dim strSelect
strSelect = "SELECT id, memo_field FROM tblFoo WHERE id=2;"
Set rs = db.OpenRecordset(strSelect)
With rs
    If Not (.BOF And .EOF) Then
        .Edit
        !memo_field.Value = strLongString
        .Update
    End If
    .Close
End With

DAO Reference on MSDNRecordset object

You should explicitly define the query parameters, at least those with LongText data type. Otherwise Access has to guess their data type.

You can do this in the query design editor, click on the "Parameters" button.

Or in SQL view, creating a PARAMETERS clause

PARAMETERS [parLongString] LongText;
UPDATE myTable
SET LongString = [parLongString]
WHERE ...
Zev Spitz

In addition to HansUp's answer, it seems that ADO doesn't have this issue.

Add a reference (Tools -> References...) to the Microsoft ActiveX Data Objects library (choose the highest version; on my machine it's 6.1).

Dim cmd As New ADODB.Command
Set cmd.ActiveConnection = CurrentProject.AccessConnection
cmd.CommandText = "qryUpdateFile"

'the rest of the parameter values need to be included in the array
'omitted for brevity
cmd.Execute , Array(txtboxUpdateConversionName.Value, txtboxZipFileNameLogic.Value)

ADO Reference on MSDN

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!