没有合适的资源?快使用搜索试试~ 我知道了~
VBA中 ADO链接以及数据库SQL链接方法介绍,以及实例讲解
资源推荐
资源详情
资源评论




格式:x-rar 资源大小:1.1MB




格式:doc 资源大小:38.0KB 页数:3














格式:doc 资源大小:49.5KB 页数:5


格式:x-rar 资源大小:249.6KB




格式:txt 资源大小:2.1KB 页数:2



1, 包含空值的记录 f13 is null
‘http://www.excelpx.com/dispbbs.asp?boardID=5&ID=46032&page=1
‘订单生成系统.xls
‘f6-第 6 列,f2-第 2 列
Private Sub Worksheet_Activate()
On Error Resume Next
Dim x As Object, yy As Object, sql As String
Set x = CreateObject("ADODB.Connection")
x.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;hdr=no;';Data
Source=" & ActiveWorkbook.FullName
sql = "select f6,f2,f3,f4,f5,f7,f13,f24 -f25 from [sheet1$] where f24 -f25<f17 and (f13<>'C3' or f13
is null)" ‘不等于字符串用 ‘C3’ 包含空值用 is null
Set yy = x.Execute(sql)
Range("a:h").ClearContents
Range("a1:h1") = Array("编号", "品名", "规格", "产地", "单位", "件装", "属性", "计划") ‘表头
另外赋值
[a2].CopyFromRecordset yy
Set yy = Nothing
Set x = Nothing
End Sub
2,用 ADO Connection 对象查询
Option Explicit
Public conn As ADODB.Connection
Sub Myquery()
Dim sConnect$, sql1$
Set conn = CreateObject("adodb.connection")
Sheets("sheet1").Cells.ClearContents
sConnect = "provider=microsoft.jet.oledb.4.0;extended properties=excel 8.0;" & _
"Data Source=" & ThisWorkbook.Path & "\" & ThisWorkbook.Name
sql1 = "select 物料代码,物料描述,属性,单位 from [物料代码表$] where 属性= '采购' " '表格
名要用[$],条件部分用单引号''
ThisWorkbook.Sheets("sheet1").Cells(2, 1).CopyFromRecordset conn.Execute(sql1) 'copy 后
面紧接 SQL 查询执行语句
With Sheets("sheet1")
.Range("A1") = "物料代码" '建立表头
.Range("B1") = "物料描述"

.Range("C1") = "属性"
.Range("D1") = "单位"
End With
'conn.Close '可不用每次关闭数据源的连接
End Sub
3,用记录集执行单个查询
Option Explicit
Sub Myquery()
Dim rd As ADODB.Recordset
Dim i%, j%, k%, sConnect$, sql1$, str$
Set rd = New ADODB.Recordset
str = "外协"
Sheets("sheet1").Cells.ClearContents
sConnect = "provider=microsoft.jet.oledb.4.0;extended properties=excel 8.0;" & _
"Data Source=" & ThisWorkbook.Path & "\" & ThisWorkbook.Name
'conn.Open sConnect '打开数据源
sql1 = "select 物料代码,物料描述,属性,单位 from [物料代码表$] where 属性= '采购' " '表
格名要用[$],条件部分用单引号''
rd.Open sql1, sConnect, adOpenForwardOnly, adLockReadOnly
ThisWorkbook.Sheets("sheet1").Cells(2, 1).CopyFromRecordset rd
With Sheets("sheet1")
.Range("A1") = "物料代码" '建立表头
.Range("B1") = "物料描述"
.Range("C1") = "属性"
.Range("D1") = "单位"
End With
rd.Close '关闭记录集
Set rd=Nothing '关闭
End Sub
4,引用一列,如 A 列
‘引用单列、单行、单个单元格.xls
'引用一列,如 A 列
Sub onecolumn()
Dim Sql$
Set Conn = CreateObject("Adodb.Connection")
Conn.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;hdr=no';data

source=" & ThisWorkbook.Path & "\1.xls"
Sql = "select f1 from [sheet1$]"
Cells.Clear
[a1].CopyFromRecordset Conn.Execute(Sql)
Conn.Close
Set Conn = Nothing
End Sub
Sub dgzbhz()
'2008/12/2
‘http://www.exceljy.com/viewthread.php?tid=4912&pid=82252&page=1&extra=page
%3D1#pid82252
‘Book12021.xls
‘由于分表的第 2 列表头是“金额”,不用它,改为“一中”,所以要用 hdr=no 无标题,拷贝时
把第一行表头归零,所以最后要加表头。
Dim Sql$
Set Conn = CreateObject("Adodb.Connection")
[b2:d4] = ""
arr = Array("一中", "二中", "三中")
For i = 0 To UBound(arr)
Conn.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;hdr=no';data
source=" & ThisWorkbook.Path & "\" & arr(i) & ".xls"
Sql = "select f2 from [sheet1$]"
Cells(1, i + 2).CopyFromRecordset Conn.Execute(Sql)
Conn.Close
Next i
Set Conn = Nothing
[b1:d1] = arr
End Sub
‘test1203.xls EH
‘有标题不用 hdr=no,列名用编码文字,可往下连续取数据。
Private Function cnn() As Object
Set cnn = CreateObject("ADODB.Connection")
cnn.Open "Provider=Microsoft.Jet.Oledb.4.0;Extended Properties ='Excel 8.0;HDR=no';Data
Source= " & ThisWorkbook.FullName
End Function
Sub onecolumn()
Dim Sql$, Sht1 As Worksheet, Sht As Worksheet
Dim n
Set Sht1 = Sheets("汇总")
Sht1.Activate
‘Set Conn = CreateObject("Adodb.Connection")

‘Conn.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0';data source=" &
ThisWorkbook.FullName
For Each Sht In Sheets
If Sht.Name <> "汇总" Then
Sql = "select 编码 from [" & Sht.Name & "$]"
n = [b65536].End(xlUp).Row + 1
Sht1.Cells(n, 2).CopyFromRecordset Cnn.Execute(Sql)
End If
Next Sht
Cnn.Close
Set Cnn = Nothing
End Sub
5,引用一行,如第 1 行
'引用一
Sub onerow()
Dim Sql$
Set Conn = CreateObject("Adodb.Connection")
Conn.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;hdr=no';data
source=" & ThisWorkbook.Path & "\1.xls"
Sql = "select * from [sheet1$a1:iv1]"
Cells.Clear
[a1].CopyFromRecordset Conn.Execute(Sql)
Conn.Close
Set Conn = Nothing
End Sub
6,引用一个单元格,如 k1 单元格
‘2013-3-14
‘http://club.excelhome.net/thread-992260-1-1.html
Dim Sql$, Conn
Sub testit()
Dim myPath$, mvvar, i&, myName$, Myr&
Sheet1.Activate
[a4:h500].ClearContents
Set Conn = CreateObject("Adodb.Connection")

myPath = ThisWorkbook.Path & "\"
myName = ThisWorkbook.Name
mvvar = FileList(myPath)
If TypeName(mvvar) <> "Boolean" Then
For i = LBound(mvvar) To UBound(mvvar)
If mvvar(i) <> myName Then
Conn.Open "provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel
12.0;hdr=no';data source=" & ThisWorkbook.Path & "\" & mvvar(i)
Sql = "select * from [sheet1$h6:h6]"
Myr = [a65536].End(xlUp).Row + 1
If Myr < 4 Then Myr = 4
Cells(Myr, 3).CopyFromRecordset Conn.Execute(Sql)
Cells(Myr, 1) = Myr - 3
Cells(Myr, 2) = Left(mvvar(i), Len(mvvar(i)) - 4)
Sql = "select * from [sheet1$c14:c14]"
Cells(Myr, 4).CopyFromRecordset Conn.Execute(Sql)
Sql = "select * from [sheet1$c15:c15]"
Cells(Myr, 5).CopyFromRecordset Conn.Execute(Sql)
Sql = "select * from [sheet1$c16:c16]"
Cells(Myr, 6).CopyFromRecordset Conn.Execute(Sql)
Conn.Close
End If
Next
Else
MsgBox "没有找到文件。"
End If
Myr = Myr + 1
Cells(Myr, 2) = "合计"
Cells(Myr, 3).Formula = "=sum(r4c:r[-1]c)"
Cells(Myr, 3).AutoFill Cells(Myr, 3).Resize(1, 5)
End Sub
Function FileList(fldr, Optional fltr As String = "*.xls") As Variant
Dim sTemp As String, sHldr As String
If Right$(fldr, 1) <> "\" Then fldr = fldr & "\"
sTemp = Dir(fldr & fltr)
If sTemp = "" Then
FileList = False
Exit Function
End If
Do
sHldr = Dir
If sHldr = "" Then Exit Do
sTemp = sTemp & "|" & sHldr
剩余62页未读,继续阅读
资源评论


疟疾
- 粉丝: 1
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 某物业公司物业项目管理办法.doc
- 网络连接设备实用技术参数要求doc.doc
- 试论计算机软件开发中的分层技术运用.docx
- 信息工程学院毕业设计(论文)指导书通信工程专业.doc
- 单片机C语言编程常用运算符简介.doc
- 基于PLC的空气压缩机的监控系统方案设计书.doc
- 电子商务与信息服务业.docx
- 软件从敏捷到超精益开发的10步.docx
- 互联网+背景下少先队工作面临的挑战与应对策略.docx
- 让PID控制器设计更简单MATLABSimulink应用案例.doc
- JavaWeb人事管理开题研究.doc
- 移动互联网与户外运动产业融合发展研究.docx
- 数据结构111111111111
- 浅析职业学校计算机教学的特点.docx
- 项目教学法在医学高专院校《计算机应用基础》教学中的应用.docx
- 基于PLC的燃油锅炉控制系统方案设计书外文翻译.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
