Link to home
Start Free TrialLog in
Avatar of SteveL13
SteveL13Flag for United States of America

asked on

Is there a way to loop through all controls on a form and have their values written to a table?

I have many fields on a form and want to write their values to a table that the form is not bound to.  Normally I would do something like this...

    Dim rs As DAO.Recordset
    Dim strSQL As String

    strSQL = "SELECT * FROM tblItemCalculations WHERE EstPartID = " & Me.txtEstPartID
    
    Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)

    If rs.RecordCount > 0 Then
        MsgBox "This item calculation already exists and will be edited if you have made changes."
        
            rs.Edit
            rs!EstID = Me.txtEstID
            'rs!EstPartID = Me.txtEstPartID
            rs!Template = Me.chkbxTemplate
            rs!PrinterID = Me.cboPrinterID
            rs!PaperType = Me.cboPaperType
            rs.Update
            
            MsgBox "This item calculation has been saved in the calculations table."
        
        Else
        
    MsgBox "This appears to be a new item calculation which does not already exist in the program.  A new record is being created."

            rs.AddNew
            rs!EstID = Me.txtEstID
            rs!EstPartID = Me.txtEstPartID
            rs!Template = Me.chkbxTemplate
            rs!PrinterID = Me.cboPrinterID
            rs!PaperType = Me.cboPaperType
            rs.Update
            
            MsgBox "The new record has been created in the Item Calculations table."
        End If

Open in new window


But there are so may fields I am wondering if there is a way via VBA code to loop through all of the fields and have their values written to the unbound table.

Is there a way to do this?
ASKER CERTIFIED SOLUTION
Avatar of PatHartman
PatHartman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of crystal (strive4peace) - Microsoft MVP, Access
crystal (strive4peace) - Microsoft MVP, Access

>"many fields on a form"
terminology correction: forms do not have fields; they have controls. Control can have a field as the Control Source if it is bound. For unbound forms, where the data goes needs to be specified. To take better advantage of Access, use bound forms and if you don't want the whole table from a database to be pulled, limit the recordsource to just one record

Forms do not contain data; they add, display, and change date in underlying table(s).

As for looping through a/(an unbound) form, you can store fieldname in the Tag property of each control and read that in code to loop through controls so code is not hard-wired and, therefore, more flexible:
dim ctl as control _
   ,sTag as string

for each ctl in me.controls 'can also use me.detail.controls to limit to Detail section
   sTag = ctl.Tag
   'do something
next ctl
set ctl=nothing

Open in new window