473,386 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Searching entire database for string

Hi, I am not a computer programmer. I have been learning VBA along the way of making this script. I know Access and SQL.

I have a very large database with many tables and queries. My goal is to write a script or function that will search the entire database for a certain string (actually two strings).

I have tried using a series of queries and unioning them together but this is a very scrappy way of doing this. Perhaps there is a method of automatically searching every field in every table for a string? I'm sure this has been done before but I have been searching the internet for about a week and have come up with nothing, so here I am, asking for help.

Any input is GREATLY appreciated; I am getting flustered due to my lack of knowledge with VBA!

Thanks!
Ian
Jul 16 '08 #1
9 16250
ADezii
8,834 Expert 8TB
Just subscribing, when I get a chance I'll see what I can come up with.
Jul 17 '08 #2
Delerna
1,134 Expert 1GB
Hi, this is an extract from some code I had in an Access form
It should get you started

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSearchTheTables_Click()
  2.     Dim DB As dao.Database
  3.     Dim tbl As dao.TableDef
  4.     Dim fld As dao.Field
  5.  
  6.     Set DB = CurrentDb
  7.     For Each tbl In DB.TableDefs
  8.         For Each fld In tbl.Fields
  9.             If fld.Value = "Search string" Then MsgBox "Found it" 
  10.         Next
  11.     Next
  12.     Set tbl = Nothing
  13.     Set fld = Nothing
  14.     Set DB = Nothing
  15. End Sub
  16.  
you will of course need a reference to "Microsoft DAO 3.6 object library"
Jul 17 '08 #3
Ah! The code makes sense to me, and I have DAO 3.6 installed and enabled.

I have attached the code to a button called cmdSearchTheTables.

However, it says the IF statement
Expand|Select|Wrap|Line Numbers
  1. If fld.Value Like "*MK 5*" Then
is an invalid operation.

I tried a few different syntax forms for the line, but it still returned the same error.

Am I missing something? Do I need to assign a text field to output the result into?
Jul 17 '08 #4
OK. I have added much to the Sub. My code now reads as:
Expand|Select|Wrap|Line Numbers
  1.  
  2. Public Sub GenerateFieldSizeInfo()
  3.     Dim cdb As database
  4.     Dim oTable As TableDef
  5.     Dim oField As field
  6.     Dim rsTable As Recordset
  7.     Dim strSQLTable As String
  8.     Dim iMaxSize As Integer
  9.     Dim iHeadroom As Integer
  10.     Dim iFileNumber As Integer
  11.     Dim strOutputFile As String
  12.  
  13.     Dim ddb As database
  14.     Dim dtbl As TableDef
  15.     'Dim dfld As field
  16.  
  17.     'Create the header file. (If the file already exists, its contents are overwritten.)
  18.     iFileNumber = 1
  19.     strOutputFile = "C:\etc,etc."
  20.  
  21.     Open strOutputFile For Output As #iFileNumber
  22.     'Open the NWSS database.
  23.     Set cdb = CurrentDb
  24.  
  25.     Print #iFileNumber, " Table"; Tab(40); "Field"; Tab(70); "Relevant Text"; Tab(101); "Size"; Tab(111); "Max"; Tab(117); "Headroom"
  26.     Print #iFileNumber, String(125, "-")
  27.     For Each oTable In cdb.TableDefs
  28.       'If (StrComp("tbl", Left$(oTable.Name, 3)) = 0) Then
  29.         For Each oField In oTable.Fields
  30.           If (oField.Type = 10) Then
  31.             Print #iFileNumber, " "; oTable.Name;
  32.             Print #iFileNumber, Tab(40); oField.Name; Tab(104 - Len(Str(oField.Size))); oField.Size;
  33.             strSQLTable = "SELECT " + "[" + oField.Name + "]"
  34.             strSQLTable = strSQLTable + " FROM " + "[" + oTable.Name + "]"
  35.             strSQLTable = strSQLTable + " ORDER BY " + "[" + oField.Name + "]"
  36.             Set rsTable = cdb.OpenRecordset(strSQLTable, dbOpenDynaset)
  37.             If Not rsTable.EOF Then
  38.               rsTable.MoveFirst
  39.             End If
  40.             Do Until rsTable.EOF
  41.               If oField.Value Like "*Search text*" Then
  42.                 Print #iFileNumber, Tab(70 - Len(Str(oField))); oField
  43.               End If
  44.               rsTable.MoveNext
  45.             Loop
  46.             iMaxSize = 0
  47.             Do Until rsTable.EOF
  48.               If (Len(rsTable.Fields(0).Value) > iMaxSize) Then
  49.                 iMaxSize = Len(rsTable.Fields(0).Value)
  50.               End If
  51.               rsTable.MoveNext
  52.             Loop
  53.             iHeadroom = oField.Size - iMaxSize
  54.             Print #iFileNumber, Tab(114 - Len(Str(iMaxSize))); iMaxSize;
  55.             Print #iFileNumber, Tab(124 - Len(Str(iHeadroom))); iHeadroom
  56.             rsTable.Close
  57.           End If
  58.         Next
  59.           Print #iFileNumber, ""
  60.       'End If
  61.     Next
  62.     Close #iFileNumber
  63. End Sub
  64.  
I am still getting the "run-time error '3219', invalid operation" message when I try to run the script. It pertains to the IF statement in line 40. Anyone have any suggestions? I am puzzled because this should work!
Jul 17 '08 #5
missinglinq
3,532 Expert 2GB
Just out of curiosity's sake, in what real-world situation would you need to search every field of every table for a given search string?

Welcome to Bytes!

Linq ;0)>
Jul 17 '08 #6
Just out of curiosity's sake, in what real-world situation would you need to search every field of every table for a given search string?

Welcome to Bytes!

Linq ;0)>
This is a task given to me for my job. It is to find data sensitive to a certain location. The script will be able to be used on any database made in the future; henceforth, creating a script to search every field is the most efficient way to go about this.

After the script is fully functional on VBA, I plan to translate it into VB and have it read a given database which is inputted by the user.
Jul 17 '08 #7
Delerna
1,134 Expert 1GB
hmmmm
the function LIKE compares two strings.
Maybe its having a problem with one of them being a field object.
Try
Expand|Select|Wrap|Line Numbers
  1. Dim str as string
  2. str=fld.value
  3. if str like "*MK 5*" Then
  4.  
Jul 17 '08 #8
ADezii
8,834 Expert 8TB
I've written an Algorithm for you that will search every Field in every Table for a Search String that you specify. System Tables, those prefaced with 'MSys' or 'USys', are ignored. If a match(es) are found, the Record Number, Table Name, Field Name, and the actual Field Value containing the String are written to the Immediate Window. The Function will return True if successful, otherwise False. The code is very flexible in that the results can easily be saved to a Table, written to a List Box, printed to a Text File, etc. with little modification. You can simply call the Function within an Expression passing it the required Search String, then test for success. I did my testing on all the Tables of the Northwind Database, looking for the String 'Seafood'. For testing purposes, I added this String to several Fields, in multiple Tables. The Function Call and subsequent results are posted below:
Expand|Select|Wrap|Line Numbers
  1. Public Function fSearchForString(strSearchString) As Boolean
  2. On Error GoTo Err_fSearchForString
  3.  
  4. Dim tdf As DAO.TableDef
  5. Dim MyDB As Database
  6. Dim MyRS As Recordset
  7. Dim intNumOfFields As Integer
  8. Dim intCounter As Integer
  9.  
  10. 'Let's not get ridiculous, need at least 4 Characters
  11. If Len(strSearchString) <= 3 Then Exit Function
  12.  
  13. Set MyDB = CurrentDb
  14.  
  15. DoCmd.Hourglass True
  16.  
  17. Debug.Print "The String [" & strSearchString & "] has been found in the following locations:"
  18. Debug.Print "******************************************************************************************"
  19. Debug.Print
  20.  
  21. For Each tdf In CurrentDb.TableDefs
  22.   If Not tdf.Name Like "MSys*" Or Not tdf.Name Like "MSys*" Then    'ignore System Tables
  23.     Set MyRS = MyDB.OpenRecordset(tdf.Name, dbOpenDynaset)
  24.       intNumOfFields = MyRS.Fields.Count
  25.         For intCounter = 0 To intNumOfFields - 1
  26.           Do While Not MyRS.EOF
  27.             If InStr(MyRS.Fields(intCounter).Value, strSearchString) > 0 Then
  28.               Debug.Print "Rec Num: " & MyRS.AbsolutePosition + 1 & " | " & _
  29.                           "Table: " & tdf.Name & " | " & "Field: " & _
  30.                            MyRS.Fields(intCounter).Name & " | Value: " & _
  31.                            MyRS.Fields(intCounter).Value
  32.             End If
  33.             MyRS.MoveNext
  34.           Loop
  35.           MyRS.MoveFirst
  36.         Next
  37.   End If
  38. Next
  39.  
  40. DoCmd.Hourglass False
  41.  
  42. MyRS.Close
  43. Set MyRS = Nothing
  44.  
  45. fSearchForString = True
  46.  
  47. Exit_fSearchForString:
  48.   Exit Function
  49.  
  50. Err_fSearchForString:
  51.   fSearchForString = False
  52.   MsgBox Err.Description, vbExclamation, "Error in fSearchForString()"
  53.   DoCmd.Hourglass False
  54.   If Not MyRS Is Nothing Then
  55.     MyRS.Close
  56.     Set MyRS = Nothing
  57.   End If
  58.     Resume Exit_fSearchForString
  59. End Function
Call to Function:
Expand|Select|Wrap|Line Numbers
  1. If fSearchForString("Seafood") Then
  2.   MsgBox "Search  for String a success!", vbExclamation, "Success"
  3. Else
  4.   MsgBox "What in the world have you done, ADezii!", vbCritical, "Failure"
  5. End If
Sample OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. The String [Seafood] has been found in the following locations:
  2. ******************************************************************************************
  3.  
  4. Rec Num: 8 | Table: Categories | Field: CategoryName | Value: Seafood
  5. Rec Num: 12 | Table: Employees | Field: LastName | Value: Seafood
  6. Rec Num: 5 | Table: Employees | Field: Title | Value: Sales Seafood Manager
  7. Rec Num: 1 | Table: Products | Field: ProductName | Value: Chai Seafood
  8. Rec Num: 23 | Table: Products | Field: ProductName | Value: Tunnbröd Seafood
  9. Rec Num: 37 | Table: Products | Field: ProductName | Value: Seafood Gravad lax
  10. Rec Num: 4 | Table: Shippers | Field: CompanyName | Value: Seafood Express
  11. Rec Num: 19 | Table: Suppliers | Field: CompanyName | Value: New England Seafood Cannery
Jul 17 '08 #9
Perfect! Thank you very much. I played around with the code for a bit and I ended up combining my script with yours, and it works perfectly!
Jul 22 '08 #10

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

Similar topics

4
by: Michi | last post by:
I was wondering what the best solution is for making large numbers of TEXT (or BLOB?) fields searchable. For example, if I have a forum, what is the best way to be able to search for specific...
3
by: Paul H | last post by:
I have a text file that contains the following: ******************** __StartCustomerID_41 Name: Fred Smith Address: 57 Pew Road Croydon
7
by: junky_fellow | last post by:
Can anyone suggest some efficient way to search a substring in a text file. For eg. suppose i want to search the pattern "abc" in a text file, then which algorithm should i use. I just want some...
33
by: Geoff Jones | last post by:
Hiya I have a DataTable containing thousands of records. Each record has a primary key field called "ID" and another field called "PRODUCT" I want to retrieve the rows that satisy the following...
1
by: Jon Beverley | last post by:
I am looking for some assistance with searching a third part LDAP database. I have managed to read from it and write to it using Dim objAD As ActiveDs.IADs Dim objDS As ActiveDs.IADsOpenDSObject...
1
by: thehumantrashcan | last post by:
Hi, This is the first database I have ever created, so please bear with me. I've created a simple database with 1 column and about 80,000 rows. In each row is a word (basically a dictionary...
4
by: Hunk | last post by:
Hi I have a binary file which contains records sorted by Identifiers which are strings. The Identifiers are stored in ascending order. I would have to write a routine to give the record given...
2
by: neilio2j | last post by:
hi, can anyone tell me how to search a given directory using C++? so far i have been successful in searching a file named, for tags"h:\\tags.cpp", whereas i would like to search for all files...
0
by: Kassimu | last post by:
Hi guys out there, There is this database Iam creating, I have a table with 40 fields among which there are Date/time, Text, Number, Memo and Yes/No fields and I have created the form bound to that...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

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.