Access开发培训
网站公告
·Access专家课堂QQ群号:151711184    ·Access快速开发平台下载地址及教程    ·欢迎加入Access专家课堂微信群!    ·如何快速搜索本站文章|示例|资料    
您的位置: 首页 > 技术文章 > Access数据库-模块/函数/VBA

EXCEL(VBA)~SQL 经典写法范本汇集(二)

时 间: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

 

相关索引:上一节 下一节



Access软件网官方交流QQ群 (群号:483923997)       Access源码网店

常见问答:

技术分类:

相关资源:

专栏作家

关于我们 | 服务条款 | 在线投稿 | 友情链接 | 网站统计 | 网站帮助