VBA 提高篇36 ADO接入外部数据库 Api打开系统百宝箱

在这里插入图片描述

Ado接入外部数据库

1.创建连接对象 Adodb.Connection
2.连接数据库(数据版本/数据路径)
3. 准备Sql语句 执行
4. 准备结果集对象Adodb.Recordset
5. 结果集-> 发送SQL语句 ->至目标数据库

rst.Open sql,con

  1. 执行操作

rst.moveNext 表格指针的移动
rst.EOF 是否移动到该表(数据表)末尾

  1. 关闭结果集和连接对象
Sub importData()
    Dim con As Object, rst As Object, sql As String, i As Integer
    
    '1.创建一个连接对象
    Set con = CreateObject("Adodb.Connection")
    
    '2.使用con将连接对象与目标数据库连接起来 -->连接
    con.Open "Provider = Miscrosoft.ACE.OLEDB.12.0;Data Source=d:\vbademo\demodb.accdb"
    
    '将准备执行的SQL命令保存在的一个字符传变量sql中
    sql = "select 课程号,课程名称,责任教师, from course"
    
    
    '创建一个结果集对象
    Set rst = CreateObject("adodb.recordset")
    
    '3.使用该结果及对象,沿con线路将sql语句发送给目标数据库,并接收数据库返回的查询结果
    rst.Open sql, con
    
    ' 4. 接受结果,并处理结果集
    '4.1将结果集中全部内容直接复制到B2开始的单元格中
    'Range("b2").CopyFromRecordset rst
    
    
    '4.2 扫描结果集中每一行数据,将其字段内容稍加修改后填入第i行单元格中
    i = 2
    Do While Not rst.EOF
        Cells(i, 2) = rst("课程号")
        Cells(i, 3) = "《" & rst("课程号") & "》"
        Cells(i, 4) = "Mr." & rst("责任教师")
        rst.MoveNext
        i = i + 1
    Loop
        
    '5.非常重要,程序结束前必须关闭结果集和连接对象sql指令
    rst.Close
    con.Close
    
    
End Sub

Windows Api函数 (Application Programming Interface)

在Excel里边通过API函数调用Windows的其他应用
在这里插入图片描述

(强大的数据库函数,几乎无所不能)

  1. WinExec(命令可执行文件名,显示方式)

  2. 在这里插入图片描述
Option Explicit
Declare Function WinExec Lib "kernel32" _
                    (ByVal lpCmdLine As String, ByVal ncmdshow As Long) As Long                 
Sub runDemo()
    WinExec "C:\windows\system32\notepad.exe", 9
End Sub
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Option Explicit '------------------------------------------------------------------------------- ' ' This VB module is a collection of routines to perform serial port I/O without ' using the Microsoft Comm Control component. This module uses the Windows API ' to perform the overlapped I/O operations necessary for serial communications. ' ' The routine can handle up to 4 serial ports which are identified with a ' Port ID. ' ' All routines (with the exception of CommRead and CommWrite) return an error ' code or 0 if no error occurs. The routine CommGetError can be used to get ' the complete error message. '------------------------------------------------------------------------------- '------------------------------------------------------------------------------- ' Public Constants '------------------------------------------------------------------------------- ' Output Control Lines (CommSetLine) Const LINE_BREAK = 1 Const LINE_DTR = 2 Const LINE_RTS = 3 ' Input Control Lines (CommGetLine) Const LINE_CTS = &H10& Const LINE_DSR = &H20& Const LINE_RING = &H40& Const LINE_RLSD = &H80& Const LINE_CD = &H80& '------------------------------------------------------------------------------- ' System Constants '------------------------------------------------------------------------------- Private Const ERROR_IO_INCOMPLETE = 996& Private Const ERROR_IO_PENDING = 997 Private Const GENERIC_READ = &H80000000 Private Const GENERIC_WRITE = &H40000000 Private Const FILE_ATTRIBUTE_NORMAL = &H80 Private Const FILE_FLAG_OVERLAPPED = &H40000000 Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000 Private Const OPEN_EXISTING = 3 ' COMM Functions Private Const MS_CTS_ON = &H10& Private Const MS_DSR_ON = &H20& Private Const MS_RING_ON = &H40& Private Const MS_RLSD_ON = &H80& Private Const PURGE_RXABORT = &H2 Private Const PURGE_RXCLEAR = &H8 Private Const PURGE_TXABORT = &H1 Private Const PURGE_TXCLEAR = &H4 ' COMM Escape Functions Private Const CLRBREAK = 9 Private Const CLRDTR

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pigerr杨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值