[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register 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
?
93 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 39

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 22
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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

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…
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

650 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