Search code examples
c#ms-accessinteropoledb

Getting Data from acQuery in C#-MS-Access-Interop


Say we've got a open Database which is running a few processes before it finishes up with acQueries which print out an result to the Access-Window. I figured out that I'm able to Access all open Queries with:

AccessObject singleQuery in currApplication.CurrentData.AllQueries

as the project carries more then one Query and not all give out an result, I'm try'n and catching on the CurrentView-Property which throws an Exception when accessed and Query is not finished and open.

I now know which AccessObject in singleQuery is the searched one, but now I'm stuck on getting the Data that is printed out on the Access screen. (could be that the object doesn't includes any Information about actual data, but that would be horrible^^)

There is this solution with using OleDb-Database reader (similiar to that: Run Access Queries via Interop?), at least I think it could apply in this case, I hope you can pass the wanted, open query in, but can retrieving Data from an Open Query can be another way, when I can assure it's run and valid?

Another option was DoCmd to Output the results to a file and read from it again, but that doesn't seem clean also: https://msdn.microsoft.com/en-us/library/office/ff192065.aspx


Solution

  • Ok here we are:

    To mention it first, I have no idea if cdb.OpenRecordset(x.Name) reexecutes the Query, if this is unwanted behaviour you have to check it first. Here it does make no difference and this works for me:

           foreach (dynamic x in currApplication.CurrentData.AllQueries)
            {
                try
                {
                    var temp = x.CurrentView;
                    Access.AccessObject temp2 = x;
                    Microsoft.Office.Interop.Access.Dao.Database cdb = currApplication.CurrentDb();
                    Microsoft.Office.Interop.Access.Dao.Recordset rst = cdb.OpenRecordset(x.Name);
                    rst.MoveFirst();
                    do
                    {
                        foreach (Microsoft.Office.Interop.Access.Dao.Field field in rst.Fields)
                        {
                            Console.WriteLine(field.Name);
                            Console.WriteLine(field.Value);
                        }
                        rst.MoveNext();
                    } while (rst.EOF != true);
                    /*object[,] z = rst.GetRows();
                    for (int i = 0; i <= z.GetUpperBound(0); i++)
                    {
                        Console.WriteLine(z[i, 0].ToString());
                    }*/
                    Console.ReadLine();
                }
                catch (Exception ex)
                {
    
                }
            }
    

    This prints out all Data + Fieldnames of all opened Queries to Console-Out. Try-catch is necessary to get if the query is currently open, this is the condition in my problem maybe it is not needed for yours.

    Gladly way less complex then I thought it would be.