Solved

How to loop through columns in DAO Recordset as each table is looped through in Access?

Posted on 2012-03-16
2
778 Views
Last Modified: 2012-03-16
Per the following code, I'm trying to loop through a long listing of tables linked to SQL Server and search out a value in every row and column... if it finds the value its looking for it puts in a temp table the table name and field name so that a person can go there to look at more information.  The problem is using DAO I don't know how to determine the number of columns which will be different per each table.  How may I rearrange below to determine number of columns so I can reiterate through the rs.Fields(x) to look up the values?

Again, I know what's wrong with this code already - I just don't know the syntax to loop through the columns as each table comes through....   A rearrangement of the code below would be ideal versus a mere explanation as to what is wrong.

Thank you sincerely!

Public Sub Find_This(sValue As String)

  Dim db As DAO.Database
  Dim docdef As DAO.Document
 
  Dim sSQL As String
  Dim rs As DAO.Recordset
 
  Dim sTable As String
  Dim sField As String
 
  sSQL = "DELETE * FROM [tblFound]"
  CurrentDb.Execute sSQL
  DoEvents
 
  Set db = CurrentProject.Application.CurrentDb
    With db.Containers!Tables
    For Each docdef In .Documents
   
      sTable = docdef.Name
     
      If "tblSQLTableNames" <> sTable And "tblFound" <> sTable Then
       
        sSQL = "SELECT * FROM [" & sTable & "]"
        Set rs = CurrentDb.OpenRecordset(sSQL)
        Do Until rs.EOF
       
          'instead of
          sField = rs.Fields(0)
       
          If sValue = sField Then
         
            sSQL = "INSERT INTO [tblFound] (FieldFound, TableFound) VALUES ('" & sField & "', '" & sTable & "')"
            CurrentDb.Execute sSQL
            DoEvents
           
          End If
         
          rs.MoveNext
        Loop
        rs.Close
        Set rs = Nothing
       
      End If
    Next docdef
    End With
    db.Close
  Set db = Nothing
 
End Sub
0
Comment
Question by:stephenlecomptejr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 37729933
start with this

Dim j as integer, fldName as string
        sSQL = "SELECT * FROM [" & sTable & "]"
        Set rs = CurrentDb.OpenRecordset(sSQL)
'loop thru the recordset fields
      for j= 0 to rs.fields.count-1
           fldName=rs(j).name
           
debug. print fldName
       next
0
 
LVL 1

Author Closing Comment

by:stephenlecomptejr
ID: 37729978
Always gracious for the help capricorn!
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

742 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question