dao への参照を追加するだけで、.net コードで dao recorset を使用できるはずです。VS で、[ソリューション エクスプローラー] > [参照] > [参照の追加] > [.Net] を開きます。
例: C:\Program Files (x86)\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office12\dao.dll.
VBA コードにアクセスします。
Option Compare Database
Option Explicit
Public Sub Test()
Dim db As DAO.Database
Dim rec As DAO.Recordset
Set db = Application.CurrentDb
Set rec = db.OpenRecordset("Table1")
Dim dotNetObject As New COMAccessibleTest.ReadData
dotNetObject.GetData rec
rec.Clone
db.Close
End Sub
C# コード:
using System.Runtime.InteropServices;
using System.Text;
namespace ReadDataTest
{
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("772de12c-3ade-4f43-9981-fd3c39691520")]
public interface IReadData
{
void GetData(ref dao.Recordset data);
}
[ClassInterface(ClassInterfaceType.None)]
[Guid("2acf2296-63b0-47b2-ab77-51c4e35e7365")]
[ProgId("ReadDataTest")]
public class ReadData : IReadData
{
public void GetData(ref dao.Recordset data)
{
var lines = new StringBuilder();
data.MoveFirst();
while (!data.EOF)
{
lines.AppendLine(data.Fields[0].Value + "," + data.Fields[1].Value + "," + data.Fields[2].Value + "," + data.Fields[3].Value);
data.MoveNext();
}
var file = new System.IO.StreamWriter("c:\\Temp\\test.txt");
file.WriteLine(lines);
file.Close();
}
}
}