MS Access:Link 列表框到文本框

MS Access: Link listbox to textbox

我有一个文本框和一个列表框,一个用于在帮助表单中输入新主题,而另一个用于查找这些新主题。我希望能够同时在文本框和列表框中显示完成的主题以编辑或查找以及在帮助表单中写入新记录。列表框提供了查看现在有哪些记录的功能。 我发现如果我什么都不输入并转到新记录,prev/next 按钮将停止工作,也许我需要添加一个控件以防止它冻结或刷新?通常我按 esc 键退出新的记录编辑,然后按 return 给其他人,但这并不像往常一样工作。

或者我还可以如何指向列表框的当前记录源?

I currently have this code:

Private Sub List35_AfterUpdate()
  DoCmd.GoToRecord acDataForm, "Help Form_Editor2", acGoTo, Me.List35.ListIndex + 1
    Me.List35 = Me.List35.Column(0, Form.CurrentRecord - 1)
    Dim index As Integer
    index = Form.CurrentRecord - 1
    Me.Text53 = Me.List35.Column(Me.List35.ListIndex + 1, index)
End Sub

我一直在获取一些要阅读的项目,但其他项目都是空的。我在源 table 中有大约 8 个项目...出了什么问题?为什么会有空值?

更新后的另一个问题。设置代码后,当我允许在表单上添加和编辑时,记录集从新开始。该代码按应有的方式显示列表项,但其他项不会从请求的列表框项中激活。什么可以解决这个问题?

Private Sub List35_AfterUpdate()
    Dim myTitle As String
    With Me.List35
        If .ListIndex > -1 Then
            'Use this one if you are using bound column
            myTitle = .Column(1, Form.CurrentRecord)

            'use this if you want something other than the bound column
            'and you have more than one column in the list (hidden or not)
            'nId = .Column(1, .ListIndex)

           Me.RecordSource = "SELECT * FROM FormsHelpTable WHERE HelpTitle = '" & myTitle & "'"
            Me.Text53.Value = myTitle
        Else
           Me.RecordSource = "SELECT * FROM FormsHelpTable WHERE HelpTitle IS NULL"
            'Me.Text53.Value = "(New)"
        End If

    End With

   Me.Requery
End Sub

这将检查 ListIndex。如果您没有选择任何内容,它将是 -1。

Private Sub List35_AfterUpdate()
    Dim index As Integer

    With Me.List35
        If .ListIndex > -1 Then
            DoCmd.GoToRecord acDataForm, "Help Form_Editor2", acGoTo, .ListIndex + 1
            .Value = .Column(0, Form.CurrentRecord - 1)
            index = Form.CurrentRecord - 1
            Me.Text53 = .Column(.ListIndex + 1, index)
        End If
    End With

End Sub

我不确定你所有的代码都试图做什么,所以除了将对 List35 的所有引用减少到单个 With 语句之外,我没有做任何其他调整。

我通常会这样做:

Private Sub List35_AfterUpdate()
    Dim nId As Long
    With Me.List35
        If .ListIndex > -1 Then                
            'Use this one if you are using bound column
            nId = .Value 

            'use this if you want something other than the bound column
            'and you have more than one column in the list (hidden or not)
            'nId = .Column(1, .ListIndex)

            Me.RecordSource = "SELECT * FROM TableName WHERE Id = " & nId
        Else
            Me.RecordSource = "SELECT * FROM TableName WHERE Id IS NULL"
        End If

    End With

    Me.Requery

End Sub