mysql - Excel for Mac 2016 中是否有 ADODB.connection ADODB.recordset 的等效项?

标签 mysql excel vba macos

... 或从 QueryTables 中提取记录。

最佳答案

试试这个排序:

  1. SQLDatabase_VBA.bas 将来自 ADODB 或系统的 SQL 数据库与 Excel 中的 VBA 连接

sconnect = "Provider=MSDASQL.1;DSN=your ODBC connection name; " & _ "UID=your user;PWD=your password;DBQ=your database" & DBPath & ";HDR=Yes';"

用于 IBM/AS400 的连接字符串:(没有 ADODB,可能在 mac 上工作,但只能在任一 IBM 服务器上工作)

sconnect = "PROVIDER=IBMDA400;Data Source=servername; " & _ "DEFAULT COLLECTION=optional;USER ID=Username ;PASSWORD=KENNWORT"

SQLDatabase_VBA.bas :

Sub SQLDatabase_VBA()



On Error Resume Next



'Step 1: Create the Connection String with Provider and Data Source options

Public sSQLQry As String

Public ReturnArray



Public Conn As New ADODB.Connection

Public mrs As New ADODB.Recordset

Public DBPath As String, sconnect As String





'Step 2: Create the Connection String with Provider and Data Source options

ActiveSheet.Activate



DBPath = ThisWorkbook.FullName 'Refering the sameworkbook as Data Source



'You can provide the full path of your external file as shown below

'DBPath ="C:\InputData.xlsx"



sconnect = "Provider=MSDASQL.1;DSN=Connect_fromODBC;UID=your user name;PWD=your password;DBQ=database name" & DBPath & ";HDR=Yes';"

'If any issue with MSDASQL Provider, Try the Microsoft.Jet.OLEDB:

'sconnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBPath _

    & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"



'Step 3: set connection timeout Open the Connection to data source

Conn.ConnectionTimeout = 30

Conn.Open sconnect



'Step 4: Create SQL Command String MRFIRM, MRIDEN, MRSART,MRSRN,MRSRRF,MRDTB,MRUSER, MRSRNA as Serien_NR_Zugriff

 sSQLSting = "SELECT * From your database " & _

            " WHERE ------ " & _

            " Group by ----- "







'Step 5: Get the records by Opening this Query with in the Connected data source

 mrs.Open sSQLSting, Conn



 'Step 6: Copy the reords into our worksheet

 'Import Headers

    For i = 0 To mrs.Fields.Count - 1

        ActiveSheet.Range("B15").Offset(0, i) = mrs.Fields(i).Name

    Next i



'Import data to destination cell

ActiveSheet.Range("B15").Offset(1, 0).CopyFromRecordset mrs



 'Step 7: Close the Record Set and Connection

  'Close Recordset

  mrs.Close



  'Close Connection

  Conn.Close

  Set mrs = Nothing

  Set Conn = Nothing



End Sub

关于mysql - Excel for Mac 2016 中是否有 ADODB.connection ADODB.recordset 的等效项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53014926/

相关文章:

excel - 将逗号分隔的值拆分为数组并查找与预设条件的匹配项

vba - 使用 Dim 时外部程序无效

vba - 对于范围内的每个单元格,如果单元格为负数,则更改颜色

internet-explorer - VBA IE自动化-阅读iFrame

mysql - GROUP BY 不删除重复项

MySQL - 如何只选择具有相同字段值的前 X 行?

MYSQL JOIN WHERE ISSUES - 需要某种 if 条件

excel - 在 IF 函数上同时使用 AND 和 OR

VBA 错误 424,使用 ".Value"属性将值写入单元格

php - 将连接查询更改为 laravel