Access中的INSERT INTO语句中断为255个字符

时间:2016-11-07 05:09:37

标签: sql vba ms-access

我在Access中有一个表单,用户可以在文本框中键入信息并点击一个运行INSERT INTO语句的按钮。即使我已将某些数据类型格式化为长文本,但我仍然遇到运行时错误3271.我希望用户能够在表单的文本框中输入超过255个字符并拥有该数据在我的表中插入一个新行:

    Dim cdb As DAO.Database
    Set cdb = CurrentDb
    Dim qdf As DAO.QueryDef
    Set qdf = cdb.CreateQueryDef("", _
            "INSERT INTO AllProjects (ProjectNumber, ProjectTitle, Description, Department, Priority, Status, [Create Date], [% Complete], [Update Notes], Leader, [Initial Target Date], [Complete Date], Category, Feedback, [Strategic Initiatives], Subcategory, [Internal/External], [Case #], Alloy, Goals, Customer, [Market Segment], [Result of Corrective Action], [Initiator], [Representative / PM / CSA], [Team Memebers], [FQA Needed], [WON / LOST / DUNNO]) VALUES (@prjnum, @title, @description, @dept, @prior, @status, @createdate, @ctlcomp, @notes, @lead, @targdate, @compdate, @cat, @feedback, @initiatives, @sub, @internal, @case, @alloy, @goals, @customer, @mktseg, @result, @initiat, @rep, @team, @FQA, @wonloss)")
    qdf.Parameters("@prjnum").Value = ProjectNumber
    qdf.Parameters("@title").Value = Me.Title
    qdf.Parameters("@description").Value = Me.Description
    qdf.Parameters("@dept").Value = Me.Department
    qdf.Parameters("@prior").Value = Me.Priority
    qdf.Parameters("@status").Value = Me.Status
    qdf.Parameters("@createdate").Value = Me.Create_Date
    qdf.Parameters("@ctlcomp").Value = Me.Ctl__Complete
    qdf.Parameters("@notes").Value = Me.Update_Notes
    qdf.Parameters("@lead").Value = Me.Leader
    qdf.Parameters("@targdate").Value = Me.Target_Date
    qdf.Parameters("@compdate").Value = Me.Complete_Date
    qdf.Parameters("@cat").Value = Me.Category
    qdf.Parameters("@feedback").Value = Me.Feedback
    qdf.Parameters("@initiatives").Value = Me.Strategic_Initiatives
    qdf.Parameters("@sub").Value = Me.Sub
    qdf.Parameters("@internal").Value = Me.Internal
    qdf.Parameters("@case").Value = Me.Case
    qdf.Parameters("@alloy").Value = Me.Alloy
    qdf.Parameters("@goals").Value = Me.Goals
    qdf.Parameters("@customer").Value = Me.Customer
    qdf.Parameters("@mktseg").Value = Me.Market_Segment
    qdf.Parameters("@result").Value = Me.result
    qdf.Parameters("@initiat").Value = Me.Initiator
    qdf.Parameters("@rep").Value = Me.RepresentativePMCSA
    qdf.Parameters("@team").Value = Me.TeamMembers
    qdf.Parameters("@FQA").Value = Me.FQANeeded
    qdf.Parameters("@wonloss").Value = Me.WonLostDunno
    qdf.Execute

2 个答案:

答案 0 :(得分:1)

您可以通过插入带有DAO对象的记录来解决此问题,而不是执行SQL语句。像这样:

Dim rs As DAO.Recordset
Set rs = cdb.OpenRecordset("AllProjects")
With rs
    .AddNew
    !ProjectNumber = ProjectNumber
    !ProjectTitle = ProjectTitle
    '...
    .Update
End With

DAO Reference on MSDN - Recordset object

即使执行SQL语句,似乎ADO也没有此问题。

您需要在 Microsoft ActiveX数据对象库中添加引用(工具 - >引用... )(选择最高版本;在我的机器它的6.1)。

Dim cmd As New ADODB.Command
Set cmd.ActiveConnection = CurrentProject.AccessConnection
'query shortened for brevity
cmd.CommandText = _
    "INSERT INTO AllProjects (ProjectNumber, ProjectTitle, Description) " & _
    "VALUES (?, ?, ?)"
cmd.Execute , Array(ProjectNumber, Me.Title, Me.Description)

ADO Reference on MSDN

答案 1 :(得分:0)

对于访问中的文本字段,最大限制为255个字符。您将无法插入超过255个字符。查看max allowed characters并按要求使用。尝试使用备注字段(备注字段65,535中的字符数。)