Results 1 to 2 of 2

Thread: Adding New Records to Access DB

  1. #1
    Guest

    Unhappy

    I have 2 forms. Both have text boxes which are bound to the same database file.(MDB ext).
    The if I addnew record in VB code in the second form and then go back to the first form the first record has been erased and replaced with the one added.
    It doesn't matter which record the 2nd forms data control is displaying, it is always the first record in the file that is erased.
    Any ideas?
    I am a beginner so any help will do...

  2. #2
    Lively Member
    Join Date
    May 2000
    Location
    Antrim
    Posts
    80
    I don't know what code you are using but the correct code to add a new record should look like this:

    'Declare the variables
    Dim dbsDatabase as Database
    Dim rstRecordset as Recordset

    Set dbsDatabase = OpenDatabase("Full path here")
    Set rstRecordset = dbsDatabase.OpenRecordset("Query string")

    'If you are using a Data Control then you won't need the
    'above you can just use the Recordset property of the
    'Data Control in place of rstRecordset in the following
    'code, ie. Data1.Recordset

    With rstRecordset
    .AddNew
    .Fields(Field number here) = Text Box Name.Text
    'You may have to use conversion functions if you want
    'to update a non-text field ie.
    'CInt(Text Box Name.Text) for an integer field
    'You can repeat the above code to update as many fields
    'as you want. The field number of each field can be
    'determined by opening the table in datasheet view and
    'counting fields from the left (start with 0)
    .Update
    End With

    'If you are using a command button to add your new record
    'then ensure that you invoke the update method before
    'leaving the new record or you will lose the record

    Hope this is helpfull.

    Best regards,

    Rob Brown.

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