【问题标题】:Cannot setfocus to control after subform requery子表单重新查询后无法设置焦点来控制
【发布时间】:2021-11-15 03:49:25
【问题描述】:

我有一个带有子表单的简单表单。在主窗体中是一个供用户扫描 UPC 代码的字段。在记录集中找到 UPC,然后请求子表单以显示扫描的项目。子集中的最后一个命令将焦点重新集中到主窗体中的 UPC 字段以进行下一次扫描。除 setfocus 外,一切正常。尝试反编译& C/R 也全部导入新数据库。

Dim db As Database
Dim rstManifest As Recordset
Dim strUPC As String
Dim strLotNum As String
Dim strCriteria As String

Set db = CurrentDb
Set rstManifest = db.OpenRecordset("tblManifest", dbOpenDynaset)
strUPC = Me.UPCScan
strLotNum = Me.LotNum
strCriteria = "[UPC] = '" & strUPC & "' And [Scanned] = False"

With rstManifest
    .MoveFirst
    .FindFirst strCriteria
    If rstManifest.NoMatch Then
        MsgBox "UPC Item " & strUPC & " not found", vbOKOnly, "Try Again"
        GoTo Cleanup
    Else
        .Edit
        !LotNum = Me.LotNum
        !DateItemAdded = Now()
        !Scanned = True
        .Update
        Me!Manifest.Form.Requery
    End If
End With

Cleanup:
If IsNull(Me.LotNum Or Me.LotNum = "") Then
    Me.LotNum = strLotNum
End If
Me!UPCScan = Null
Me.UPCScan.SetFocus 'This line does not work
'DoCmd.GoToControl "UPCScan" 'Does not work either

rstManifest.Close
Set rstManifest = Nothing
Set db = Nothing

【问题讨论】:

    标签: vba ms-access setfocus


    【解决方案1】:

    您可能可以使用 RecordsetClone 及其自动更新表单:

    Dim rstManifest As DAO.Recordset
    Dim strUPC As String
    Dim strLotNum As String
    Dim strCriteria As String
    
    Set rstManifest = Me!Manifest.Form.RecordsetClone
    strUPC = Me.UPCScan
    strLotNum = Nz(Me.LotNum)
    strCriteria = "[UPC] = '" & strUPC & "' And [Scanned] = False"
    
    With rstManifest
        .MoveFirst
        .FindFirst strCriteria
        If rstManifest.NoMatch Then
            MsgBox "UPC Item " & strUPC & " not found", vbOKOnly, "Try Again"
            GoTo Cleanup
        Else
            .Edit
            !LotNum = Me.LotNum
            !DateItemAdded = Now()
            !Scanned = True
            .Update
        End If
    End With
    
    Cleanup:
    rstManifest.Close
    If Nz(Me.LotNum) = "" And strLotNum <> "" Then
        Me.LotNum = strLotNum
    End If
    Me!UPCScan = Null
    Me.UPCScan.SetFocus
    

    或尝试:

    Me.SetFocus
    Me!UPCScan.SetFocus
    

    【讨论】:

    • 让我澄清一下:rstManifest 是一个包含 28K 项的表。在这种形式中,用户在一个字段中输入批号,然后按 Enter 键移动到 UPC 字段。仅在扫描在表中找到更新为正在扫描的项目后,才会填充子表单。所以表单本身不是记录集。
    • 我明白了。您可以使用 MasterLinkFields 过滤 UPC 的子表单。和/或首先将焦点设置在主窗体上。请查看编辑后的答案。
    • 谢谢...这是 UPCScan 的更新后代码。唯一的其他控件是 LotNo (tab0)。当扫描器向 UPCScan (tab1) 控件发送输入操作时,它会执行对表的记录更改,请求然后将焦点更改回 LotNo,这是正常的。我试过你最后的建议,没有用。由于 LotNo 是必需的数据(尝试在 LotNo 为空时进行扫描会产生错误消息),因此我不能使用该控件的 OnFocus 或 OnEnter 来更改焦点。我可能只需要以不同的方式重写它。
    • 这里有一些有趣的东西。我注意到代码运行后,我可以按 Tab 键并返回 UPCScan 控件。想知道活动控件是什么,我插入了代码来确定活动控件。奇怪的是,它是 UPCScan,但 curr 在
    • 上一个控件。然后我在 SetFocus 行之后插入了一个 SendKeys "{TAB}" 并且它起作用了。虽然我不喜欢 SendKeys,但我现在就使用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 2016-07-09
    • 2022-09-30
    • 2015-04-26
    • 2010-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多