m5k1umn 发表于 2024-8-4 15:12:29

VBA数据库之工作表内连接查询


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">【分享成果,随喜正能量】人若能心静如水,则万物不侵。静以修心,静以修身,以静视物,以静观心,我静故我在,静了,你的生活才会听你的安排。。 ​​</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">《VBA数据库<span style="color: black;">处理</span><span style="color: black;">方法</span>》教程(10090845)是我推出的第二套教程,<span style="color: black;">日前</span><span style="color: black;">已然</span>是<span style="color: black;">第1</span>版修订了。这套教程定<span style="color: black;">位置于</span>中级,是学完字典后的另一个专题讲解。数据库是数据处理的利器,教程中<span style="color: black;">仔细</span>介绍了利用ADO连接ACCDB和EXCEL的<span style="color: black;">办法</span>和实例操作,教程<span style="color: black;">第1</span>版的修订内容<span style="color: black;">重点</span>是完成所有程序文件的32位和64位OFFICE系统测试。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这套教程共两册,八十四讲,今后一段时间会给<span style="color: black;">大众</span><span style="color: black;">持续</span>推出修订后的教程内容。今日的内容是:VBA数据库之工作表内连接<span style="color: black;">查找</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p26-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/3277af34e3cf42d1908fea741e66a2f1~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723346812&amp;x-signature=N%2FGK3IombSWrdDRUNWol84c%2B%2B4E%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">第五十六讲 工作表<span style="color: black;">查找</span>中,内连接Inner join的应用</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">大众</span>好,<span style="color: black;">咱们</span>继续讲解VBA数据库<span style="color: black;">处理</span><span style="color: black;">方法</span>,今日讲解第56讲内容: 数据表<span style="color: black;">查找</span>中,内连接Inner join的讲解。从这讲<span style="color: black;">起始</span>给<span style="color: black;">大众</span>实例讲解在<span style="color: black;">查找</span>中<span style="color: black;">各样</span>连接方式的应用。<span style="color: black;">为何</span>到<span style="color: black;">此刻</span>才讲解这块内容呢?是的,<span style="color: black;">非常多</span>的内容我在之前的<span style="color: black;">文案</span>中<span style="color: black;">已然</span><span style="color: black;">起始</span>利用了,相信<span style="color: black;">大众</span><span style="color: black;">已然</span><span style="color: black;">起始</span>有所了解或许只是概念的欠缺,<span style="color: black;">同期</span>,随着讲解的深入<span style="color: black;">大众</span><span style="color: black;">发掘</span>SQL语句是操作数据库的一个非常重要的工具,我的观点<span style="color: black;">始终</span>是:搭积木思想。不要<span style="color: black;">太多</span>的纠缠于其中的算法,只要会利用就<span style="color: black;">能够</span>。至于<span style="color: black;">为何</span>这么写,那是专业人员的事情。</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">1 什么是内连接,内连接的应用场景</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">好了,先给<span style="color: black;">大众</span>简单的解释一下什么是内连接,内连接的<span style="color: black;">查找</span>操作及其特点。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">内连接<span style="color: black;">亦</span>叫连接,是最早的一种连接。还<span style="color: black;">能够</span>被<span style="color: black;">叫作</span>为普通连接<span style="color: black;">或</span>自然连接,内连接是从结果表中删除与其他被连接表中<span style="color: black;">无</span>匹配行的所有行。返回与连接<span style="color: black;">要求</span>匹配的数据行,它<span style="color: black;">运用</span>比较运算符比较被连接列的列值。在<span style="color: black;">每一个</span>表中找出符合<span style="color: black;">要求</span>的共有记录。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">实例情景,下面的两个工作表,一个是“数据”,一个是“数据1”。我要把其中有型号<span style="color: black;">同样</span>的数据提取出来,<span style="color: black;">然则</span>型号,生产厂,数量来自“数据”工作表,而供应商的数据来自“数据2”的数据。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/eb8fc70ae4274c8ca0486dbd50f87f2e~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723346812&amp;x-signature=5HVImRA64rKJEhUtbpq4MIvUdB0%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">2 内连接的应用代码及代码<span style="color: black;">诠释</span></h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">下面看我给出的内连接的代码,其实代码并不难:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Sub mynzRecords_56() 第56讲</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Dim cnADO, rsADO As Object</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Dim strPath, strSQL As String</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Worksheets("56").Select</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Cells.ClearContents</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Set cnADO = CreateObject("ADODB.Connection")</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Set rsADO = CreateObject("ADODB.Recordset")</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">strPath = ThisWorkbook.FullName</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">cnADO.Open "provider=Microsoft.ACE.OLEDB.12.0;extended properties=excel 12.0;hdr=yes;imex=1;data source=" &amp; strPath</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">strSQL = "Select a.型号,a.生产厂,a.数量,b.供应商 From [数据$]as a,[数据2$] as b Where a.型号=b.型号"</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">strSQL = "Select a.型号,a.生产厂,a.数量,b.供应商 From [数据$] as a INNER JOIN [数据2$] as b ON a.型号=b.型号"</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">rsADO.Open strSQL, cnADO, 1, 3</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">For i = 1 To rsADO.Fields.Count</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Cells(1, i) = rsADO.Fields(i - 1).Name</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Next</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Range("a2").CopyFromRecordset rsADO</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">rsADO.Close</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">cnADO.Close</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Set rsADO = Nothing</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Set cnADO = Nothing</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">End Sub</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">代码截图:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/9077c0d31ed640b3bba734f0c21a827c~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723346812&amp;x-signature=Vo%2BiZ47u7M1ok8QF%2B8AEYo5hDzI%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">代码讲解:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1) cnADO.Open "provider=Microsoft.ACE.OLEDB.12.0;extended properties=excel 12.0;hdr=yes;imex=1;data source=" &amp; strPath</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">此语句的代码<span style="color: black;">创立</span>起ADO和Excel的连接,<span style="color: black;">大众</span><span style="color: black;">能够</span>直接利用,只要是连接07版本以上的EXCEL<span style="color: black;">就可</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2) strSQL = "Select a.型号,a.生产厂,a.数量,b.供应商 From [数据$]as a,[数据2$] as b Where a.型号=b.型号"</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">此语句的代码为一个典型的SQL内连接的语句,用的是WHERE的表达式,<span style="color: black;">大众</span>要<span style="color: black;">重视</span>我字段的写法和定义a,b 两个数据表的写法。不<span style="color: black;">能够</span>有丝毫的错误。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3) strSQL = "Select a.型号,a.生产厂,a.数量,b.供应商 From [数据$] as a INNER JOIN [数据2$] as b ON a.型号=b.型号"</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">此语句的代码为一个典型的SQL内连接的语句的另一种写法,用的是的INNER JOIN….on…的表达式,<span style="color: black;">大众</span>要<span style="color: black;">重视</span>这种写法和定义a,b 两个数据表的写法。大体上和上面的写法是一致的,读者<span style="color: black;">能够</span>根据自己的习惯利用。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">下面看代码的运行:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/64d45c64a0ce4d2eaf4d788b2ce132df~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723346812&amp;x-signature=bwVOCX3%2ByZd7rMUnD6JC4dp4My0%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">大众</span>要<span style="color: black;">重视</span>,<span style="color: black;">以上</span>型号为QQ01的供应商为RRR05是取自数据2的数据。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">今日内容回向:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1 什么是内连接?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2 内连接的两种表法方式是什么?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3 <span style="color: black;">倘若</span>将select后的字段去掉换成*号会是什么样的结果?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">本讲内容参考程序文件:VBA与数据库操作(第二册).xlsm</span></strong></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/efef7cde997547468788de16e0a7d37c~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723346812&amp;x-signature=kLV6aG%2Br9ddi4lC71ovHYKc0dXM%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">我20<span style="color: black;">数年</span>的VBA实践经验,<span style="color: black;">所有</span>浓缩在下面的各个教程中:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/2ce7c9af559f4bf48dcf328607fdb2fe~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723346812&amp;x-signature=4fipr7e7E%2FMBuvhHIvA2uZSyg00%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/42094149ef1845238dc2c104ad0f7c7f~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723346812&amp;x-signature=uqqXR682rd52fgKLCxoXctCMluY%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/6f2a48c23f9d45228a0c07e5456c849a~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723346812&amp;x-signature=CPCzkfBsqDJXAkRRcR%2Bmz0%2Fzg%2FM%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">【分享成果,随喜正能量】人生无处不修行,能在世事中心静如水,<span style="color: black;">才可</span>在纷扰中安然无恙,非<span style="color: black;">清闲</span>非<span style="color: black;">导致</span>远,想要成就一番大事,<span style="color: black;">必要</span>要有静气。。</span></strong></p>




364463952 发表于 2024-8-26 15:46:31

“沙发”(SF,第一个回帖的人)‌

4zhvml8 发表于 2024-10-4 07:32:01

祝福你、祝你幸福、早日实现等。

4lqedz 发表于 2024-10-6 02:56:46

这夸赞甜到心里,让我感觉温暖无比。

b1gc8v 发表于 2024-10-23 03:57:13

你的见解真是独到,让我受益匪浅。

4lqedz 发表于 昨天 20:21

交流如星光璀璨,点亮思想夜空。
页: [1]
查看完整版本: VBA数据库之工作表内连接查询