Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936

    Conditional "For each..."

    I have a piece of code coming from a command button that opens another database, reads thru all its objects and does a whole bunch of other stuff. This code can be initiated either from a table (For each tdf in dbs.Tabledefs) or from a query (For each qdf in dbs.Querydefs). All the rest of the code is the same.

    I am trying to avoid having to repeat the code - that is my last (and worst) option. But is there any other way to do it? I tried putting the FOR into an if statement but the compiler complained. I am not sure if I can call another procedure - what parameters do I send? At this point I have the database open, the recordsets all positioned and ready for action, etc.

    Any ideas would be appreciated.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    depends on what you are doing...
    the FOR EACH only works with objects (lists, collections)

    FOR i = 1 to x works for variables.

  3. #3
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Not sure I understand your question - the FOR will either be for the list of tables or queries.

  4. #4
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    what are you trying to do?
    then i can answer.

  5. #5
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Read all the objects in an external table searching for use - example, opening all the forms for each table and checking to see where (or if) that table is used, same for query.

  6. #6
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Should have read "external database", sorry!

  7. #7
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    you dont use FOR,
    reading a table/query is a RECORDSET
    then cycle thru the records

    Code:
    set rst = currentdb.openrecordset("select * from table")
    with rst
    while not .eof
      sItem = .fields("myFieldName")
       
       .movenext
    wend
    end with
    set rst = nothing

  8. #8
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    I am not sure I understand. I suppose you could create a custom Class. Place your code in the Class and connect to the Class from an external Access file.

  9. #9
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    This is the beginning part of the code. Where the "For Each tdf.." is, I want to change it optionally to "For Each qdf...."

    I haven't used classes before! Will need to read up about them.

    Code:
        ObjType(1) = "Query"
        ObjType(2) = "Form"
        ObjType(3) = "Report"
        ObjType(4) = "Macro"
        ObjType(5) = "Module"
        
        DoCmd.SetWarnings False
        Set app = New Access.Application
        app.OpenCurrentDatabase Me!DBName
        Set dbs = app.CurrentDb                                     'open other database
        Set rst = CurrentDb.OpenRecordset("WhereUsed_tbl")
        DoCmd.RunSQL "DELETE * FROM WhereUsed_tbl;"
        Me!TblTotal = dbs.TableDefs.Count                           'get count of tables
        Me!TblProc = 0
        
        For Each tdf In dbs.TableDefs
            UpdOpt = False
            If Not IsNull(Me!txtObjName) And Me!txtObjName <> "" Then         'if only one table selected
                TableN = Me!txtObjName
            Else
                TableN = tdf.Name
            End If
            Me!TblProc = Me!TblProc + 1
            
            For x = 1 To 5

  10. #10
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    It seems you wish to type less code by dynamically instantiating a tabledef object or a querydef object. I tried to imagine a couple ways and I am not seeing anything that will work. Bottom line, you will need to define the collection type somewhere.

    Perhaps understanding what it is you wish to automate and what it is you wish to be dynamic will shed some light on the issue. Maybe you want to allow the User to select a collection (table or query) and then execute the appropriate code block.

  11. #11
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    Attach the external table.

  12. #12
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    It's a "where used" button, as in "where is this table (or query) referenced?". It reads thru all the objects in the external database - either using the Tabledefs list or Querydefs - and looks for a reference to that object.

    (Working on some very old databases which have been maintained by the user community up to now, you can imagine the mess they are in)

  13. #13
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    If the objective is to understand the design and have a tool assist you in development, why not consider a third party tool?

    http://www.mztools.com/

    or

    http://www.fmsinc.com/Products/access.asp

  14. #14
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Looks like I am re-inventing the wheel! Sadly those products are not free but in truth I am having fun, lol.

    Thanks for your help.

  15. #15
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    Yeah, there is not anything wrong with customizing things for yourself.

    What I see is you will have one procedure to process each of the different Object Types. They reside in different collections. So I am not seeing a way of using a Strongly typed VBA function to dynamically use a single function. This is how I understand your question. You want to combine them into a single function. I say just write different functions.

    You could include the different function names in an Enum. Declare the enum in a Standard Module. A function in the same module could use the enum to process one of the previously described functions.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Conditional Formatting not "fireing" as expected
    By GraeagleBill in forum Forms
    Replies: 11
    Last Post: 02-14-2016, 06:31 AM
  2. Replies: 18
    Last Post: 01-07-2016, 11:37 PM
  3. Replies: 1
    Last Post: 09-07-2015, 08:00 AM
  4. Conditional Formatting using "Expression is:"
    By TToc2u in forum Programming
    Replies: 1
    Last Post: 07-20-2013, 08:05 PM
  5. Replies: 1
    Last Post: 08-09-2012, 08:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums