Results 1 to 12 of 12

Thread: [RESOLVED] 1st column not in order on rs.AddNew? SQL.

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Resolved [RESOLVED] 1st column not in order on rs.AddNew? SQL.

    I have the following code

    vb Code:
    1. Do Until i = rs_dao.Fields.Count
    2.  
    3. Do Until rs_dao.EOF
    4. If i = 0 Then
    5. rs.AddNew (rs.Fields(0).Name), (rs_dao.Fields(0).Value)
    6. Else
    7. rs.Update (rs.Fields(i).Name), (rs_dao.Fields(i).Value)
    8. End If
    9.  
    10. rs.MoveNext
    11. rs_dao.MoveNext
    12.  
    13. Loop
    14.  
    15. i = i + 1
    16. rs.MoveFirst
    17. rs_dao.MoveFirst
    18.  
    19. Loop

    rs - SQL Server recordset

    rs_dao - Access recordset

    All columns work fine apart from the first one on the rs.AddNew. It's not in order, i.e 1 - 10. But the other rs.Update columns are fine and in order

    The rs database is a blank one so the inital row will have to be added through the AddNew function.

    Im using SQL Server 2000, I noticed that if I toggle the NULLs on in the SQL table design and then run it works fine. I do it again and it doesn't. Then if I toggle the NULLs off again it'll work perfectly for the first time only.

    Any ideas?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: 1st column not in order on rs.AddNew? SQL.

    Moved to Database Development

  3. #3
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: 1st column not in order on rs.AddNew? SQL.

    Try this bit of code. It's based off of the example used in the MSDN.

    Code:
    Count = RS_DAO.RecordCount
    Columns = RS_DAO.Fields.Count
    
    Dim Fields() As Variant
    Dim Values() As Variant
    
    RS_DAO.movefirst
    For i = 1 To Count
        ReDim Fields(Columns - 1)
        ReDim Values(Columns - 1)
        For ii = 0 To Columns
            Fields(ii) = RS_DAO.Fields.Item(ii).Name
            Values(ii) = RS_DAO.Fields.Item(ii).Value
        Next
        rs.AddNew Fields, Values
        rs.Update
        RS_DAO.MoveNext
    Next
    See this for more info:
    http://msdn2.microsoft.com/en-us/lib...36(VS.85).aspx

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Re: 1st column not in order on rs.AddNew? SQL.

    I tried it, the function doesn't seem to input any fields or values into the SQL table and comes back with no errors.
    Last edited by soconnor; Mar 28th, 2008 at 07:18 AM.

  5. #5
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: 1st column not in order on rs.AddNew? SQL.

    Step through it to check and see if the Fields and Values arrays are being filled, and then also if the record is updated. If not, you may have to try it using SQL.

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Re: 1st column not in order on rs.AddNew? SQL.

    There was a problem with the Count and Columns so I just hardcoded rs_dao.recordCount into every instance of Count and did the same for Columns.

    The only problem now is im getting the error 3265 item cannot be found in collection.

    Any ideas?

  7. #7
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: 1st column not in order on rs.AddNew? SQL.

    I'm assuming you hard coded rs_dao.fields.count for columns.
    What line is throwing the error, and WHEN does it happen?

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Re: 1st column not in order on rs.AddNew? SQL.

    Ha ha yeah i did that for columns, I'm not that bad... I think

    The ii seem to be throwing the error, so I changed it to an int j...
    Code:
    For j = 0 To rs_dao.Fields.Count
            Fields(j) = rs.Fields.Item(j).Name
            Values(j) = rs_dao.Fields.Item(j).Value
        Next j
    This works and creates the correct values.

    Now the next line is giving the error number: 9 Subscript out of range.
    Code:
        rs.AddNew Fields, Values
    Last edited by soconnor; Mar 28th, 2008 at 08:35 AM.

  9. #9
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: 1st column not in order on rs.AddNew? SQL.

    When is it giving a subscript out of range? During the last iteration?

    If so, then change the first IF..Then statement to "Do While not RS_dao.EOF" instead. I only used the If..Then for efficiency.

    Also, why did you change the Fields line to just RS? Do the two tables use different column names? If they are not in the same same order, then using RS instead of RS_dao will cause the wrong data to go into the wrong column.

  10. #10

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Re: 1st column not in order on rs.AddNew? SQL.

    The subscipt is out of range when it tries to add a new record.

    Both tables have the same names, no. of columns and column order. I meant to change that rs back.

    I changed it like you said, now back to the 3265 error, item not found!

  11. #11
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: 1st column not in order on rs.AddNew? SQL.

    I see your issue. Change:

    Code:
    For j = 0 To rs_dao.Fields.Count
    to
    Code:
    For j = 0 To rs_dao.Fields.Count - 1
    And I see where I went wrong (I can't test this code without a DB.) Using zero based with a non-zero based number makes it so you are trying to add to a column that does not exist.

    So now, it should be:


    Code:
    Count = RS_DAO.RecordCount
    Columns = RS_DAO.Fields.Count
    
    Dim Fields() As Variant
    Dim Values() As Variant
    
    RS_DAO.movefirst
    For i = 1 To Count
        ReDim Fields(Columns - 1)
        ReDim Values(Columns - 1)
        For ii = 0 To Columns - 1 'error was here 
            Fields(ii) = RS_DAO.Fields.Item(ii).Name
            Values(ii) = RS_DAO.Fields.Item(ii).Value
        Next
        rs.AddNew Fields, Values
        rs.Update
        RS_DAO.MoveNext
    Next

  12. #12

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Thumbs up Re: 1st column not in order on rs.AddNew? SQL.

    Excellent that worked. It inserts all the values correctly!! Thanks man!

    Solution:

    vb Code:
    1. Dim Fields() As Variant
    2. Dim Values() As Variant
    3.  
    4. rs_dao.MoveFirst
    5.  
    6. Do Until rs_dao.EOF 'i = rs_dao.recordCount
    7.     ReDim Fields(rs_dao.Fields.Count - 1)
    8.     ReDim Values(rs_dao.Fields.Count - 1)
    9.     For j = 0 To rs_dao.Fields.Count - 1
    10.         Fields(j) = rs.Fields.Item(j).Name
    11.         Values(j) = rs_dao.Fields.Item(j).Value
    12.     Next j
    13.     rs.AddNew Fields, Values
    14.     rs.Update
    15.     rs_dao.MoveNext
    16. Loop

    Thanks again!

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