【发布时间】: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