Results 1 to 7 of 7

Thread: [RESOLVED] How To Copy A Record in Database

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Resolved [RESOLVED] How To Copy A Record in Database

    Hi everyone,

    I used the following format of code to access database, can anyone tell me how to duplicate a record :


    Code:
        Dim CN As ADODB.Connection
        Dim RS As ADODB.Recordset
        
        Set CN = CreateObject("ADODB.Connection")
        Set RS = CreateObject("ADODB.Recordset")
        
        CN.Open "dbclient"
        RS.Open "select * from clientstyle where clientstyleclient='" & strClientCode & "' and clientstylestyle='" & strStyleCode & "'", CN, adOpenDynamic, adLockPessimistic
        
        If RS.EOF Then
    
        End If
    
        RS.Close
        CN.Close
    
        Set RS = Nothing
        Set CN = Nothing
    Thank You In Advance

  2. #2
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Re: How To Copy A Record in Database

    1 way to do that is to create 2 Recordsets.

    Use the first one to access the record you want to duplicate and use the second one to create the record based on the first ones' query

    VB Code:
    1. Dim RS1 as ADODB.RecordSet
    2. Dim RS2 as ADODB.RecordSet
    3.  
    4. Set RS1 = New ADODB.RecordSet
    5. Set RS2 = New ADODB.RecordSet
    6.  
    7. RS1.Open ("SELECT * FROM Table), constring
    8. RS2.Open ("SELECT * FROM SameTable), constring
    9.  
    10. With RS2
    11.     .AddItem
    12.           .Fields("FieldName") = RS1.Fields("FieldName")
    13.     .Update
    14. End With
    15.  
    16. Set RS1 = Nothing
    17. Set RS2 = Nothing

    Something like that.
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Re: How To Copy A Record in Database

    Capp,

    Thank you for your quick respond.

    But this line ".Fields("FieldName") = RS1.Fields("FieldName")" doesn't work, it come back with RUN-TIME ERROR "-214727887".

    And at the same time, is there a way to handle the "FieldName" quicker because my recordset will have more than 50 data fields.

  4. #4
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Re: How To Copy A Record in Database

    Quote Originally Posted by JohnAtom
    Capp,

    Thank you for your quick respond.

    But this line ".Fields("FieldName") = RS1.Fields("FieldName")" doesn't work, it come back with RUN-TIME ERROR "-214727887".

    And at the same time, is there a way to handle the "FieldName" quicker because my recordset will have more than 50 data fields.
    I do apologize if there is a syntax error. I don't have VB installed on this machine.

    One thing you can do to check is let VB use the autocomplete feature. When you type RS1 or RS2 and press the "." button, see what is closest to "Fields"....it might just be "Field" (without the 's').

    If you are wanting to speed it up a bit, you can do something like this:

    VB Code:
    1. With RS1
    2. .Field("Fieldname") = Whatever
    3. .Field("FieldName2") = Whatever2
    4. End With

    I hate to give you this incorrectly but I think you can use the "!" symbol to replace RS as well.
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Re: How To Copy A Record in Database

    Capp,

    This is how I handle the copy and it work fine in my application:

    Code:
        
        Dim g As Integer
        Dim RS2 As ADODB.Recordset
    
        Do While Not RS.EOF
            Set RS2 = RS.Clone
            RS2.AddNew
            For g = 1 To RS.Fields.Count - 1
                RS2(g) = RS(g)
            Next g
            RS2.Update
            Set RS2 = Nothing
            RS.MoveNext
        Loop

  6. #6
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Re: How To Copy A Record in Database

    Quote Originally Posted by JohnAtom
    Capp,

    This is how I handle the copy and it work fine in my application:
    Well I am glad you got it working. I don't know how much help I actually was, but at least you figured it out and thanks for sharing the code. I appreciate it
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: [RESOLVED] How To Copy A Record in Database

    To add a record, you'd say:
    [Highlight=VB]
    Dim strSQL As String

    strSQL = "Insert Into <tablename> (<Field1Name>, <Field2Name>,<etc>) Values(" & RS1.Fields(0).Value & ", '" & RS1.Fields.(1).Value & "', ...)"
    ConnectionToRS2.Execute strSQL
    [Highlight=VB]
    You could build the string in code in a For loop of 0 to 49 if you have 50 fields, to shorten the code a bit (get the field names from RS1.Fields(i).Name and the value from RS1.Fields(i).Value).
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

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