|
修改一下:
Dim cnn As ADODB.Connection
Private Sub CommandButton1_Click()
Dim i%, arr, Sql As String
arr = [a2:h2&""]
For i = 1 To 4
If Len(Controls("ComboBox" & i).Value) Then Sql = Sql & " and " & Controls("Label" & i).Caption & "='" & Controls("ComboBox" & i).Value & "'"
Next
[a3:h65536] = ""
If Sql = "" Then Exit Sub
Sql = "select " & Join(arr, ",") & " from 明细 where " & Mid(Sql, 6)
[a3].CopyFromRecordset cnn.Execute(Sql)
End Sub
Private Sub UserForm_Initialize()
Dim i%, arr
Set cnn = New ADODB.Connection
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ThisWorkbook.Path & "\数据库.mdb"
For i = 1 To 4
arr = cnn.Execute("select distinct " & Controls("Label" & i).Caption & " from 明细").GetRows
Controls("ComboBox" & i).List = WorksheetFunction.Transpose(arr)
Next
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
cnn.Close
Set cnn = Nothing
End Sub |
|