MS Access:将列表框链接到文本框

时间:2015-08-06 18:36:39

标签: forms vba ms-access textbox

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

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

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

我一直在阅读一些要阅读的内容,但其他内容为空。我在源表中有大约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

1 个答案:

答案 0 :(得分:1)

检查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