add data from text box to record with button

S

Steve

I am having a little trouble figuring out the code to put in to just simply
add information I have entered in 3 or 4 text box fields to a table I have in
access.

Basically it's like this, I have 4 text boxes (date, forman, notes and
quantity) and when I click a button I want it to add those 4 entries into a
table.

But I can't for the life of me figure out the code. can anybody help?
 
P

PJFry

Steve,

Is there a reason you are not using a bound control for the data entry?

I am assuming that the table you want to write this data to already has the
four fields.

PJ
 
S

Steve

Basically I have 2 tables already set up. One has multiple columns, but
between the two tables they hvae a relation for the item #. Now what i'm
basically trying to do is add text box's to the form that will allow me to
add to the 2nd table I have stored.

So basically it's set like this. I have item # 10 in view. Showing me all of
the information from the primary table. item #... cost.. discription..
quantity.. so on and so forth. Now below this I had added the 4 text boxes to
enter information that would relate to the 2nd table. I would like to just
set it so i could click the update button so it would add the information I
had added into the text fields, and add it to the 2nd table I have stored.

I'm guessing it is related to the .addNew function but I can't figure out
the coding.

The table is named "Work Done" and the fields are Item no, Date, Foreman,
Notes and Qty.

I have these text boxes in a form that has the first table's information
listed in bound text boxes already.
 
P

PJFry

What you need is a subform for those four values.

Create a form that only contains the elements that you are trying to update
on your second table. Make sure you include the field the relationship is
based on.

In your toolbox on the first form, use the Add Subform/Subreport button and
select where you want your subform to go. The wizard will ask how you want
to relate the two forms. Select the field that you use to relate them and
you should be good to go.

Give that a try and let me know if you run into any problems.

PJ
 
S

Steve

Actually I've already done that on the form :p.

I have my form with all the information listed from table1 and a subform
below that with table2. Thing is, instead of entering information in the
subform, I would like to have the information able to be entered and when I
click the button i created, it save that information by adding it to table2.
table1 is really just for show, table 2 has information thta will be added.
Basically i'm looking for the code i could put to save the information
entered in particular text boxes and save it to table2.
 
P

PJFry

Fair enough.

It sounds like the text boxes are on the main form, the values they control
are on the subform. Assuming that is correct...

First, you want your text boxes to be unbound controls, if they are not
already. Then on the OnClick event for your button, put the following code:

Forms![Main Form Name]![Subform Name]![Name of first value on subform] =
Me.[Name of first value on main form]

Just replace the names here with the actual form names.

From the description I still think you should be entering the values
directly into the subform, but this should do the trick. Let me know if it
does not work.

PJ
 
S

Steve

That actually worked perfectly JP. The only thing is that it replaces the
first entry on my subform instead of adding it as "new". I'm going to search
a little more to prevent this but i'm sure you have it on the tip of your
tongue.

PJFry said:
Fair enough.

It sounds like the text boxes are on the main form, the values they control
are on the subform. Assuming that is correct...

First, you want your text boxes to be unbound controls, if they are not
already. Then on the OnClick event for your button, put the following code:

Forms![Main Form Name]![Subform Name]![Name of first value on subform] =
Me.[Name of first value on main form]

Just replace the names here with the actual form names.

From the description I still think you should be entering the values
directly into the subform, but this should do the trick. Let me know if it
does not work.

PJ

Steve said:
Actually I've already done that on the form :p.

I have my form with all the information listed from table1 and a subform
below that with table2. Thing is, instead of entering information in the
subform, I would like to have the information able to be entered and when I
click the button i created, it save that information by adding it to table2.
table1 is really just for show, table 2 has information thta will be added.
Basically i'm looking for the code i could put to save the information
entered in particular text boxes and save it to table2.
 
S

Steve

I figured out how to add it as new.
Basically like this..

'add record to sub form
Me![Work Done Subform].SetFocus
DoCmd.GoToRecord , , acNewRec

'assign a value to a field in that sub form
Forms![Item List]![Work Done Subform]![Date] = Me.[Date]
Forms![Item List]![Work Done Subform]![Foreman] = Me.[Foreman]
Forms![Item List]![Work Done Subform]![Notes] = Me.[Notes]
Forms![Item List]![Work Done Subform]![Qty] = Me.[Qty]

'save the record
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

' Requery the sub form
Me![Work Done Subform].Requery

Me.[Foreman].Value = Null
Me.[Notes].Value = Null
Me.[Qty].Value = Null

Only thing now that I want to add is to have an if then else statement to
prevent anything from being added if no value is entered (mainly for adding a
blank entry).
 
P

PJFry

Very good. You can set the if statement to check the values and you are all
set. You may wish to have a message box indicating that there is a missing
value.
 
S

Steve

Done and done. Did it all before you even got back to me on that last one.

Again thanks a ton, everything stores and updates perfect now.

Will it always add the item to the begining of the subform or is there a way
to add it to the end of that list? Guess that'll be my last question before
I retire :p
 
P

PJFry

I am not sure what you mean by the beginning of the subform. If it is a
continuous form it would always add records after the last one. Is that what
you mean?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top