我有一个组合框,可以在首次使用时构建它的列表。我知道我想要的方式" NotInList"表现不常见 - 我不想浪费将物品添加到与所需条目分开的表格中,但是我仍然想要警告一件物品没有被记录下来已经使用,因此用户在接受条目之前必须三思而后行。
用户添加项目后,下次会自动显示在列表中,因为组合框的数据源如下:
SELECT tbl_SP.PROGRAM
FROM tbl_SP
GROUP BY tbl_SP.PROGRAM
HAVING (((tbl_SP.PROGRAM) Is Not Null And (tbl_SP.PROGRAM)<>""));
我试过了:
Private Sub cmbPROGRAM_NotInList(NewData As String, Response As Integer)
If MsgBox("'" & Chr(34) & NewData & Chr(34) & " hasn't been used yet. Add to list? ", vbQuestion + vbYesNo, "Add - " & NewData & "?") = vbYes Then
Response = acDataErrAdded
End If
End Sub
但当然,Access希望该项目在发布错误之前实际存在。并且......如果我将LimitToList设置为&#34; No&#34;然后用户没有收到警告。
好的,我尝试了这个,如果用户选择YES则工作正常,但当用户选择&#34; NO&#34;
时会变得更复杂Public Function ReturnsRecords(strSQL As String) As Boolean
Dim d As DAO.Database
Dim arr(1 To 3) As DAO.Recordset
'Dim rs As DAO.Recordset
'assume 3 items in array above
Set d = CurrentDb
Set arr(1) = d.OpenRecordset(strSQL)
' MsgBox "Record Count is " & arr(1).RecordCount
If arr(1).RecordCount > 0 Then
ReturnsRecords = True
Else
ReturnsRecords = False
End If
Set d = Nothing
End Function
Private Sub cmbPROGRAM_BeforeUpdate(Cancel As Integer)
Dim strSQL As String
strSQL = "Select * from LU_PROGRAM where PROGRAM ='" & Me.cmbPROGRAM & "'"
If ReturnsRecords(strSQL) = False Then
If MsgBox("'" & Chr(34) & Me.cmbPROGRAM & Chr(34) & " hasn't been used yet. Add to list? ", vbQuestion + vbYesNo, "Add - " & Me.cmbPROGRAM & "?") = vbNo Then
Cancel = True
' how do I reset this? Me.cmbPROGRAM.Text = Null
End If
End If
End Sub
如果用户选择NO,如何清除组合框?如果我选择me.undo,那将取消所有条目,但我只想清除组合框。
顺便提一下,表单完全不受约束,在用户选择&#34; Save&#34;
之前不接受参赛作品答案 0 :(得分:0)
首先,我不太确定你想要达到的目标......
然后,教育用户按 Escape 取消。这是操作Access应用程序时的必备智慧。
要使代码生效,您无法在 BeforeUpdate 事件中更改控件的内容。因此,请尝试 AfterUpdate 事件:
Me!cmbPROGRAM.Text = ""
或:
Me!cmbPROGRAM.Value = Null