我已经花了大约一周的时间来解决这个表单的问题,几乎找到了一个解决方案,但我遇到了障碍。我有一个具有多个功能的大表单,其中一个功能是编辑子表单,其中一个子表单包含代码列表和其他各种数据。当我单击编辑按钮时,它会自动用所选数据填充这些框。当我单击更新按钮时,该函数起作用,但当我单击保存按钮时,它实际上并不保存数据。代码是:
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
发布于 2013-08-05 13:25:17
您只是将值复制到字段中。它们不会以任何方式绑定到子窗体的记录集。因此,要保存它们,只需颠倒过程:
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-09-10 18:05:51
在我看来,您正在考虑强制保存当前绑定的表单的数据。
If Me.Dirty Then Me.Dirty = False
这条语句实际上是说“如果这个窗体/报表上有未保存的数据--保存它”
您还可以引用您的子窗体
If subform.Form.Dirty Then subform.Form.Dirty = False
它将做与Fabio的建议相同的事情,但我发现使用绑定表单比使用记录集方法更可靠。
https://stackoverflow.com/questions/18058975
复制相似问题