Results 1 to 5 of 5

Thread: Save to DB

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2000
    Posts
    20
    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

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You can do it by not using the datacontrol. Although this is for DAO, since it is faster for Access:
    Code:
    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]
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2000
    Posts
    20
    Do you think you could put some comments in that code..I dont really understand what all of it does exactly..thanks

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Aug 2000
    Posts
    20
    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

  5. #5
    Junior Member
    Join Date
    Sep 2000
    Location
    Poland
    Posts
    26
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width