简体   繁体   English

如何将查询结果添加到组合框

[英]How to add Query results to Combobox

Here I am trying to add results of query into combobox but am able to add to combobox wherever the query result restricted to one value but more than not able to add it. 在这里,我试图将查询结果添加到组合框,但是无论查询结果限制为一个值,还是无法添加,都可以将其添加到组合框。

Please help me on this issue. 请帮我解决这个问题。

Private Sub CommandButton1_Click()
Dim i As Integer
Dim rs As ADODB.Recordset
Dim vendor As String
Dim season As String
Dim ms As String
Dim JS As String

With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Show
For i = 1 To .SelectedItems.Count
Me.TextBox1.Value = .SelectedItems(i)
Next
End With
fpath = Me.TextBox1.Value
'settings.savesettings
'fpath = UserForm1.TextBox1.Value
vendor = "Select distinct [Vendor Code] from [Data$] where [Vendor Code] is not null"
season = "Select distinct [Season] from [Data$] where [Season] is not null"
ms = "Select distinct [Material Style] from [Data$] where [Material Style] is not null"
JS = "Select distinct [JDE Style] from [Data$] where [JDE Style] is not null"

Call connectiontosql.connectiontosql
Set rs = New ADODB.Recordset

rs.Open vendor, cn, adOpenKeyset, adLockOptimistic
For i = 0 To rs.RecordCount - 1
Me.ComboBox1.AddItem rs.Fields(i).Value
Next
rs.Close

rs.Open season, cn, adOpenKeyset, adLockOptimistic
For i = 0 To rs.RecordCount - 1
Me.ComboBox2.AddItem rs.Fields(i).Value
Next
rs.Close
Call connectiontosql.connectiontosql

rs.Open ms, cn, adOpenKeyset, adLockOptimistic

i = 1
Do Until i = rs.RecordCount
Me.ComboBox3.AddItem rs.Fields(i).Value
i = i + 1
Loop
Call connectiontosql.connectiontosql

rs.Open JS, cn, adOpenKeyset, adLockOptimistic
For i = 0 To rs.RecordCount - 1
Me.ComboBox4.AddItem rs.Fields(i).Value
Next
rs.Close

End Sub

Note that RecordCount doesn't always return what you want, so it's best to avoid it in loops unless you really need it (in which case check that your providor and cusortype support RecordCount). 请注意, RecordCount并不总是返回您想要的内容,因此除非真正需要它,否则最好避免循环执行(在这种情况下,请检查您的提供者和cusortype是否支持RecordCount)。 Also, you're attempting to loop through the records (rows) of the recordset, but you're reading the Fields (columns). 另外,您试图遍历记录集的记录(行),但正在阅读字段(列)。 Generally, to loop through a recordset and add the first field of each record to a combobox, it would look like this: 通常,要遍历记录集并将每个记录的第一个字段添加到组合框,它看起来像这样:

If Not rs.EOF And Not rs.BOF Then
    Do
        Me.combobox3.AddItem rs.Fields(0).Value
        rs.MoveNext
    Loop Until rs.EOF
End If

When you open rs, the cursor is on the first record. 当您打开rs时,光标在第一条记录上。 If there are are no records, both EOF and BOF will be true. 如果没有记录,则EOF和BOF都为true。 In the Do Loop, rs.MoveNext advances the cursor to the next record. 在“执行循环”中, rs.MoveNext将光标移至下rs.MoveNext记录。

rs.Fields(0).Value returns the value of the first field for the record the cursor is on. rs.Fields(0).Value返回光标所在记录的第一个字段的值。 If you need a different field, specify it by number (starting with zero) or by name (in quotes). 如果需要其他字段,请按数字(以零开头)或名称(用引号引起来)指定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM