input form fields into table ms access vba

Posted on 2007-11-28
Last Modified: 2013-11-28
I have an Access form with several data stored in Labels.  I would like to input the data contained within these labels into a target table called "target_table" within the same acccess dbase.  All the data will go into the same column, but in different rows.  I was wondering how to go about doing this in VBA, to kick off a function when the user selects a button on the form?

Question by:vishalarya
  • 4
  • 2

Accepted Solution

Chi Is Current earned 500 total points
ID: 20371717
Vishalarya ~

"Labels", do you mean text boxes on the form?


1) Setting the record source for the form to the destination table.
2) Use "text boxes" instead of labels on your form.
3) Set the control source for each text box on the form to the corresponding field on your table.


Author Comment

ID: 20371728
No labels are fine since it's simple text data.  Actually can't I just use DoCmd.RunSQL query and reference my caption for the field.  As I'm not too familiar with SQL, does anyone know the code.  For example:

Form name: vtest
caption name: label47

label47 contains text = "abcd"

I want to insert label47's caption into table "target_table".  What would be the INSERT INTO statement for doing so?


Expert Comment

by:Chi Is Current
ID: 20371741
If you are using a form that is not bound or cannot be bound, you can do this by placing the data in a recordset that updates to your destination table.

dds110 provides this example on solution:

Sub YourButton_Click()
Dim db As Database
Dim rst As Recordset

Set db = CurrentDb
Set rst = db.OpenRecordset("Table_To_Be_Updated")

With rst
  .Fields("Table_Field_Name1") = Me.ControlName
  .Fields("Table_Field_Name2") = Me.ControlName2
End With


Set rst = Nothing
Set db = Nothing

End Sub

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.


Expert Comment

by:Chi Is Current
ID: 20371754
For this to work you will need to store the data in 'text box' controls on your form.  I don't think you'll be able to reference 'labels'.

Author Comment

ID: 20371768
no jacob you are wrong.

THe following code works:

DoCmd.runSQL ("UPDATE target_table set col2 = '" & label47.caption & "' where col1 = 'one'")

no need for text boxes.

Expert Comment

by:Chi Is Current
ID: 20371787

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The first two articles in this short series — Using a Criteria Form to Filter Records ( and Building a Custom Filter ( — discuss in some detail how a form can be…
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
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…
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…

939 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

2 Experts available now in Live!

Get 1:1 Help Now