0

I have a problem in MS Access and I don't know how can I solve this problem.

I have a listbox on my Form. If the use changes the value of the Listbox,a SQL query should be run and the result should be displayed is some Textboxes. For example:

The user select 'A' value From the list box then this query should be run:

SELECT XValue, YValue,Wert FROM Mytable WHERE Name='A' and ID=fzgID 

then I want to display XValue, YValue and Wert in three textboxes. The problem is may be I have more than one pair for this combination (XValue, YValue,Wert).

How can I do that? How can I Link the list box and the query to the textboxes?

thank you so much

4

2 回答 2

1

每个 ListBox 都有一个ListBox_Click()过程,该过程在单击列表框的元素时运行。

如果你做这样的事情,你可以获得你想要的东西:

Sub ListBox1_Click()
    Dim valueOfListbox As String
    valueOfListBox = ListBox1.Value
    ' **** here comes  your code ****
    ' get the actual value from the listbox and do the query

    ' change the values of the textboxes to the result of the query
End Sub

如果您的列表框的名称与“ListBox1”不同,您必须更改该过程的名称。例如,如果列表框名为“blaBox”,则它必须是blaBox_Click().

于 2013-06-25T11:00:36.087 回答
0

根据您的数据结构,我会做这样的事情。将列表框的表/查询设置为包含您要显示的所有数据。

SELECT ID, Name, XValue, YValue, Wert FROM Mytable

在列表框属性表的格式选项卡中,将列数设置为2,将列宽设置为0";2"或您的偏好。这将隐藏列表框行中的其他值。

在三个文本框中,像这样设置控制源。

Textbox1.ControlSource:=[Listbox1].[Columns](3)

Textbox2.ControlSource:=[Listbox1].[Columns](4)

Textbox3.ControlSource:=[Listbox1].[Columns](5)

您可能需要调整数字。列表框有一个名为 Columns 的属性,使您能够访问列表框不同列的值。如果您不想或不能拥有列表框中的所有数据,则可以DLookUp在每个文本框中使用该函数。

Textbox1.ControlSource:=DLookUp("XValue", "Mytable", "ID=""fzGID"" And Name=""" & [Listbox1] & """")

对 [Listbox1] 的引用将提取绑定列的值。如果绑定的列不是您正在查找的数据,那么您将需要引用一个列(即[Listbox1].[Columns](2))。

于 2013-06-25T16:45:31.977 回答