Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse ADOX to search the queries in an Access database for a string in Visual Basic .NET
DescriptionThis example shows how to use ADOX to search the queries in an Access database for a string in Visual Basic .NET.
KeywordsADOX, Access, database, query, command text, search query, VB.NET
CategoriesDatabase
 
First add references to the COM libraries:

  • Microsoft ActiveX Data Objects 2.6 Library
  • Microsoft ADO Ext. 2.6 for DLL and Security

(Or whatever your versions are.)

When you click the Search Queries button, the following code executes. It opens the database and uses the ADOX catalog to learn about the database. It loops through the Views collection and searches each query's command text for the target string. If it finds the string, it lists the query's name and command text in a ListView control.

 
Private Sub btnSearchQueries_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btnSearchQueries.Click
    lvwTables.Items.Clear()

    ' Open the connection.
    Dim conn As New ADODB.Connection
    conn.ConnectionString = _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Persist Security Info=False;" & _
        "Data Source=" & txtDatabase.Text
    conn.Open()

    ' Make a catalog for the database.
    Dim cat As New ADOX.Catalog
    cat.ActiveConnection = conn

    ' Search the catalog's views.
    Dim target As String = txtSearchFor.Text.ToLower()
    For i As Integer = 0 To cat.Views.Count - 1
        Dim query As String = _
            cat.Views(i).Command.CommandText
        query = query.Replace(vbCr, " ").Replace(vbLf, " ")
        If query.ToLower().IndexOf(target) >= 0 Then
            Dim lvi As ListViewItem = _
                lvwTables.Items.Add(cat.Views(i).Name)
            lvi.SubItems.Add(query)
        End If
    Next i

    conn.Close()

    lvwTables.Columns(0).Width = -2
    lvwTables.Columns(1).Width = -2
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated