Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add data to table from form

Status
Not open for further replies.

JennieFernandes

Programmer
Aug 4, 2005
10
US
Hi All, I have a form that uses some visual basic coding to generate data. I am not able to figure out how to add this data from the form to a table.

The data in the form that I would like to transfer to table is in text boxes. As I see it, some data need to be added to one table and some data need to be added to another.

These tables are existing in the database and already contain some information.

Please provide me with some suggestions.

Jennie.
 
Using DAO

dim rs1 as recordset
dim rs2 as recordset


set rs1 = currentdb.openrecordset("Table1")
set rs2 = currentdb.openrecordset("Table2")

with rs1
.addnew
.Fields("FieldName1") = TextBox1 'or !FieldName1 = TextBox1 if you have spaces in the fieldnames then ![Field Name 1] etc.
.Fields("FieldName2") = CInt(TextBox2)
.Update
end with

etc.
etc.

rs1.close
rs2.close

typed NOT tested


Hope this helps
 
Thanks a lot. This worked as a gem. I am jumping as this worked.

Tell me one thing. I am using a command button to add data from form to table. Is there a way in which, I can restrict the same data to be added twice to the table if the user accidently presses the command button twice.
 
Presumably you will have a unique field on which you could perhaps do a DLookup.

Private Sub Button_Click(etc.)
If Not IsNull(Dlookup(.....)) Then
Save data
Else
MsgBox("You've already saved this record")
End If
End Sub

Alternatively, you could keep a String/Integer/Whatever variable in the Declarations section of the form - the String or whatever would hold the unique field's data as follows:

Private Sub Button_Click(etc.)
If UniqueFieldVar = UniqueFieldTextBox Then
MsgBox ("You've already saved this record")
Exit sub
Else
UniqueFieldVar = UniqueFieldTextBox
Save data
End If
End Sub


Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top