2016-08-24 72 views
1

我有一个保存的查询,qryInsertLog这是如下:的MS Access 2013保存追加查询没有更新的所有字段

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字符串paramater像这样:

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

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

我找不出为什么更新LogEntry字段没有问题,但FNotes字段无法更新。

+0

您有参数查询问题。最后一个也没有运气?使用debug.print检查您的值。将一个dbFailonError添加到您的执行中。 – dbmitch

+1

我敢打赌这是LongText参数。 – Andre

回答

1

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

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

Dim rs As DAO.Recordset 

然后用这个Nz(Me.txtFNotes.Value, "")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 

注意你同样的事情IIf(IsNull(Me.txtFNotes.Value), "", Me.txtFNotes.Value),但更简洁。

+0

完美的作品,谢谢! –