PostgreSql:

第一步 在网上下载postres的驱动程序,之后安装,下载地址:https://www.devart.com/odbc/postgresql/download.html

第二步 创建ODBC数据源

点击“开始-》控制面板-》管理工具-》数据源(ODBC)-》用户DSN-》添加”

安装上图配置好之后写入VBA代码并导入包文件

  1. Private Sub CommandButton1_Click()
  2.  
  3. Dim cnn As New ADODB.Connection
  4. Dim rs As New ADODB.Recordset
  5. Dim dataBase As String
  6. Dim userName As String
  7. Dim password As String
  8. Dim DBname As String
  9. Dim openCommand As String
  10. dataBase = "PostgreSQL35W"
  11. userName = "postgres"
  12. password = "test1234"
  13. DBname = "testRPA"
  14. openCommand = "DSN=" & dataBase & ";" & "UID=" & userName & ";" & "PWD=" & password & ";" & "Database=" & DBname
  15. cnn.Open openCommand
  16. Dim SQL As String
  17. '検索
  18. SQL = "select id from test where ID = '1001'"
  19. rs.Open SQL, cnn
  20. While Not rs.EOF
  21. MsgBox rs!ID
  22. rs.MoveNext
  23. Wend
  24. rs.Close
  25. cnn.Close
  26. Set rs = Nothing
  27. Set cn = Nothing
  28.  
  29. '削除
  30. SQL = "delete from test where ID = '1001'"
  31. Call cnn.Execute(SQL)
  32.  
  33. '修正
  34. SQL = "update test set NAME = 'name123' where ID = '1002'"
  35. Call cnn.Execute(SQL)
  36.  
  37. End Sub

Mysql:

下载数据源链接,之后创建mysql的数据源

下载地址
https://dev.mysql.com/downloads/connector/odbc/

创建数据源

VBA包文件也要引入

写入VBA代码

  1. 'MySql接続
  2. Private Sub mySql_Click()
  3. Dim conn As ADODB.Connection
  4. Dim rs As ADODB.Recordset
  5. Set conn = New ADODB.Connection
  6. Set rs = New ADODB.Recordset
  7.  
  8. conn.ConnectionString = "DSN=mysqlConntion;Server=localhost;DB=zhd_sale_demo;UID=root;PWD=123qwe!@#;OPTION=3;"
  9. conn.Open
  10.  
  11. rs.Open "select * from `ims_wcy_hand_over`", conn
  12. While Not rs.EOF
  13. MsgBox rs!WEID
  14. rs.MoveNext
  15. Wend
  16.  
  17. rs.Close: Set rs = Nothing
  18. conn.Close: Set conn = Nothing
  19. End Sub

执行即可!

vba Excel连接数据库的更多相关文章

  1. VBA Excel 常用 自定义函数

    1. 将 互换 Excel 列号(数字/字母) Public Function excelColumn_numLetter_interchange(numOrLetter) As String Dim ...

  2. VBA Excel WideCharToMultiByte Compile error on 64-bit System

    Compile Error: The code in this project must be updated for use on64-bit systems. Please review and ...

  3. 1.VBA Excel宏

    Excel VBA宏 在这一章中,让我们了解如何编写一个简单的宏.让我们一步一步来. 第1步:首先,让我们能够在Excel20XX'开发'菜单.做同样的,点击 File >> Option ...

  4. VBA Excel对象(十九)

    使用VBA进行编程时,用户将要处理的重要对象很少.下面是一些常见的对象 - 应用程序对象 工作簿对象 工作表对象 范围对象 应用程序对象 应用程序对象由以下部分组成 - 应用程序范围的设置和选项. 返 ...

  5. VBA Excel宏(二)

    在本章中,我们来学习如何逐步编写一个简单的宏. 第1步 - 首先,在Excel 2016中启用“开发者”菜单.要完成这个设置,请点击左上角菜单:文件 -> 选项.如下图所示 - 第2步 - 点击 ...

  6. [vba]excel中求选中数据和为给定数所有的组合

    昨天下午开始学习的vba,累死了,肯定有bug,待调试 vba程序如下: Dim aSum As Integer Dim tSum As Integer ) As Integer Dim arrMax ...

  7. python使用VBA:Excel创建图表(转)

    # -*- coding: utf-8 -*- """ Created on Thu Mar 06 11:22:03 2014 @author: Administrato ...

  8. VBA Excel 对比两列数据

    Sub Md() ' ' Macro1 Macro ' 宏由 BX 录制,时间: 2012-6-8 ' 宏中的列数可以输入 A - IV 也可以输入 1-256 ' Dim i%, j%, i1%, ...

  9. VBA excel中表示列的字母换成数字

    出自这里 数字转列标: Split(Cells(1,).Address(1,0),"$")(0)    '将1-256替换红色的1就可以 Cells(1, a) 选中对应的第一行第 ...

随机推荐

  1. Codeforces Round #528 (Div. 2)题解

    Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...

  2. TensorFlow(七):tensorboard网络执行

    # MNIST数据集 手写数字 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data # ...

  3. codeforces626F

    CF626F Group Projects  有n个学生,每个学生有一个能力值ai.现在要把这些学生分成一些(任意数量的)组,每一组的“不和谐度”是该组能力值最大的学生与能力值最小的学生的能力值的差. ...

  4. Ubuntu 14.04 indigo 安装 cartographer 1.0.0

    安装依赖(cmake 版本为2.8,我的是自带的)sudo apt-get updatesudo apt-get install -y g++ git google-mock libboost-all ...

  5. SSO单点登录实例

    单点登录流程图 系统登陆拦截器 // // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernf ...

  6. java中int 和String相互转换

    一.String转为int int i=Integer.parseInt(string):int i=Integer.valueOf(s).intValue(); 二.int转为String Stri ...

  7. Hibernate用到HQL查询时的错误

    Exception in thread "main" org.hibernate.hql.internal.ast.QuerySyntaxException: student is ...

  8. excel被保护或者锁定时候按住alt和enter可以输入换行

    excel被保护或者锁定时候按住alt和enter可以输入换行

  9. Leetcode题目617:合并二叉树(递归-简单)

    题目描述: 给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠. 你需要将他们合并为一个新的二叉树.合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新 ...

  10. centos7 windows7 双系统重新构建引导和启动顺序

    安装centos后无法引导启动windows7的解决方法 在电脑Windows7系统上安装Centos7,安装后找不到Windows7引导菜单. 原因:因为CentOS 7已采用新式的grub2系统, ...