简体   繁体   English

如何从VB6使用MS Access公共功能

[英]How to use MS Access Public Function from VB6

I have a MS Access database with a Public Function defined in VBA, stored in Module1. 我有一个MS Access数据库,在VBA中定义了一个公共功能,存储在Module1中。 I would like to include the function as part of a query which is handed to ADODB from VB6 by ADODB.RecordSet's Open method. 我想将该函数作为由ADODB.RecordSet的Open方法从VB6处理到ADODB的查询的一部分。

The VBA function is called IsOpcodePresent and takes two parameters which are provided in the query. VBA函数称为IsOpcodePresent,它采用查询中提供的两个参数。

The query (abbreviated) is 查询(缩写)为

SELECT * FROM tbl WHERE IsOpcodePresent(example,syl) order by syl;

Can this be done? 能做到吗? If I use standard Access functions like IsNull, then it works. 如果我使用像IsNull这样的标准Access函数,那么它将起作用。 But with my own function it doesn't. 但是用我自己的功能却没有。

I am afraid that is impossible. 恐怕这是不可能的。 You would need an MS Access instance. 您将需要一个MS Access实例。

There are two parts to an MS Access "database". MS Access“数据库”分为两部分。 The forms, reports and code, held in MS Access and the data, generally held in a Jet/ACE database. 表格,报告和代码(保存在MS Access中)和数据通常保存在Jet / ACE数据库中。 Your ADODB query refers to the Jet/ACE database, which does not have a connection to the "front-end". 您的ADODB查询引用了Jet / ACE数据库,该数据库没有与“前端”的连接。 When you run the query in MS Access, it has a reference to the code. 在MS Access中运行查询时,它具有对代码的引用。

Depending on the function, it may be possible to rewrite as a Data Macro if you are using Access 2010 or later. 根据功能的不同,如果您使用的是Access 2010或更高版本,则可能会将其重写为数据宏 These will run even outside of MS Access as they are tied to the ACE database. 由于它们与ACE数据库绑定,因此它们甚至可以在MS Access外部运行。

Function GetAccessRecordset(sDatabase As String, sSQL As String) As Variant
    Dim oAccess As Object
    Set oAccess = CreateObject("Access.Application")
    oAccess.OpenCurrentDatabase (sDatabase)
    oAccess.Visible = False
    Dim dbs As Object
    Set dbs = oAccess.CurrentDb.OpenRecordSet(sSQL)
    GetAccessRecordset = dbs.GetRows(dbs.RecordCount)
    dbs.Close
    oAccess.Quit
End Function

This is what I wanted. 这就是我想要的。 It works fine provided one has either no script blocking or else that the script is signed and the database accepts the signature. 只要没有脚本阻止或脚本已签名并且数据库接受签名,它就可以正常工作。 It gives me the 2D array of results and lets me use script-based functions. 它为我提供了2D结果数组,并允许我使用基于脚本的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM