清除绑定到主键问题的组合框



我目前正在尝试将一个事件添加到"保存"按钮中,该按钮查询和更新记录,然后清除表单上的所有控件,但是填充所有这些控件的组合框 - 也绑定到表的主键 - 除非它有值,否则不会清除。

我试过了

cboName.Clear()      'This method doesn't exist
Me.cboName.Clear()   'Same issue as above
Me.cboName = Null    'The table field must have a value error
Me.cboName = ""      'Also must have a value error
Dim ctl As Control          'This returns an invalid use of index
For Each ctl in Me.Controls
Select Case TypeName(ctl)
Case "ComboBox"
ctl.ListIndex = -1
End Select
Next ctl

这是保存按钮代码:

Private Sub Save_Click()
Dim db As DAO.Database
Dim rst As Recordset
Dim rec As Recordset

Set db = CurrentDb
Set rst = db.OpenRecordset( _
"SELECT Count(*) FROM [Records] WHERE [Name] = '" & [Forms]![Record Form]!cboName.Value & "'")
If rst.Fields(0) > 0 Then
Set rec = db.OpenRecordset("SELECT * FROM [Records] WHERE [Name] = '" & Me.cboName.Value & "'")
rec.Edit
rec("Date1") = Me.Date1.Value
rec("Date2") = Me.Date2.Value
rec("Check1") = Me.Check1.Value
rec("Check2") = Me.Check2.Value
rec("Incidents") = Me.Incidents.Value
rec.Update
rec.Close

Dim ctl As Control ' Removed MSForms.
For Each ctl In Me.Controls
Select Case TypeName(ctl)
Case "TextBox"
ctl.Value = ""
Case "CheckBox", "ToggleButton" ' Removed OptionButton
ctl.Value = False
Case "OptionGroup" ' Add OptionGroup
ctl = Null
Case "OptionButton" ' Add OptionButton
' Do not reset an optionbutton if it is part of an OptionGroup
If TypeName(ctl.Parent) <> "OptionGroup" Then ctl.Value = False
Case "ListBox"
ctl.ListIndex = -1
End Select
Next ctl
Me.cboName.RowSource = ""
Else
DoCmd.RunCommand (acCmdRecordsGoToNew)
End If

以及 cboName 的代码:

Private Sub cboName_AfterUpdate()
Dim dQry As String
Dim dupeItems As String

dQry = Me.cboName.Value

On Error Resume Next
dupeItems = DJoin("[Incidents] & '- out:' & [TimeOut] & ' / in:' & [TimeIn]", "[Outage Records]", "[Name] = '" & dQry & "'", vbCrLf)

If dupeItems <> "- out: / in:" Then
Me.Incidents.Value = dupeItems
Else
Me.Incidents.Value = Nothing
End If
End Sub

以及 cboName 的 RowSource:

SELECT [Outage Records].[Name], [Outage Records].[Date1], [Outage Records].[Date2] FROM [Outage Records] ORDER BY [Name]; 

我最好的选择是取消绑定并通过 vba 将 cboName 值添加到表中?

谢谢!

在我当前的数据库开始损坏后,我复制了数据库并将 cboName 更改为未绑定,并使其填充绑定文本框。

似乎像一个魅力。

谢谢!

最新更新