SteveL13
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...
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?
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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:
Open in new window