PDA

Click to See Complete Forum and Search --> : Save to DB


ad1044
Sep 14th, 2000, 03:26 PM
I have a save button on my form and the code behind it is Data1.Updaterecord
The user enters in all there infor to textboxes and clicks this button so the info in stored to the access 97 DB. But most of the time when this is clicked it populates all the textboxes with a previous record....Is there someway to stop this or a better way to save to the database..PLEASE HELP

parksie
Sep 14th, 2000, 03:48 PM
You can do it by not using the datacontrol. Although this is for DAO, since it is faster for Access:

Dim db as Database ' Database object
Set db = OpenDatabase("me.mdb") ' Open the Access database

dim rst as recordset ' Recordset, used to retrieve records
set rst = db.TableDefs("MyTable").OpenRecordSet ' Open all records for table "MyTable"
rst.addnew ' Begin adding a new record
rst!FieldA = Text1.Text ' Set text value
rst!OtherField = CInt(Val(Text2.Text)) ' Set integer value
rst.update ' Update record in database
rst.close ' Close recordset
db.close ' Close database

Or something similar.

[Edited by parksie on 09-14-2000 at 05:03 PM]

ad1044
Sep 14th, 2000, 03:58 PM
Do you think you could put some comments in that code..I dont really understand what all of it does exactly..thanks

ad1044
Sep 14th, 2000, 07:35 PM
How do you know what fields go to which fields?
rst!FieldA = Text1.Text
Like the fieldA.....
IF you have a large table Im not sure how to determine which field corresponds to FieldA

Syl
Sep 15th, 2000, 02:11 AM
sorry, but even if U have a VERY large table, you have to know names of the fields. Or U should know the order of the information.

If U know names, U use
something like Parksie wrote:

rst!FieldA = Text1.Text

(fieldA is only an example of a name)

If U know the order, U can use

rst.Fields(5) = Text1.Text

but in all cases U have to know what exactly is in the table and which textbox corresponds to which information.

By the way, It's vary useful to name each textbox and each field in a table. Then U can write

rst!EmployeeName = txtEmployeeName.Text

instead of

rst!FieldA = Text1.Text