我的公司计划从MS Office 365 32位迁移到相当于64位的版本。我已经在Excel中开发了一个工具,它可以连接到IBMDB2(也可以连接到MySQL数据源)。这个工具多年来在不同的32位MS Office版本(XP,2007,2013和365Pro)上通过不同的WinOS (XP/7/10,无论是32位还是64位)工作得很好。但在64位MS Office365Pro上运行该工具时,我在数据库连接上遇到了困难(所有其他障碍,例如PtrSafe声明都已解决)。我安装了64位版本的ODBC IBM DB2客户端,并在SysWoW64\ODBCAD中对其进行了配置,连接测试成功返回。就我而言,我认为至少司机是正常的。在VBA中调用打开连接时,我总是收到以下错误消息:
在我看来,Excel似乎不知何故没有认识到DB2驱动程序的->registration问题?下面是我的代码:
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
连接到MySQL的类似构造对于64位Excel也会失败。
发布于 2021-09-03 10:01:00
我自己发现的!查看HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\ODBC驱动程序的注册表,我看到驱动程序的名称看起来略有不同:"IBM DB2 ODBC driver - IBMDBCL1“
在我的代码中,我最初写道:"IBM DB2 ODBC DRIVER“
因此,通过上面的修复,我让它工作了,万岁!
https://stackoverflow.com/questions/69042653
复制相似问题