简体   繁体   中英

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. 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.

The VBA function is called IsOpcodePresent and takes two parameters which are provided in the query.

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. But with my own function it doesn't.

I am afraid that is impossible. You would need an MS Access instance.

There are two parts to an MS Access "database". The forms, reports and code, held in MS Access and the data, generally held in a Jet/ACE database. Your ADODB query refers to the Jet/ACE database, which does not have a connection to the "front-end". When you run the query in MS Access, it has a reference to the code.

Depending on the function, it may be possible to rewrite as a Data Macro if you are using Access 2010 or later. These will run even outside of MS Access as they are tied to the ACE database.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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