首页 > 解决方案 > 无法在 Excel for MAC 上为 MsSQL 服务器连接重新创建 ODBC

问题描述

我正在编写一个与 mac 兼容的 excel 文件,但我没有 mac,所以我连接到一个 mac 朋友来录制一个宏并进行模拟:我得到了这段代码,创建了一个对我的服务器的简单调用:

Sub Macro1()
'
' Macro1 Macro
'

'
    Application.CutCopyMode = False
    ActiveWorkbook.Worksheets.Add
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
        "ODBC;DRIVER=SQL Server;SERVER=myserver.database.windows.net;Database=mydb;UID=myuser;" _
        , Destination:=Range("$A$1")).QueryTable
        .CommandText = Array("select * from mytable")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .RefreshPeriod = False
        .PreserveColumnInfo = True
        .Refresh BackgroundQuery:=False
    End With
    Range("C2").Select
End Sub

把它放到windows上,测试,它,一切正常,

所以我写了以下代码,基本上 100% 相同,带有一个新的查询:

Sub demo__myCall()
  
  Dim sqlstring As String
  Dim sLogin As String
  Dim connstring As String
  Dim destinationCells As Range
    
  Set destinationCells = Sheets("mySheet").Range("A10")

    
  sLogin = "UID=myuser;Pwd=mypawd&123;"
               
  connstring = "ODBC;DRIVER=SQL Server;SERVER=myserver.database.windows.net;Database=mydb;" & sLogin
  
  Dim lo As ListObject
    For Each lo In Sheets("mySheet").ListObjects
        lo.Delete
    Next lo

  
    
    sqlstring = "select * from myothertable"
                
     
     With Sheets("INTERNAL").ListObjects.Add(SourceType:=0, Source:=connstring, Destination:=destinationCells).QueryTable
        .CommandText = Array(sqlstring)
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = False
        .RefreshStyle = xlInsertDeleteCells 'xlInsertDeleteCells
        .SavePassword = True
        .SaveData = True
        .RefreshPeriod = False
        .PreserveColumnInfo = True
        .Refresh BackgroundQuery:=False
     End With
   
 
End Sub

如你所见,difernec 是最小的,

回到mac,测试原来的(Macro1),工作正常,测试第二个(demo__myCall)得到错误“无法完成操作,因为没有安装ODBC控制器”(或类似的东西,因为原始消息是西班牙语)

回到 MAcro 1,工作,回到 demo_myCall,不工作......

有谁能够帮我?

标签: excelvbamacosodbc

解决方案


推荐阅读