【发布时间】:2016-06-02 09:28:13
【问题描述】:
我正在尝试从数据库中读取数据以显示在列表框中。
我有文本框和组合框:txtIndex、txtEventID、cmbServer、cmbCompany、txtMessage...
当我点击“显示列表”按钮时,数据表中的所有数据都将被读取并填充到列表框中。单击列表框行项后,所有相关信息将显示在相应的文本框和组合框中对于这种情况。
我使用 FindAll 函数实现搜索引擎,搜索结果似乎还可以。
问题是:当我点击列表框中的搜索结果时,文本框和组合框中显示的数据不正确。
下面是列出索引,不是我想要的。
这是我显示数据的代码(由 UserForm1_Click() 处理)
Private Sub Listbox1_Click()
Dim strAddress As String
Dim idx As Long
' idx = ListBox1.ListIndex + 1
For idx = 0 To ListBox1.ListCount
If ListBox1.Selected(idx) = True Then
strAddress = ListBox1.ListIndex + 1
UserForm1.txIndex.Value = Sheets("DATA").Range("A" & strAddress).Value
UserForm1.txtEventID.Value = Sheets("DATA").Range("B" & strAddress).Value
UserForm1.txtSource.Value = Sheets("DATA").Range("C" & strAddress).Value
UserForm1.cmbServer.Value = Sheets("DATA").Range("D" & strAddress).Value
UserForm1.txtMessage.Value = Sheets("DATA").Range("E" & strAddress).Value
UserForm1.cmbStatus.Value = Sheets("DATA").Range("F" & strAddress).Value
UserForm1.txtDate.Value = Sheets("DATA").Range("G" & strAddress).Value
UserForm1.txtDate.Text = VBA.Format(txtDate, "yyyy.mm.dd")
UserForm1.txtIssueNo.Value = Sheets("DATA").Range("H" & strAddress).Value
UserForm1.cmbCompany.Value = Sheets("DATA").Range("I" & strAddress).Value
UserForm1.txtErrorType.Value = Sheets("DATA").Range("J" & strAddress).Value
UserForm1.cmbPriority.Value = Sheets("DATA").Range("K" & strAddress).Value
UserForm1.txtComment.Value = Sheets("DATA").Range("L" & strAddress).Value
UserForm1.txtName.Value = Sheets("DATA").Range("M" & strAddress).Value
GoTo EndLoop
' UserForm1.Show
End If
Next idx
EndLoop:
Application.ScreenUpdating = True
Label25.Caption = "You're looking for event id: " & txtEventID.Text
End Sub
结果不正确。我想在文本框和组合框中显示与所选项目完全相关的数据,但是当我选择搜索结果(显示在第一行)时,Useform 在工作表中显示了 1 行数据,而不是搜索结果。
问题如附图所示。
【问题讨论】:
-
我理解正确吗?数据表的第一行具有值“索引”、“事件 ID”等作为列标题。然后表单填充这些而不是相关行,即
strAddress始终为 1? -
嗨,arcadeprecinct,是的,你是对的。我的第一行显示列标题。
标签: excel vba ms-access excel-formula excel-2010