1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

visual basic 6.0 ADO

Discussion in 'Software Development' started by The G-man, Jun 7, 2002.

Thread Status:
Not open for further replies.
  1. The G-man

    The G-man Thread Starter

    Joined:
    Jun 3, 2001
    Messages:
    140
    Hi everyone,

    I have a relatively simple question. I have a ms access database app written in vb6. I have a function in the app that allows the user to edit a record. The way i'm currently working this is by using the adodata.recordset.addnew method which writes an new record to the database and then i'm going through using SQL and deleting the old record. I was wondering if there is, and i'm sure there is, a way to bring up a record and edit it and save it as opposed to writing a whole new record and deleting the old one.

    Thanks to all who read!
     
  2. TimCottee

    TimCottee

    Joined:
    Oct 10, 2001
    Messages:
    186
    G-Man:

    As you are using ADO then you have the ability to Update a record:

    Code:
    With ADOData.Recordset
      .Fields("MyField1").Value = "My New Value"
      .Fields("MyField2").Value = 400
      .Update
    End With
    
    This updates the current record with the new field values. Now this appears very simple (and it can be) however you do generally need to consider a few things. Firstly you need to ensure that the recordset (table) that you are updating has a primary key. If you don't then you will either get spurious results or you will get an error message that may say something like "insufficient base information for updating".

    I would strongly recommend that you avoid using the ADO Data control and create your recordset(s) in code as this gives you much more flexibility in terms of setting the cursortype/locktype of the recordset which governs whether it is updatable etc.

    Another method that you can employ is to use a standard SQL statement to perform the update (though here again a primary key is strongly recommended to avoid odd results) and do something like this:

    Code:
    ADOData.ActiveConnection.Execute "Update MyTable Set MyField1 = 'My New Value', MyField2 = 400 Where MyKeyField = 123456"
    
     
  3. The G-man

    The G-man Thread Starter

    Joined:
    Jun 3, 2001
    Messages:
    140
    Worked just fine! Thanks Tim!
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/82588

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice