Solved

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

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

Accepted Solution

by:
Rey Obrero 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 Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now