怕重复请直接用ComboBox代码如下:Public Sub CreateComboBoxItems(ByVal TableName As String, _ ByVal FieldName As String, TargetComboBox As ComboBox)
Dim rs As New ADODB.Recordset Dim tmpstr As String
tmpstr = TargetComboBox.Text rs.Open "SELECT " & FieldName & " FROM " & TableName & " GROUP BY " & FieldName, SYScnn Do Until rs.EOF If (Not IsNull(rs.Fields(FieldName))) And rs.Fields(FieldName) <> "" Then If Not MemberComboBox(TargetComboBox, rs.Fields(FieldName)) Then TargetComboBox.AddItem rs.Fields(FieldName) End If End If rs.MoveNext Loop rs.Close TargetComboBox.Text = tmpstr End Sub Private Function MemberComboBox(ByVal TargetComboBox As ComboBox, ByVal Targetstr As String) As Boolean Dim i As Integer
MemberComboBox = False
i = 0 Do Until i = TargetComboBox.ListCount If TargetComboBox.List(i) = Targetstr Then MemberComboBox = True Exit Do End If i = i + 1 Loop End Function
Dim Rs_Dm_Spdm As New ADODB.Recordset With Rs_Dm_Spdm If .State = adStateOpen Then .Close End If .ActiveConnection = adoCN .CursorLocation = adUseClient .CursorType = adOpenDynamic .LockType = adLockBatchOptimistic .Source = "SELECT * FROM dm_spdm " .Open End With Combo1.Clear If Rs_Dm_Spdm.RecordCount > 0 Then Rs_Dm_Spdm.MoveFirst Do While Not Rs_Dm_Spdm.EOF Combo1.AddItem Rs_Dm_Spdm.Fields("spmczw") & "" Rs_Dm_Spdm.MoveNext Loop End If
Dim Rs_Dm_Spdm As New ADODB.Recordset With Rs_Dm_Spdm If .State = adStateOpen Then .Close End If .ActiveConnection = adoCN .CursorLocation = adUseClient .CursorType = adOpenDynamic .LockType = adLockBatchOptimistic .Source = "SELECT * FROM dm_spdm " .Open End With Combo1.Clear If Rs_Dm_Spdm.RecordCount > 0 Then Rs_Dm_Spdm.MoveFirst Do While Not Rs_Dm_Spdm.EOF Combo1.AddItem Rs_Dm_Spdm.Fields("spmczw") & "" Rs_Dm_Spdm.MoveNext Loop End If
ByVal FieldName As String, TargetComboBox As ComboBox)
Dim rs As New ADODB.Recordset
Dim tmpstr As String
tmpstr = TargetComboBox.Text
rs.Open "SELECT " & FieldName & " FROM " & TableName & " GROUP BY " & FieldName, SYScnn
Do Until rs.EOF
If (Not IsNull(rs.Fields(FieldName))) And rs.Fields(FieldName) <> "" Then
If Not MemberComboBox(TargetComboBox, rs.Fields(FieldName)) Then
TargetComboBox.AddItem rs.Fields(FieldName)
End If
End If
rs.MoveNext
Loop
rs.Close
TargetComboBox.Text = tmpstr
End Sub
Private Function MemberComboBox(ByVal TargetComboBox As ComboBox, ByVal Targetstr As String) As Boolean Dim i As Integer
MemberComboBox = False
i = 0
Do Until i = TargetComboBox.ListCount
If TargetComboBox.List(i) = Targetstr Then
MemberComboBox = True
Exit Do
End If
i = i + 1
Loop
End Function
Datamember的内容是?
datamenber--表
datasource--字段
If .State = adStateOpen Then
.Close
End If
.ActiveConnection = adoCN
.CursorLocation = adUseClient
.CursorType = adOpenDynamic
.LockType = adLockBatchOptimistic
.Source = "SELECT * FROM dm_spdm "
.Open
End With
Combo1.Clear
If Rs_Dm_Spdm.RecordCount > 0 Then
Rs_Dm_Spdm.MoveFirst
Do While Not Rs_Dm_Spdm.EOF
Combo1.AddItem Rs_Dm_Spdm.Fields("spmczw") & ""
Rs_Dm_Spdm.MoveNext
Loop
End If
If .State = adStateOpen Then
.Close
End If
.ActiveConnection = adoCN
.CursorLocation = adUseClient
.CursorType = adOpenDynamic
.LockType = adLockBatchOptimistic
.Source = "SELECT * FROM dm_spdm "
.Open
End With
Combo1.Clear
If Rs_Dm_Spdm.RecordCount > 0 Then
Rs_Dm_Spdm.MoveFirst
Do While Not Rs_Dm_Spdm.EOF
Combo1.AddItem Rs_Dm_Spdm.Fields("spmczw") & ""
Rs_Dm_Spdm.MoveNext
Loop
End If