Results 1 to 9 of 9
  1. #1
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496

    Create objects from array or other

    I'm looking to make objects using a loop in VBA (and I know this might not be possible)



    What I want to be able to do is put in a table name and have it make a bunch of objects (eventually I will make a class out of this whole thing with it's own methods)

    I want it to get every name of the table that I type in - basically I will on launch create a class on load with all the fields of the table as public objects

    whenever the database needs to update the field I can call it and use a method with ID as a parameter to update the row.

    I want a standard function that builds the objects as I want to be able to create/remove fields when required without it affecting the code

    I would have caveats in for the data type too (so if it is bool, int, lng etc the method behaves differently)

    my code below... any ideas much appreciated


    Code:
    Public Function objTableSQL(TableName As String)
    
    
    If IsNothing(TableName) = True Then
    
    
    Exit Function
    End If
    
    
    Dim tableExists As Boolean
    Dim strTable As String
    strTable = TableName
    
    
    tableExists = False
    Debug.Print
    
    
    Dim aDatabase As DAO.Database
    Dim aTable As DAO.TableDef
    Dim aField As DAO.Field
    Set aDatabase = CurrentDb()
    
    
    For Each aTable In aDatabase.TableDefs
    
    
        If aTable.Name = strTable Then
            tableExists = True
            For Each aField In aTable.Fields
                
                'create object here with name of field
                
                
            Next aField
        End If
    
    
    Next aTable
    
    
    If tableExists = False Then
        Exit Function
    End If
    
    
    
    
    End Function

  2. #2
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    ...anyone?

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,740
    Can you give an example of what you are visualizing?

    You can get all the fields in the table with the code you've shown.
    I'm not following the "object" and "class".

    Seems to me recordset(s) , filter(s) etc would apply..

  4. #4
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by orange View Post
    Can you give an example of what you are visualizing?

    You can get all the fields in the table with the code you've shown.
    I'm not following the "object" and "class".

    Seems to me recordset(s) , filter(s) etc would apply..
    I want to treat tables as objects

    so "tableName"

    tableName.FieldMethod(0)

    TableName.FirstName(123) = "Bob" <= this would change the name field

    TableName.LastName(123) = "Smith" <= this would change the surname

    that way anywhere in the database code I could call

    TableName

    And it would give me an intelligent of list of it's fields with each method for each

    You could also use a with statement

    with TableName

    .FirstName(x) = me.Text_FirstName.text
    .LastName(x) = "me.Text_LastName.text

    End Wtih

  5. #5
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    If when loading the database, the front end goes and makes objects automatically of each table and corresponding fields then there is no requirement to make individual object classes for each table

    so if you add a new table -> the database makes a new object next load (and every load)

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,740
    Hmmm,

    Interesting.
    I was thinking more along the standard object model
    currentdb.TableDefs("Orders").Fields(0).Name etc.

  7. #7
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by orange View Post
    Hmmm,

    Interesting.
    I was thinking more along the standard object model
    currentdb.TableDefs("Orders").Fields(0).Name etc.
    It's something that could be used to list and access all the known tables' fields in the database in order to make them objects - but to build objects from the tables without knowing what they are and letting access handle the object creation would be nice.

    I don't want to go as far as build a string (text document) or xml and then import the text as vba script - that seems messy especially since it means parsing, importing and activating.

  8. #8
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    I'd like to see June7's opinion on this too :P

    just to hear out any ideas...

  9. #9
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,740
    Yes, June and xipooo(Steve) may have ideas (others also).
    Steve does more with classes than most on the forum.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Append one array to another
    By Ruegen in forum Programming
    Replies: 7
    Last Post: 11-10-2014, 08:02 PM
  2. Need help with array
    By programmingbeginner in forum Programming
    Replies: 14
    Last Post: 08-28-2014, 01:30 PM
  3. Replies: 6
    Last Post: 05-31-2013, 07:46 PM
  4. Replies: 8
    Last Post: 12-19-2012, 06:56 AM
  5. Replies: 2
    Last Post: 09-29-2011, 12:50 PM

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