MS Access 2013 保存的附加查询未更新所有字段
MS Access 2013 saved append query not updating all fields
我有一个保存的查询,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
当此代码为 运行 时,table 的 FNotes 字段不会更新。其他三个字段按预期更新。 FNotes 是唯一不需要的字段。我像这样为 FNotes 参数硬编码了一个字符串:
qdf.Parameters("FNotesPar").Value = "why doesn't this work"
而不是使用表单控件值,得到了相同的结果:该字段只是不更新。当我 运行 从访问对象 window 查询并从提示中提供参数值时,它工作得很好。当我创建绑定到 table 的表单时,它似乎也能正常工作。
我想不通为什么更新LogEntry字段没有问题,但是FNotes字段更新失败。
通过 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)
相同的内容,但更简洁。
我有一个保存的查询,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
当此代码为 运行 时,table 的 FNotes 字段不会更新。其他三个字段按预期更新。 FNotes 是唯一不需要的字段。我像这样为 FNotes 参数硬编码了一个字符串:
qdf.Parameters("FNotesPar").Value = "why doesn't this work"
而不是使用表单控件值,得到了相同的结果:该字段只是不更新。当我 运行 从访问对象 window 查询并从提示中提供参数值时,它工作得很好。当我创建绑定到 table 的表单时,它似乎也能正常工作。
我想不通为什么更新LogEntry字段没有问题,但是FNotes字段更新失败。
通过 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)
相同的内容,但更简洁。