Learn how to a build a cloud-first strategyRegister Now

x
?
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
Medium Priority
?
97 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 40

Accepted Solution

by:
PatHartman earned 2000 total points
ID: 41788035
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 23
ID: 41788240
>"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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Suggested Courses

810 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