473,382 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,382 developers and data experts.

Search QueryDefs, Reports and Form controls for a string

TheSmileyCoder
2,322 Expert Mod 2GB
Introduction
This article will explain how you can search your current application for a specified string.

Not often, but once in a while I need to change a field name, in order to for it to make more sense, often because I need to add an extra field, and with the new field, there might be ambiguity as to the meaning of each field.

Therefore I made a simple bit of code that will check (not auto-correct) your QueryDefs, the control source of controls in your your forms and reports.

Note that stored queries (such as a forms Recordsource, or a combobox Rowsource) are stored in the QueryDefs collection, and as such are included in this search.



Implementation:
I have created it as three separate functions, one for queries,one for forms, and one for reports, as well as a function combining the use of all three. The functions are shown below:

For Queries
Expand|Select|Wrap|Line Numbers
  1. Public Sub SearchQueryDefs(strSearchWord As String)
  2.    Dim qdf As DAO.QueryDef
  3.    Dim strSQL As String
  4.    For Each qdf In CurrentDb.QueryDefs
  5.       strSQL = qdf.SQL
  6.       If InStr(1, strSQL, strSearchWord, vbTextCompare) > 0 Then
  7.          Debug.Print "Query: " & qdf.Name
  8.       End If
  9.  
  10.    Next
  11.    Set qdf = Nothing
  12. End Sub
Example output looks like:
Expand|Select|Wrap|Line Numbers
  1. Query: ~sq_ffrm_List_My_Reviews
  2. Query: ~sq_ffrm_ListReviewsByDate
  3. Query: qry_ExportReviewOverviewToExcel
  4. Query: qry_ReviewDoc
You may notice the ~sq which means this is a stored query, in this first case its the underlying recordsource for my frm_List_My_Reviews.

On occasion while using this you might find that a query which has been deleted is returned. This is due to the way access stores/caches the queries. You can usually remove these by a Compact And Repair operation. If not this could be a sign of database corruption.

For Forms
For forms, I open each form in design view, and loop over the controls collection. I only check Textbox, combobox, listbox and checkbox, but if needed it could be expanded to check such items as bound pictures. Finally the form is closed again, ensuring that no changes are saved. BEFORE RUNNING you should close all forms.
Expand|Select|Wrap|Line Numbers
  1. Public Sub searchForms(strSearchWord As String)
  2.    Dim oAO As Object
  3.    Dim frm As Form
  4.    Dim ctrl As Object
  5.    For Each oAO In CurrentProject.AllForms
  6.       DoCmd.OpenForm oAO.Name, acDesign
  7.       Set frm = Forms(oAO.Name)
  8.       For Each ctrl In frm.Controls
  9.          Select Case ctrl.ControlType
  10.  
  11.             Case acTextBox, acComboBox, acListBox, acCheckBox
  12.                If InStr(1, ctrl.ControlSource & "", strSearchWord) Then
  13.                   Debug.Print "Form: " & frm.Name & ": " & ctrl.Name
  14.                End If
  15.  
  16.          End Select
  17.       Next
  18.       DoCmd.Close acForm, oAO.Name, acSaveNo
  19.    Next
  20.  
  21.  
  22.    Set oAO = Nothing
  23.    Set frm= Nothing
  24.    Set ctrl = Nothing
  25. End Sub
Example output looks like:
Expand|Select|Wrap|Line Numbers
  1. Form: frm_ReviewDetails: tb_DateSubmitted
  2. Form: frm_Obs: tb_DateSubmitted
For Reports
This implementation is quite similar to the how the forms are searched exept I loop over the AllReports collection.
Expand|Select|Wrap|Line Numbers
  1. Public Sub searchReports(strSearchWord As String)
  2.    Dim oAO As Object
  3.    Dim rpt As Report
  4.    Dim ctrl As Object
  5.    For Each oAO In CurrentProject.AllReports
  6.       DoCmd.OpenReport oAO.Name, acDesign
  7.       Set rpt = Reports(oAO.Name)
  8.       For Each ctrl In rpt.Controls
  9.          Select Case ctrl.ControlType
  10.  
  11.             Case acTextBox, acComboBox, acCheckBox
  12.                If InStr(1, ctrl.ControlSource & "", strSearchWord) Then
  13.                   Debug.Print "Report:" & rpt.Name & ": " & ctrl.Name
  14.                End If
  15.  
  16.          End Select
  17.       Next
  18.       DoCmd.Close acReport, oAO.Name, acSaveNo
  19.    Next
  20.  
  21.  
  22.    Set oAO = Nothing
  23.    Set rpt = Nothing
  24.    Set ctrl = Nothing
Example Output:
Expand|Select|Wrap|Line Numbers
  1. Report:rep_Main: tb_DateSubmitted
  2. Report:supRep_Obs: tb_DateSubmitted

Modules
While it is possible to loop over the modules and check them in a similar way, its easy enough to use standard Find built into the VB-Environment so I have not made code for this.

Combine the 3 into a single call
For ease of use, I choose to combine the 3 functions into a single call:
Expand|Select|Wrap|Line Numbers
  1. Public Sub SearchDBObjects(strSearchWord As String)
  2.     SearchQueryDefs strSearchWord
  3.     searchForms strSearchWord
  4.     searchReports strSearchWord
  5. End Sub

I hope this can be usefull to someone. If you found this article usefull please post here, link to it, share it. You are welcome to use any and all parts of the code shown.
Aug 28 '12 #1
10 13270
twinnyfo
3,653 Expert Mod 2GB
Wow, Smiley! You read my mind. I was just thinking about posting this as a question, as I have had to do manual searches in the past. What a time saver! Thanks for your article!
Aug 28 '12 #2
TheSmileyCoder
2,322 Expert Mod 2GB
Thanks for the feedback. Its nice to know that the time spent on writing the article was not wasted. :)
Aug 28 '12 #3
twinnyfo
3,653 Expert Mod 2GB
And........... Thanks to this handy dandy little code, I just had to use it this morning. Rather than searching 400 queries by hand, I could now identify the 60 queries affected ....

Endless time save, Smiley! Thanks again!
Aug 29 '12 #4
twinnyfo
3,653 Expert Mod 2GB
Smiley,

Your code above works for the QueryDefs, but when I look in the Reports and Forms, I get an error when I get to this line:

Expand|Select|Wrap|Line Numbers
  1. For Each oAO In CurrentProject.AllForms
  2. For Each oAO In CurrentProject.AllReports
  3.  
When I checked it at home, it worked fine. Office has 2007, Home has 2010.

Any ideas why these would behave differently?
Aug 30 '12 #5
TheSmileyCoder
2,322 Expert Mod 2GB
I am guessing that AccessObject has only been added in version 2010. Replace the lines
Expand|Select|Wrap|Line Numbers
  1. Dim oAO As AccessObject
with
Expand|Select|Wrap|Line Numbers
  1. Dim oAO As Object
and the code should run fine in Access 2007.

I will update the original post to be compatible with Access 2007. I do believe that this new version is Ac2000+ compatible.
Aug 30 '12 #6
twinnyfo
3,653 Expert Mod 2GB
Many thanks as usual, Smiley!
Aug 30 '12 #7
twinnyfo
3,653 Expert Mod 2GB
Still getting the same error on the same lines:

Invalid qualifier
Aug 30 '12 #8
twinnyfo
3,653 Expert Mod 2GB
Here is what I have found to get rid of any errors:

Expand|Select|Wrap|Line Numbers
  1. Public Sub SearchForms(strSearchWord As String)
  2.     Dim dbs As Object
  3.     Dim oAO As AccessObject
  4.     Dim frm As Form
  5.     Dim ctrl As Object
  6.     Set dbs = Application.CurrentProject
  7.     For Each oAO In dbs.AllForms
  8.         DoCmd.OpenForm oAO.Name, acDesign
  9.         Set frm = Forms(oAO.Name)
  10.         For Each ctrl In frm.Controls
  11.             Select Case ctrl.ControlType
  12.                 Case acTextBox, acComboBox, acListBox, acCheckBox
  13.                     If InStr(1, ctrl.ControlSource & "", strSearchWord) Then
  14.                         Debug.Print "Form: " & frm.Name & ": " & ctrl.Name
  15.                     End If
  16.             End Select
  17.         Next
  18.         DoCmd.Close acForm, oAO.Name, acSaveNo
  19.     Next
  20.     Set oAO = Nothing
  21.     Set frm = Nothing
  22.     Set ctrl = Nothing
  23. End Sub
  24.  
The report version of this code works and works on most of my forms, but one one particular form, I get a strange error:

You entered an expression that has an invlaid reference to the property ControlSource.

Any ideas? I'm trying to figure out which control and where this error is occuring.
Aug 30 '12 #9
twinnyfo
3,653 Expert Mod 2GB
It appears that checkboxes within option groups do not have a control source. The option group itself has a control source, but the individual checkboxes do not. How do we check to see if a control has a particular property available?
Aug 30 '12 #10
twinnyfo
3,653 Expert Mod 2GB
Also, just thinking about this, Smiley.... You mentioned in the original post that it is easy enough to search modules for a string. however, if one has several modules, and many forms and reports with modules, it would entail searching each module individually. I tried playing with the code for the modules, but admittedly, I have no idea where to start....
Oct 16 '12 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Davey | last post by:
Is it possible to add new form controls such as <select> and <input> controls using JavaScript - all client-side and on-the fly?
0
by: Lauren Quantrell | last post by:
In terms of resources, wondering if it makes a difference in adopting one of the three options below for resizing controls on a form and controls on a subform at the same time. option a.) Put...
4
by: Dries De Rudder | last post by:
Hi, I am trying to bind an object to form controls. I've got an object MyObject which has some properties e.g. ID. I've got a form, Form1, that contains a textbox ,TextBox1, now I want to...
2
by: Adam Right | last post by:
Hi, I want to get the controls of the windows form. I know that form.Controls give me the list of controls but i have a problem. For example, the toolstripmenu control cannot be reached with...
8
by: Ryan | last post by:
Ok.. I have a form with lots of stuff on it; a tool strip panel, menu strip, data binding elements (dataset, binding source, table adapter), tab control with 7 tab pages, each page contains a...
4
by: DFS | last post by:
'Search all reports for a text box named something Dim cMsg As String, db As Database, rs As Recordset, rpt As Report, ctl as Control Set db = CurrentDb() cMsg = "BuildingID" Set rs =...
16
by: Mike | last post by:
Hi, I have a form with some controls, and a different class that needs to modify some control properties at run time. Hoy can I reference the from so I have access to its controls and...
11
by: dhtml | last post by:
(originally mis-posted on m.p.s.jscript...) I've just closed all windows in Firefox and its using 244MB of memory. I have no idea why. I had GMail open, a page from unicode, the CLJ FAQ. ...
9
by: dhtml | last post by:
I have written an article "Unsafe Names for HTML Form Controls". <URL: http://jibbering.com/faq/names/ > I would appreciate any reviews, technical or otherwise. Garrett --...
10
by: =?Utf-8?B?UmljaA==?= | last post by:
A lot of users at my workplace use different screen resolutions, and I build apps to use 1680 x 1050 pixels res by default. But some users are using 800 x 600, and the apps are too large for their...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.