Solved

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

Posted on 2012-03-16
2
777 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
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
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.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
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…

740 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