时 间:2012-04-07 09:09:03
作 者:欢乐小爪 ID:20149 城市:杭州
摘 要:EXCEL(VBA)~SQL 经典写法范本汇集(二)
正 文:
一)
选择供应商和选择月份记录的查询
原创:小爪 日期:2008-5-10
Private Sub CommandButton1_Click()
Range('a5:k1000').ClearContents
Set conn = CreateObject('ADODB.Connection')
conn.Open 'provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;imex=1';data source=' & ThisWorkbook.FullName
If Range('b3') = '全部' And Range('d3') = '全部' Then
Sql = 'select * from [数据源$a3:i1000] '
GoTo 100
End If
If Range('b3') = '全部' Then
'
Sql = 'select * from [数据源$a3:i1000] where month(日期) = '' & [d3] & '''
GoTo 100
End If
If Range('d3') = '全部' Then
Sql = 'select * from [数据源$a3:i1000] where 供应商= '' & [b3] & '''
GoTo 100
End If
If Range('d3') <> '全部' And Range('d3') <> '全部' Then
i = Range('d3')
Sql = 'select * from [数据源$a3:i1000] where (供应商= '' & [b3] & '') and (month(日期) = '' & i & '')'
GoTo 100
End If
100:
Sheets('统计').Range('a5').CopyFromRecordset conn.Execute(Sql)
conn.Close: Set conn = Nothing
End Sub
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
二)查询某地的 收款记录工作表 的收款日期,凭证号,金额,摘要
和 送货记录工作表 的发货日期,单号,金额,折扣,赠送,退货,备注
原创:小爪 日期:2008-5-6
实例地址:E:\欢乐--office\小爪-excel\小爪-vba\ADO+SQL
Private Sub CommandButton1_Click()
Range('a6:k16').ClearContents
Set conn = CreateObject('ADODB.Connection')
conn.Open 'provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;imex=1';data source=' & ThisWorkbook.FullName
Sql1 = 'select 收款日期,凭证号,金额,摘要 from [收款记录$B2:F20] where 客户名称 = '' & [b2] & '''
Sql2 = 'select 发货日期,单号,金额,折扣,赠送,退货,备注 from [送货记录$B2:i20] where 客户名称 = '' & [b2] & '''
Sheets('套打').Range('a6').CopyFromRecordset conn.Execute(Sql1)
Sheets('套打').Range('e6').CopyFromRecordset conn.Execute(Sql2)
conn.Close: Set conn = Nothing
End Sub
联系客服