Results 1 to 9 of 9

Thread: Insert Into

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Resolved Insert Into

    i use this to grab the fields from my database but how do you add a new row using the INSERT INTO.
    VB Code:
    1. Set rec = New Recordset
    2.  
    3. rec.Open "SELECT * FROM Tdetails", con, adOpenKeyset, adLockOptimistic
    4.  
    5.  Do While Not rec.EOF
    6.   Set itm = lv.ListItems.Add(, , rec.Fields(0))
    7.    itm.SubItems(1) = rec.Fields(1)
    8.    itm.SubItems(2) = rec.Fields(2)
    9.    itm.SubItems(3) = rec.Fields(3)
    10.    itm.SubItems(4) = rec.Fields(4)
    11.    itm.SubItems(5) = rec.Fields(5)
    12.   rec.MoveNext
    13.  Loop
    14.  
    15. rec.Close
    16. Set rec = Nothing

    thanks.
    Last edited by user name; May 19th, 2005 at 03:43 AM.

  2. #2
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872

    Re: Insert Into

    Why not use "AddNew"??
    VB Code:
    1. Set rec = New Recordset
    2.  
    3. rec.Open "SELECT Top 1 * FROM Tdetails", con, adOpendynamic, adLockPessimistic
    4.  
    5. with rec
    6.  .addnew
    7.  .Fields(0).value = "value 0"
    8.  .Fields(1).value = "value 1"
    9.  .Fields(2).value = "value 2"
    10.  .Fields(3).value = "value 3"
    11.  .update
    12. end with
    13.  
    14. rec.Close
    15. Set rec = Nothing

    replace "value x" by the value you want to insert in the database

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: Insert Into

    that was perfect robbedaya.

    another question if you dont mind.
    i see you put adOpendynamic, adLockPessimistic.
    is adOpenKeyset, adLockOptimistic correct for just showing the data and yours for updating it ? and why have you put "Top 1" in the statement ?

    thanks for your help.

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Insert Into

    A simpler method is to use the connection object to execute the query like this:

    con.Execute "INSERT INTO Tdetails VALUES( value1, value2, value3 ....)"

    This will directly execute the query on ur database.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: Insert Into

    thank you both.

  6. #6
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872

    Re: Insert Into

    Quote Originally Posted by user name
    i see you put adOpendynamic, adLockPessimistic.
    is adOpenKeyset, adLockOptimistic correct for just showing the data and yours for updating it ?
    Don't exactly know when to use adopenkeyset en adlock optimistic

    i use adOpenStatic and adLockReadOnly to read data from the database, because nothing is editable, so nothing can be changed by accident

    i use adOpenDynamic and adLockPessimistic to write data to the database, the data is editable and is the record locked properly so the date kan not be altered by a third party while you're editing.

    Quote Originally Posted by user name
    and why have you put "Top 1" in the statement ?
    When inserting you don't need to load the whole recordset (if your table contains thousands of records it would take a lot of time) so i took the top 1 the only one record is loaded.

    Normally i do it like this

    "SELECT * FROM table Where ID_column=0" (if the id_column never contains 0, so no records are loaded when inserting.)

  7. #7
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Insert Into

    U r doing it OK username.
    adLockOptmistic and adOpenStatic. This is sufficient in most of the situations. u may consider adOpenKeySet also if u want to see records changed by other users.

    Quote Originally Posted by robbedaya
    i use adOpenDynamic and adLockPessimistic to write data to the database, the data is editable and is the record locked properly so the date kan not be altered by a third party while you're editing.
    we should avoid dynamic recordsets and pessimistic locking where ever possible.
    I think u are wrong here. Pessimistic locking means locking the whole table when updating it. Optimistic locking locks only the current row.

    Quote Originally Posted by robbedaya
    When inserting you don't need to load the whole recordset (if your table contains thousands of records it would take a lot of time) so i took the top 1 the only one record is loaded.

    Normally i do it like this

    "SELECT * FROM table Where ID_column=0" (if the id_column never contains 0, so no records are loaded when inserting.)
    I too prefer this method. But a bettter method to open empty recordsets is to avoid any field name in where clause like this:

    SELECT * FROM TableName WHERE 0=1

    This will always return an empty recordset.

    Pradeep

  8. #8
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872

    Re: Insert Into

    is it possible to edit a record that isn't opened "adDynamic"?

  9. #9
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Insert Into

    Quote Originally Posted by robbedaya
    is it possible to edit a record that isn't opened "adDynamic"?
    Yes dynamic recordsets are editiable.
    Rerecordsets are readonly only when they are opened as adLockReadOnly
    .
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

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