MS Access ListBox 选定项目

huangapple go评论97阅读模式
英文:

MS Access ListBox Selected item

问题

如何使Access中的列表框显示列表的特定值?

假设:我想显示“Alex”。

列表框的值如下:

  1. ID 姓名
  2. 1 Tom
  3. 2 Peter
  4. 3 Alex
  5. 4 Susanne

通常情况下,可以使用以下代码:

  1. Forms("Trade").Controls("Erfassung").Column(0) = 3 列表框显示“3, Alex

但是,使用ADODB.Connection来填充列表框时,它不再起作用。

  1. Dim DBSQL As New ADODB.Connection
  2. Dim RSSQL As New ADODB.Recordset
  3. DBSQL.ConnectionString = CONSTRING
  4. DBSQL.Open
  5. SQL = "SELECT [ID], [Name] FROM [Clients] ORDER BY [Name] ASC; "
  6. RSSQL.Open SQL, DBSQL, adOpenKeyset, adLockOptimistic
  7. Set Forms!Trade.Erfassung_Client.Recordset = RSSQL
  8. RSSQL.Close
  9. Forms("Trade").Controls("Erfassung").Column(0) -> 导致运行时错误 451
  10. Forms("Trade").Controls("Erfassung").Value = 3 -> 列表框显示“4, Susanne”因为Susanne是排序后列表的第3个值

如何使列表框显示“3, Alex”?(而不是通过查找Alex的位置来实现的变通方法)

英文:

How can I make a listbox in access display a certain value of the list?

Assumtion: I want to display “Alex”

Listbox value are the following:

  1. ID Name
  2. 1 Tom
  3. 2 Peter
  4. 3 Alex
  5. 4 Susanne

Normally it works with

  1. Forms("Trade").Controls("Erfassung").Column(0) = 3 Listbox shows 3, Alex

But using ADODB.Connection to fill the listbox its not working anymore.

  1. Dim DBSQL As New ADODB.Connection
  2. Dim RSSQL As New ADODB.Recordset
  3. DBSQL.ConnectionString = CONSTRING
  4. DBSQL.Open
  5. SQL = "SELECT [ID], [Name] FROM [Clients] ORDER BY [Name] ASC; "
  6. RSSQL.Open SQL, DBSQL, adOpenKeyset, adLockOptimistic
  7. Set Forms!Trade.Erfassung_Client.Recordset = RSSQL
  8. RSSQL.Close
  9. Forms("Trade").Controls("Erfassung").Column(0) -> results in runtime error 451
  10. Forms("Trade").Controls("Erfassung").value = 3 -> Listbox shows 4, Susanne because Susanne is the 3rd value in the sorted List

How can I make the listbox display “3, Alex” ? (without the workaround figuring out which position Alex is)

答案1

得分: 1

如果它绑定到第一列(并且应该是这样),则设置其Value属性:

  1. Me!Erfassung.Value = 3

或者,如果来自外部:

  1. Forms!Trade!Erfassung.Value = 3
英文:

If it is bound to the first column (and it should), then set its Value property:

  1. Me!Erfassung.Value = 3

or, if from outside:

  1. Forms!Trade!Erfassung.Value = 3

答案2

得分: 0

If using ADODB to add items to a listbox, the listbox itself needs to be set to "Value List". Please refer to Microsoft's Documentation.

As Gustav mentioned, you just do:

Me!Erfassung.Value = "Alex".

Also, don't forget that listboxes are zero-indexed.

英文:

If using ADODB to add items to a listbox, the listbox itself needs to be set to "Value List". Please refer to Microsoft's Documentation

As Gustav mentioned, you just do

  1. Me!Erfassung.Value = "Alex"

Also don't forget that listboxes are zero-indexed.

huangapple
  • 本文由 发表于 2023年5月10日 20:43:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218584.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定