目前,我很难在数据表或窗体视图中为表单设置验证规则,这些规则会立即触发到记录中的另一个字段或另一个记录中。
我的表单旨在将记录添加到一个目标表中,其中主键列需要匹配源表的任何记录中特定字段的值。表单(和目标表)中的其余字段用于一般用户输入(一些DateTime字段、一些文本、一些十进制)。
当用户尝试释放时,我可以获得显示标准错误对话框的权限--在选择另一个字段或记录后立即输入列表中的值。显示的错误是
您输入的文本不是列表中的项. 从列表中选择一项,或输入与所列项目之一匹配的文本“
如果我重新选择一个已经选定的查找值并转到下一个记录,我将得到
您请求对表进行的更改没有成功,因为它们将在索引、主键或关系中创建重复值。更改字段中包含重复数据的数据、删除索引或重新定义索引以允许重复条目,然后再试一次。
但是,如果进入同一记录中的另一个字段,我希望该错误(或类似的)立即出现。换句话说,我希望它告诉我,在允许用户在表单或表中填写当前记录的其余部分之前,它是一个副本。
我希望选择列表仅限于目标表中以前没有出现的值。显然,如果编辑一个已经创建的条目,您应该能够保留以前的值(也就是说,该值不会被从下拉列表中排除)。
或者,如果复制了另一个有效值,则会出现一个选择对话框。
复制值 你已经用过那个值了。是否要更改此记录或先前输入的记录。 ⪡这个⪢<⪢>
如果选择了“前一个”,它将跳到指定记录中的同一个字段,为重新选择提供下拉列表(一旦完成将跳回“当前”记录并自动选择临时重复的值。
我将用表的设计细节以及表单的源代码设置来编辑这篇文章。
发布于 2017-02-09 17:05:32
在VBA中“解决”了这个问题。
Private Sub MyControl_AfterUpdate()
newval = Me.MyControl.Value
oldval = Me.MyControl.OldValue
If newval = oldval Then Exit Sub ' everything's okay
Dim rs As Object
Set rs = Me.Form.Recordset
whereclause = "MyControl = '" & newval & "'"
qry = "SELECT COUNT(*) as c FROM MyQuery WHERE " & whereclause
qrs = CurrentDb.OpenRecordset(qry)
If qrs.Count = 1 Then cnt = qrs(0).Value
If cnt >= 1 Then
selval = MsgBox("Would you like to keep your selection for this record?" & vbCrLf & "[yes = change previous record's MyField; no = change MyField for this record]", vbYesNo Or VbMsgBoxStyle.vbExclamation Or vbSystemModal Or vbDefaultButton2 Or vbMsgBoxSetForeground, "Duplicate MyField selection encountered")
If selval = vbYes Then
' set focus to the other entry, preserving selection here
thissel = Me.MyControl.ListIndex
Me.MyControl.Value = "temp" ' if a string is okay & so we can jump away from this record
thisloc = Me.Form.CurrentRecord ' record number
rs.Findfirst (whereclause)
thatloc = Me.Form.CurrentRecord
Debug.Print (thisloc & "now ; was" & thatloc)
Me.MyControl.Value = "invalid"
DoCmd.GoToRecord , , acGoTo, thisloc ' jump to the new row
Me.MyControl.Value = newval
DoCmd.GoToRecord , , acGoTo, thatloc ' jump to the one to change
If thissel <= 0 Then thissel = 1 ' may not be useful, given the error handling below
On Error Resume Next
Me.MyControl.ListIndex = thissel - 1
On Error GoTo 0
Me.MyControl.Dropdown
ElseIf selval = vbNo Then
Me.MyControl.Value = Me.MyControl.OldValue
Me.MyControl.Undo ' for some reason this doesn't clear the "dirty" bit, resulting in the edit pencil showing up for the row.
Me.MyControl.SetFocus
End If
Else
Debug.Print ("There were no matches! Das ist gut")
End If
End Sub
剩余问题
https://stackoverflow.com/questions/42124168
复制