MS Access ListBox 选定项目

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

MS Access ListBox Selected item

问题

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

假设:我想显示“Alex”。

列表框的值如下:

ID           姓名
1             Tom
2             Peter
3             Alex
4             Susanne

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

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

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

Dim DBSQL As New ADODB.Connection
Dim RSSQL As New ADODB.Recordset

DBSQL.ConnectionString = CONSTRING
DBSQL.Open

SQL = "SELECT [ID], [Name] FROM [Clients] ORDER BY [Name] ASC; "
RSSQL.Open SQL, DBSQL, adOpenKeyset, adLockOptimistic
Set Forms!Trade.Erfassung_Client.Recordset = RSSQL
RSSQL.Close

Forms("Trade").Controls("Erfassung").Column(0) -> 导致运行时错误 451
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:

ID           Name
1             Tom
2             Peter
3             Alex
4             Susanne

Normally it works with

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

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

Dim DBSQL As New ADODB.Connection
Dim RSSQL As New ADODB.Recordset

DBSQL.ConnectionString = CONSTRING
DBSQL.Open


SQL = "SELECT [ID], [Name] FROM [Clients] ORDER BY [Name] ASC; "
RSSQL.Open SQL, DBSQL, adOpenKeyset, adLockOptimistic
Set Forms!Trade.Erfassung_Client.Recordset = RSSQL
RSSQL.Close


Forms("Trade").Controls("Erfassung").Column(0)  -> results in runtime error 451
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属性:

Me!Erfassung.Value = 3

或者,如果来自外部:

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

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

Me!Erfassung.Value = 3

or, if from outside:

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

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:

确定