|
原帖由 望江婷 于 2011-3-8 17:18 发表
谢谢赵老师,我的天呢,我的附件数据库数据写错了,让赵老师受累了,我看看试一下你的代码,下面的才是我准备上船的数据库。
884743
没有发现有什么区别,下面按照月份汇总:
Private Sub CommandButton1_Click()
Dim s$, t$, v$, i%, SQL$
For i = 1 To 4
If Me.Controls("ComboBox" & i).Value <> "" Then
' If i <> 2 Then t = t & "," & Me.Controls("Label" & i).Caption Else t = t & ",year(" & Me.Controls("Label" & i).Caption & ")&month(" & Me.Controls("Label" & i).Caption & ")"
' If i <> 2 Then v = v & "," & Me.Controls("Label" & i).Caption Else v = v & ",year(" & Me.Controls("Label" & i).Caption & ")&month(" & Me.Controls("Label" & i).Caption & ")"
If i <> 2 Then t = t & "," & Me.Controls("Label" & i).Caption Else t = t & ",month(" & Me.Controls("Label" & i).Caption & ")"'纯按月份,上面是年连接月
If i <> 2 Then v = v & "," & Me.Controls("Label" & i).Caption Else v = v & ",month(" & Me.Controls("Label" & i).Caption & ")"
Else
t = t & ",null"
End If
Next
t = t & ",费用类别,费用明细,sum(金额)"
v = v & ",费用类别,费用明细"
If ComboBox1.Value <> "" Then
s = s & " and " & Label1.Caption & "='" & ComboBox1.Value & "'"
If ComboBox2.Value <> "" Then s = s & " and month(" & Label2.Caption & ")=val(" & Split(ComboBox2.Value, "-")(1) & ")"
Else
If ComboBox2.Value <> "" Then s = s & " and " & Label1.Caption & "='" & Split(ComboBox2.Value, "-")(0) & "年' and month(" & Label2.Caption & ")=val(" & Split(ComboBox2.Value, "-")(1) & ")"
End If
For i = 3 To 4
If Me.Controls("ComboBox" & i).Value <> "" Then s = s & " and " & Me.Controls("Label" & i).Caption & "='" & Me.Controls("ComboBox" & i).Value & "'"
Next
If s = "" Then Exit Sub
SQL = "select " & Mid(t, 2) & " from 费用汇总单 where " & Mid(s, 6) & " group by " & Mid(v, 2)
Set rs = New ADODB.Recordset
rs.Open SQL, cnn, adOpenKeyset, adLockOptimistic
With Sheets("查询")
.[A4:H65536].ClearContents
.Range("b4").CopyFromRecordset rs
End With
End Sub
[ 本帖最后由 zhaogang1960 于 2011-3-8 17:42 编辑 ] |
|