【发布时间】:2014-07-30 20:42:17
【问题描述】:
我正在尝试从 Excel 调用 Access 函数并收到此错误:
编译错误:只有在公共对象中定义的用户定义类型 模块可以强制转换为变体或从变体强制转换或传递给后期绑定 功能。
我尝试采用我找到的这个solution,但没有运气。这是我的代码:
在 Excel 模块 ExternalStatistics 中
Option Explicit
Public Type MyExternalStatistics
esMyInvites As Single
esMyInvitePerTalk As Single
End Type
Public MyExtRecStats As MyExternalStatistics
在 Sheet1(A-Crunched Numbers) 对象中:
Option Explicit
Public appRecruitingAccess As Access.Application
Public Sub Worksheet_Activate()
Dim MyExtRecStats As MyExternalStatistics
Dim RecruitWindow As Integer
Dim test As String
Set appRecruitingAccess = New Access.Application
With appRecruitingAccess
.Visible = False
.OpenCurrentDatabase "C:\Dropbox\RECRUITING\Remote0\Recruiting 0.accdb"
RecruitWindow = DateDiff("d", Format(Date, Worksheets("ActivityAndIncentive").Range("IncentiveStart").Value), Format(Date, Worksheets("ActivityAndIncentive").Range("IncentiveEnd").Value))
RecruitWindow = DateDiff("d", Format(Date, Worksheets("ActivityAndIncentive").Range("IncentiveStart").Value), Format(Date, Worksheets("ActivityAndIncentive").Range("IncentiveEnd").Value))
MyExtRecStats = .Run("ExternalRecruitingStats", RecruitWindow) '*** ERROR HERE ***
.CloseCurrentDatabase
.Quit
End With
Set appRecruitingAccess = Nothing
End Sub
在访问模块外部统计中
Option Compare Database
Option Explicit
Public Type MyExternalStatistics
esMyInvites As Single
esMyInvitePerTalk As Single
end Type
Public Function ExternalRecruitingStats(StatWindow As Integer) As MyExternalStatistics
Dim MyRecStats As MyExternalStatistics
Dim Invites As Integer, Talks As Integer
Invites = 1
Talks = 2
With MyRecStats
.esMyInvites = CSng(Invites)
.esMyInvitesPerTalk = CSng(Invites/Talks)
End With
ExternalRecruitingStats = MyRecStats 'return a single structure
End Function
它不喜欢MyExtRecStats = .Run("ExternalRecruitingStats", RecruitWindow) 语句。我想最终在 Access 函数中分配几个集合,并用一个对象将它们全部带回来。然后,我可以将这些值放在电子表格中应放在的位置。
【问题讨论】:
-
您是否有理由不使用 Excel 中的数据库查询来提取您需要的数据?在我看来,这会更容易、更有效。
-
最终我需要让该函数向 Excel 提供至少 36 个数据点。我正在使用一个字符串并解析该字符串,这是有效的,但我上面所做的研究似乎是完成它的一种更有效的方法。我不知道可以为我做到这一点的查询方法。接受建议...