这里我试图将查询结果添加到组合框中,但是只要查询结果限制为一个值但不能添加它,我就可以添加到组合框中。
请帮我解决这个问题。
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
答案 0 :(得分:0)
请注意,RecordCount
并不总是返回您想要的内容,因此除非您确实需要它,否则最好在循环中避免它(在这种情况下请检查您的提供者和cusortype是否支持RecordCount)。此外,您正在尝试遍历记录集的记录(行),但您正在阅读Fields(列)。通常,要遍历记录集并将每个记录的第一个字段添加到组合框中,它将如下所示:
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
打开rs时,光标位于第一条记录上。如果没有记录,则EOF和BOF都为真。在Do循环中,rs.MoveNext
将光标前进到下一条记录。
rs.Fields(0).Value
返回光标所在记录的第一个字段的值。如果您需要不同的字段,请按编号(从零开始)或按名称(在引号中)指定。