Solved

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

Posted on 2016-09-07
2
38 Views
Last Modified: 2016-09-07
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?
0
Comment
Question by:SteveL13
2 Comments
 
LVL 34

Accepted Solution

by:
PatHartman earned 500 total points
Comment Utility
Why not just use an append query that selects the current record and appends it to the history table?  Depending on your desired goal, you can copy the "before" image or the "after" image depending on which event you use to run the query.  If you run it in the BeforeUpdate event, you will be copying the "before" image.  If you run it in the AfterUpdate event, you will be copying the "after" image.  I have a slight preference for copying the "after" image since the logic is simpler.  If you copy the "before" image, you have to be prepared to delete the history record if the BeforeUpdate event fails.
0
 
LVL 18
Comment Utility
>"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

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

10 Experts available now in Live!

Get 1:1 Help Now