【问题标题】:Are there equivalents to ADODB.connection ADODB.recordset in Excel for Mac 2016?Excel for Mac 2016 中是否有 ADODB.connection ADODB.recordset 的等价物?
【发布时间】:2018-10-27 06:01:20
【问题描述】:

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

【问题讨论】:

  • ADODB 不适用于 Mac。大多数人使用的 QueryTables 不太好,但他们似乎可以完成这项工作。
  • 理论上你可以编写一个 .NET Core 库来包装 ADO.NET,然后让该库公开一个 COM-visible API,然后可以想象能够从 VBA 引用它,假设 Mac-VBA 允许你引用此类类型库,并假设上述所有内容都有效……但这基本上是未知领域。不过值得一试。免责声明:我没有 Mac,并在 Windows 上构建 .NET……但 .NET Core 确实可以在 Mac 上运行,所以……

标签: mysql excel vba macos


【解决方案1】:

试试这个:

  1. SQLDatabase_VBA.bas 使用 Excel 中的 VBA 从 ADODB 或系统连接 SQL 数据库

sconnect = "Provider=MSDASQL.1;DSN=您的 ODBC 连接名称;" & _ "UID=你的用户;PWD=你的密码;DBQ=你的数据库" & 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

【讨论】:

    猜你喜欢
    • 2014-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 1970-01-01
    • 2011-07-29
    • 2013-06-21
    • 2014-01-09
    相关资源
    最近更新 更多