2

我已经敲定这个表格的问题大约一个星期了,几乎已经找到了解决方案,但我已经碰壁了。我有一个具有多种功能的大型表单,其中一个功能是编辑一个包含代码列表和其他各种数据的子表单。当我单击编辑按钮时,它会自动用所选数据填充框。当我单击更新按钮时,该功能起作用,但当我单击保存按钮时,它实际上并没有保存数据。代码是:

Private Sub cmdEdit_Click()
'check whether there exists data in list
If Not (Me.TableSub.Form.Recordset.EOF And Me.TableSub.Form.Recordset.BOF) Then
'Get data to text box control
With Me.TableSub.Form.Recordset
    Me.text_key = .Fields("KW")
    Me.txt_code = .Fields("Code")
    Me.combo_source = .Fields("Source")
    'Store id of student in tag of text id in case id is modified
    Me.txt_code.Tag = .Fields("Code")
    'Change caption of button add to Update
    Me.cmdAdd.Caption = "Update"
    'disable button edit
    Me.cmdEdit.Enabled = False
End With
End If
End Sub

这是保存或添加按钮的代码。

Private Sub cmdAdd_Click()
'when we click on button Add there are two options
'1. For insert
'2. For Update
If Me.txt_code.Tag & "" = "" Then
    'this is for insert new
    'add data to table
    CurrentDb.Execute "INSERT INTO KWTable(KW, Source, Code) " & _
        " VALUES('" & Me.text_key & "','" & Me.combo_source & "','" & _
        Me.txt_code & "')"

Else
'otherwise (Tag of txtID store the id of student to be modified)
CurrentDb.Execute "UPDATE KWTable " & _
" SET KW='" & Me.text_key & "'" & _
", Code='" & Me.txt_code & "'" & _
", Source='" & Me.combo_source & "'" & _
" WHERE KW='" & Me.text_key & "'"
End If
'clear form
cmdClear_Click
'refresh data in list on form
TableSub.Form.Requery

End Sub
4

2 回答 2

1

您只是将值复制到字段中。它们不会以任何方式绑定到子表单的记录集。因此,要保存它们,只需反转过程:

With Me.TableSub.Form.Recordset
    .Edit
    .Fields("KW") = Me.text_key
    .Fields("Code") = Me.txt_code
    .Fields("Source") = Me.combo_source
    .Fields("Code") = Me.txt_code.Tag
    .Update
End With
于 2013-08-05T13:25:16.773 回答
0

在我看来,您正在考虑强制保存当前绑定表单的数据。

If Me.Dirty Then Me.Dirty = False

该声明实质上是说“如果此表单/报告上有未保存的数据 - 保存它”

您还可以参考您的子表单

If subform.Form.Dirty Then subform.Form.Dirty = False

它将与 Fabio 的建议做同样的事情,但我发现绑定表单比记录集方法更可靠。

于 2013-09-10T18:05:51.017 回答