64位Excel中如何通过VBA连接IBM DB2数据源?
64位Office 365下VBA连接DB2、MySQL数据源故障解决方案
故障背景
企业正在推进32位MS Office 365向64位版本的迁移,此前开发的一款可对接IBM DB2、MySQL双数据源的Excel工具,过往可兼容全系列32位MS Office(XP/2007/2013/365Pro)及不同位数Windows系统(XP/7/10),运行稳定。
迁移后工具出现数据库连接故障,其余适配项如PtrSafe声明均已完成修改。已完成的前置验证:
- 已安装64位IBM DB2 ODBC客户端
- 在
SysWoW64\ODBCAD中完成配置,连接测试成功,确认驱动本身无异常 - 同类连接逻辑的MySQL数据源也出现相同连接失败问题
故障表现为VBA调用连接打开时持续报错,报错截图如下:
初步判断为64位Excel未识别对应数据库驱动。
相关VBA代码
Option Explicit Public oDB2Connection As ADODB.Connection Public zQuery As String Public zDB2User As String Public zDB2Pwd As String Public oRecordSet As ADODB.Recordset Public iLoginTrials As Integer Public zTable As String Sub ConnectDB2() Dim PS As stPositions Dim bFirstLogin As Boolean Dim zStr As String '//create connection object Set oDB2Connection = New ADODB.Connection Set oRecordSet = New ADODB.Recordset On Error GoTo Errorhandler iLoginTrials = 0 bFirstLogin = False With oDB2Connection While (.State = 0) If (.State = 0) Then zDB2User = oMainSheet.Range(RANGE_DB_USR).Value zDB2Pwd = oMainSheet.Range(RANGE_DB_PWD).Value If (AccessLevel < AL_RO) Then bFirstLogin = True With DlgLogin .LoginReadOnly.Visible = True .LoginPM.Visible = True .LoginAdmin.Visible = True .LoginReadOnly.Value = 1 .UserName = USER_RO .Password = "member" .StartUpPosition = 1 'posCenterOwner PS = PositionForm(WhatForm:=DlgCalendar, AnchorRange:=ActiveSheet.Cells(1, 1)) .Top = PS.FrmTop ' set the Top position of the form .Left = PS.FrmLeft ' set the Left position of the form .Show '//RunUserInterface End With End If End If .ConnectionString = "Driver={IBM DB2 ODBC DRIVER};" _ & "Database=<myDB>;" _ & "Hostname=<myHost>;" _ & "Port=<myPort>;" _ & "Protocol=TCPIP;" _ & "Uid=" & zDB2User & ";" _ & "Pwd=" & zDB2Pwd & ";" '//set cursor location .CursorLocation = adUseClient '//open database .Open zQuery = "SET CURRENT SCHEMA = 'DCODB2'" Message zQuery .Execute zQuery, , adExecuteNoRecords If (.State = 1) Then With oMainSheet .Unprotect ("*") .Range(RANGE_DB_USR).Value = zDB2User .Range(RANGE_DB_PWD).Value = zDB2Pwd .Protect ("*") End With End If '//in case of failure try again to login PP iLoginTrials If (iLoginTrials > 3) Then zStr = "Access to PM database denied" MsgBox zStr Message zStr End End If Wend '//.State = 0 If (.State And bFirstLogin) Then ShowDlgDone "Connection to DB2 successfully established", vbModal Message "Userlevel: " & zDB2User End If End With '//oDB2Connection Exit Sub Errorhandler: zDB2Pwd = "" With oMainSheet .Unprotect ("*") .Range(RANGE_DB_USR).Value = "" .Range(RANGE_DB_PWD).Value = zDB2Pwd .Protect ("*") End With MsgBox "Error: " & ERR.Description Resume Next End Sub
修复步骤
按优先级依次操作即可解决:
- 修正ODBC配置路径
你当前用于测试驱动的SysWoW64\ODBCAD.exe是32位ODBC数据源管理器,64位Office只会读取C:\Windows\System32\odbcad32.exe中配置的64位数据源,首先打开64位ODBC管理器,重新配置DB2、MySQL驱动并测试连通性。 - 修正连接字符串的驱动名称
64位数据库ODBC驱动的注册名称通常和32位不一致,打开64位ODBC管理器的「驱动程序」标签,确认驱动的完整名称:
- DB2驱动常见名称为
IBM DB2 ODBC DRIVER - DB2COPY1,替换原有代码中的IBM DB2 ODBC DRIVER即可 - MySQL驱动同理,确认64位驱动的实际名称(如
MySQL ODBC 8.0 Unicode Driver)替换原有32位驱动名称
- 验证ADODB引用版本
打开VBA编辑器的「工具-引用」,确认Microsoft ActiveX Data Objects x.x Library的引用版本为2.8及以上通用版本,避免版本适配问题。 - 兼容优化方案(可选)
如果修改驱动名称后仍有问题,可以在64位ODBC管理器中创建系统DSN,连接字符串修改为"DSN=你创建的DSN名称;Uid=用户名;Pwd=密码;",跨环境兼容性更高。
内容的提问来源于stack exchange,提问作者user3544736
相关产品推荐
相关产品推荐

