Results 1 to 4 of 4

Thread: ADODB Recordset Update

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140

    ADODB Recordset Update

    Sorry to cross-post, but after getting no traffic in the other forum, I thought I might get better response here.

    I have the following bit of code.

    VB Code:
    1. Dim myConn as New ADODB.Connection
    2.   Dim myRS as New ADODB.Recordset
    3.  
    4.   myConn.Open "myDSN"
    5.  
    6.   myRS.Open "SELECT * FROM myTable WHERE 0=1", myConn, adOpenDynamic, adLockBatchOptimistic
    7.  
    8.   myRS.AddNew
    9.   myRS.Fields("Field1").Value = strFoo
    10.   myRS.Fields("Field2").Value = strBar
    11.   myRS.UpdateBatch
    12.  
    13.   myRS.Close
    14.  
    15.   myRS.Open "SELECT IndexField FROM myTable WHERE " & _
    16.             "Field1 = '" & strFoo & "' AND " & _
    17.             "Field2 = '" & strBar & "'", _
    18.             myConn, adOpenDynamic, adLockBatchOptimistic
    19.   intIndex = myRS.Fields("IndexField").Value
    20.   myRS.Close

    Is there a safe way (that will work with MS Access, MS SQL Server, and Oracle) that I can get the autonumber field (IndexField) without closing and reopening the recordset? Are there any other places that I can make this code safer, use more accepted conventions?
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  2. #2
    Lively Member iataman's Avatar
    Join Date
    Jun 2000
    Location
    Turkey
    Posts
    71
    Basically I belive that recordset are for readinda data, not updating. So I can recomemend you to use code below

    VB Code:
    1. Dim strConnStr,strQry as string
    2. Dim conConn as ADODB.Connection
    3. Dim rsRecSet as ADODB.recordset
    4. Dim intIndex as Long
    5.  
    6. ' strConnStr = "Provider..." Changes according to your DB
    7. conConn.open strConnStr
    8.  
    9. strQry = "INSERT INTO myTable (Field1, Field2) VALUES ('"&strFoo&"' , '"&strBar&"')"
    10. conConn.Execute (strQry)
    11.  
    12. strQry="SELECT indexField FROM myTable WHERE field1='"&strFoo&"' AND field2 = '"&strBar&"'"
    13. Set rsRecSet = conConn.execute(strQry)
    14. intIndex=rsRecSet.Fields(0)
    15.  
    16. Set rsRecSet=Nothing
    17. Set conConn=Nothing

    I personally use this code. This is much more safer I belive because you always have the exact query in hand and sure about what will exactly happen.

    Hope it helps.

    Regards.
    While YourAge<=40
    Live(YourLife)
    Wend

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    My main reason for doing the update via the Recordset and not an SQL insert statement is MS SQL Server. The DB will not process a string literal over 4000 characters in an SQL statement. ADO works around this.

    Now maybe someone can explain this deal with VB's early and late binding. It is my understanding that if you don't declare certain objects with the "New" operator then you will have to "Set" them after the declare.

    As to nitpicky things... I don't like the use of "rsRecSet.Fields(0)". If I change the SQL statement, I now have to change the index. And even though .Value is the default property, call it.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    Anyone? I was hoping to avoid closing and querying.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

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