I have a perplexing problem that I have been grappling with for many hours to no avail.
Our (general audience) application compares queries in the latest version of our MS-Access database with end-users currently installed versions and and updates/syncs. the DB accordingly. Although this functionality has been working, it is now broken. I have resorted to the simplest test to demonstrate the behavior. OLEDB is NOT returning the correct number of queries.
After opening the Access 2007 database, I display the location and correct number of queries:
?CurrentDB.Name
C:\Users\Ron\Documents\Database4.accdb
?CurrentDB.QueryDefs.Count
1
The following simple form calls the OleDbSchemaTable method, but returns the wrong number of rows / queries (=0):
Public Class Form1
Private ConnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Ron\Documents\Database4.accdb"
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim dbo As DataTable = GetSchemaInfo()
Debug.Print("Number of queries=" & dbo.Rows.Count)
End Sub
Private Function GetSchemaInfo()
Try
Dim db As DataTable
Using conn = New OleDb.OleDbConnection(ConnStr)
conn.Open()
db = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Procedures, Nothing)
End Using
Return db
Catch ex As Exception
MsgBox(ex.Message)
Return Nothing
End Try
End Function
End Class
Immediate Window:
Number of queries=0
I have tried everything I can think of (Compact/Repair, running on a different machine). Unfortunately, I have only one copy of Access 2007 to test with. Could this be a virus?
Any and all ideas are appreciated.
There are 2 types of saved queries.
SELECTqueries which could be created withCREATE VIEWDDL statements.CREATE VIEWbut could be withCREATE PROCEDURE. Included within this category are: “action queries” (insert, update, delete, “make table”); parameter queries; simpleSELECTqueries which include anORDER BYclause; and perhaps more I can’t recall at the moment.Schema views include the first type. Schema procedures include the second type.
QueryDefs.Countgives you the count of all saved queries, which includes both types.Try the VBA procedure below in your database. With my database, I get this output in the Immediate window:
Unfortunately I don’t know how to translate this VBA to Dot.Net. Perhaps that doesn’t matter. I just want to emphasize that
QueryDefs.Countshould be the count of views plus the count of procedures. And it looks to me like your code asks for procedures only.