I am still in the process of making a function that makes an accessible class object that you can use anywhere in the code to load in data
so far I have
what this will do is build an access module that plants in all the field names of the table as variables (I use a modified function from Allen's FieldTypeName to get the datatype for vba)Code:Public Sub AddAModule(TableName As String) Dim VBAEditor As VBIDE.VBE Dim VBProj As VBIDE.VBProject Dim VBComp As VBIDE.VBComponent Dim CodeMod As VBIDE.CodeModule Set VBAEditor = Application.VBE Set VBProj = VBAEditor.ActiveVBProject For Each VBComp In VBProj.VBComponents If VBComp.Name = "mod_" & TableName Then VBProj.VBComponents.Remove VBComp End If Next VBComp Set VBComp = VBProj.VBComponents.Add(vbext_ct_StdModule) VBComp.Name = "mod_" & TableName Set CodeMod = VBComp.CodeModule Dim i As Integer Dim db As DAO.Database Dim aTable As DAO.TableDef Dim aField As DAO.Field Set db = CurrentDb() i = 1 With CodeMod .DeleteLines i, .CountOfLines For Each aTable In db.TableDefs If TableName = aTable.Name Then For Each aField In aTable.Fields 'create object here with name of field Debug.Print "adding var" & aField.Name .InsertLines i, "Public var" & Replace(aField.Name, " ", "") & " as " & FieldTypeName(aField) i = i + 1 Next aField End If Next aTable '.InsertLines i, "private Sub EditField()" 'i = i + 1 End With End Sub
What I want is to make a "Class" object. I want to be able to put the same code into a class an not a module - so I don't quite know how to do that yet.
Ideally I want to make an object where I can pull (if "tblSchools" is my table name parameter)
school.name
or school.phonenumber
this way I can edit a records details in code however it would load at startup and when you add a field the code would auto add the field to the class object.
if anyone knows the code to make it a class rather than adding a module please let me know!
Update: vbext_ct_ClassModule gives me a class module - working!![]()