【问题标题】:MS Access 2013 saved append query not updating all fieldsMS Access 2013 保存的追加查询未更新所有字段
【发布时间】:2023-03-21 14:04:01
【问题描述】:

我有一个保存的查询,qryInsertLog,如下:

PARAMETERS UserIDPar Long, UnitIDPar Long, LogEntryPar LongText, FNotesPar LongText;
INSERT INTO tblLogBook ( UserID, UnitID, LogEntry, FNotes )
SELECT [UserIDPar] AS Expr1, [UnitIDPar] AS Expr2, [LogEntryPar] AS Expr3, [FNotesPar] AS Expr4;

我正在尝试在未绑定表单上单击保存按钮时运行此查询,其中参数是从表单控件收集的。我的保存按钮的 VBA 代码是:

Private Sub cmdSave_Click()

Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim okToSave As Boolean

If Me.cboUser.Value = 0 Or IsNull(Me.cboUser.Value) Then
    MsgBox "You must choose a user. Record not saved."
    okToSave = False
ElseIf Me.cboUnit.Value = 0 Or IsNull(Me.cboUnit.Value) Then
    MsgBox "You must choose a unit. Record not saved."
    okToSave = False
ElseIf Me.txtLogEntry.Value = "" Or IsNull(Me.txtLogEntry.Value) Then
   MsgBox "You must have somtehing to log. Record not saved."
    okToSave = False
Else
    okToSave = True
End If

Set db = CurrentDb
Set qdf = db.QueryDefs("qryInsertLog")

qdf.Parameters("UserIDPar").Value = Me.cboUser.Value
qdf.Parameters("UnitIDPar").Value = Me.cboUnit.Value
qdf.Parameters("LogEntryPar").Value = Me.txtLogEntry.Value
qdf.Parameters("FNotesPar").Value = IIf(IsNull(Me.txtFNotes.Value), "", Me.txtFNotes.Value)

If okToSave Then
     qdf.Execute
End If

qdf.Close
Set qdf = Nothing

End Sub

运行此代码时,表的 FNotes 字段不会更新。其他三个字段按预期更新。 FNotes 是唯一不需要的字段。我为 FNotes 参数硬编码了一个字符串,如下所示:

qdf.Parameters("FNotesPar").Value = "why doesn't this work"

而不是使用表单控件值,并得到相同的结果:该字段只是不更新​​。当我从“访问对象”窗口运行此查询并从提示中提供参数值时,它工作得很好。当我创建绑定到表格的表单时,它似乎也可以正常工作。

我不明白为什么更新 LogEntry 字段没有问题,但 FNotes 字段更新失败。

【问题讨论】:

  • 您有参数查询问题。最后一个也没有运气?使用 debug.print 检查您的值。将 dbFailonError 添加到您的 Execute。
  • 我敢打赌又是 LongText 参数。

标签: ms-access vba ms-access-2013


【解决方案1】:

通过DAO.Recordset 而不是DAO.QueryDef 添加新记录。

首先,包含这个声明...

Dim rs As DAO.Recordset

然后在Set db = CurrentDb ....之后使用这个。

Set rs = db.OpenRecordset("tblLogBook")
With rs
    If okToSave Then
        .AddNew
        !UserID = Me.cboUser.Value
        !UnitID = Me.cboUnit.Value
        !LogEntry = Me.txtLogEntry.Value
        !FNotes = Nz(Me.txtFNotes.Value, "")
        .Update
    End If
    .Close
End With

注意Nz(Me.txtFNotes.Value, "") 提供与IIf(IsNull(Me.txtFNotes.Value), "", Me.txtFNotes.Value) 相同的功能,但更简洁。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 2016-01-18
    相关资源
    最近更新 更多