问题描述
我正在处理一些在 VBA 6 中使用 MS Access 数据库的代码.我的目标是使相同的代码也可以与 PostgreSQL 一起使用(这意味着它应该能够与两者一起使用).该代码与 DAO.DBEngine
一起使用,但我还没有找到使此类连接到 PostgreSQL 数据库的方法.我的解决方案是使用与 DAO.DBEngine
相同的方法创建另一个类,例如原始代码使用 TableDefs,当我创建一个名为 TableDefs 的函数时,我得到了检测到的模糊名称"..有没有办法将两个方法命名为相同的类或抑制/避免这种检测到的模糊名称"?错误?
I'm working on some code working with MS Access database in VBA 6. My goal is to make this same code working with PostgreSQL too (this means it should be able to work with both) . The code is working with DAO.DBEngine
but I've not found a way to make this class connect to a PostgreSQL database. My solution is to create an other class with the same methods as DAO.DBEngine
, for example the original code is using TableDefs and when I'm create a function called TableDefs I got "ambiguous name detected". Is there any way to name two methods class the same or to suppress/avoid this "ambiguous name detected" error ?
推荐答案
基于 link 你可以创建三个类
Based on the information provided in the link you could create three classes
IDatabase
Option Explicit
Function Hello()
End Function
clsDBAccess
Option Explicit
Implements IDatabase
Function IDatabase_Hello()
MsgBox TypeName(Me)
End Function
clsPostSQL
Implements IDatabase
Function IDatabase_Hello()
MsgBox TypeName(Me)
End Function
你可以这样使用它
Option Explicit
Sub Main()
Dim myType As String
Dim oDatbase As IDatabase
' Determine myType
myType = "Access"
' myType = "PostSQL"
Set oDatbase = ClassFactory(myType)
' Your code here
oDatbase.Hello
End Sub
Function ClassFactory(dbType As String) As IDatabase
Dim oDatabase As IDatabase
If dbType = "Access" Then
Set oDatabase = New clsDBAccess
ElseIf dbType = "PostSQL" Then
Set oDatabase = New clsPostSQL
End If
Set ClassFactory = oDatabase
End Function
这篇关于创建具有现有名称的类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!