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
67 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 35

Accepted Solution

by:
PatHartman earned 500 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 19
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Insert Into syntax error 10 28
default combobox value 12 18
90 days before current date 12 30
How to get query to return one row for each unique part # 2 12
In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

809 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