Results 1 to 11 of 11
  1. #1
    Amerigo is offline Novice
    Windows 98/ME Access 97
    Join Date
    Mar 2011
    Posts
    24

    Updating table's column via VBA

    Haven't programmed in access in a while, so i am a bit rusty


    I am trying to assign a vlue to the column in a table


    (based on content of other criteria), but i am stuck in the first step

    how do i say that i want 16th column to become "2" for all records ?

    .Fields(16).Value = 2




    Code:
     
     
    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    Dim strSQL As String
    
     
    Set db = CurrentDb()
    strSQL = "SELECT *  FROM qryResult qryResult"
    Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)
    rst.MoveFirst
    
    Debug.Print Z
    With rst
         Do While .EOF = False
             .Edit
             .Fields(16).Value = 2
             .MoveNext
        Loop
    End With
    rst.Close

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    What's your question? You've doubled up "qryResult" in the SQL string. It would be much more efficient to execute an UPDATE query than to loop a recordset. Personally, I don't like to use the field number; the name is a lot easier to understand:

    !FieldName = 2
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Amerigo is offline Novice
    Windows 98/ME Access 97
    Join Date
    Mar 2011
    Posts
    24
    Quote Originally Posted by pbaldy View Post
    What's your question? You've doubled up "qryResult" in the SQL string. It would be much more efficient to execute an UPDATE query than to loop a recordset. Personally, I don't like to use the field number; the name is a lot easier to understand:

    !FieldName = 2

    >>You've doubled up "qryResult" in the SQL string.
    not sure what u mean

    I can't use 'Update' query since my end goal is to populate Fields(16) based on other criteria. I've done this few years ago, but can't move past first step.
    I think the problem is in this line

    .Fields(16).Value = 2

    i am not getting errors, but nothing is updating

  4. #4
    Amerigo is offline Novice
    Windows 98/ME Access 97
    Join Date
    Mar 2011
    Posts
    24
    technically i need something like this

    BookMyNewFieldX_111d_561t_221t_222r_771r_772r_773yy_661f_41f_781

    this new field i am trying to update depends on 'Book'
    I'd like MyNewField to count start counting each time book changes

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    This is what I mean:

    strSQL = "SELECT * FROM qryResult qryResult"

    An UPDATE query can have criteria. Have you tried Fields(15) or the field name? I suspect the Fields property is zero based.

    I would also include the update line:

    .Edit
    !FieldName = 2
    .Update
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    Amerigo is offline Novice
    Windows 98/ME Access 97
    Join Date
    Mar 2011
    Posts
    24
    Quote Originally Posted by pbaldy View Post
    This is what I mean:

    strSQL = "SELECT * FROM qryResult qryResult"

    An UPDATE query can have criteria. Have you tried Fields(15) or the field name? I suspect the Fields property is zero based.

    I would also include the update line:

    .Edit
    !FieldName = 2
    .Update

    I see - u were correct, now i am getting last row populated
    So now i am trying to make the last row depend on column 'book'

    Code:
     
    rst.MoveFirst
    Debug.Print Z
    With rst
    Value = "value"
         Do While .EOF = False
             .Edit
                
                If .Fields(0).Value = Value Then
                    .Fields(17).Value = 1
                     Value = .Fields(0).Value
                Else
                    .Fields(17).Value = Order
                    Order = Order + 1
                    Value = .Fields(0).Value
                End If
             
             
             .Update
             .MoveNext
        Loop
    End With
    rst.Close
    So if Fields(0) is the the same for 2 or more rows, i'd like increment it, and to go back to 1 for eac new Fields(0)

    (what i wrote doesn't work)

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    I suspect this:

    Value = "value"

    should be

    Value = .Fields(0).Value

    And I'd note that having a variable with the same name as a property will be confusing. I think your logic is reversed. If the current Fields(0) is the same as the last, you want to increment your variable and use it. In the else, reset it to 1.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    Amerigo is offline Novice
    Windows 98/ME Access 97
    Join Date
    Mar 2011
    Posts
    24
    hey - so I got what i wanted with below --
    but it run;s for almost 10 min ! need it to be much faster. Any way ?

    Code:
     
    With rst
    Value = "value"
    Order = 1
         Do While .EOF = False
             .Edit
                
                If .Fields(0).Value <> Value Then
                    .Fields(4).Value = 1
                    Order = 1                       'order back to 1
                    Debug.Print .Fields(4).Value
                     Value = .Fields(0).Value
                     Spread = .Fields(2).Value
                Else
                    
                        If .Fields(2).Value <> Spread Then Order = Order + 1
                       .Fields(4).Value = Order
                        Debug.Print .Fields(4).Value
                        
                    Value = .Fields(0).Value
                    Spread = .Fields(2).Value
                End If
             
             
             .Update
             .MoveNext
        Loop
    End With

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    How many records? For 100, that's terrible. For 100,000, it's probably to be expected.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    Amerigo is offline Novice
    Windows 98/ME Access 97
    Join Date
    Mar 2011
    Posts
    24
    Quote Originally Posted by pbaldy View Post
    How many records? For 100, that's terrible. For 100,000, it's probably to be expected.
    around 70 thousand now; but the populion is growing daily by 2000 - 3000 records

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    Are you redoing the whole table every time, or just the new records? Just doing the new records would obviously be preferable if feasible. I don't know that there's a lot you can do to speed that up. I assume the appropriate fields are indexed on the table, but I suspect the bottleneck is the recordset having to examine each record. You can make it run at night when nobody is there, or something along those lines.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 03-14-2011, 10:04 AM
  2. Replies: 7
    Last Post: 04-27-2010, 02:47 PM
  3. Replies: 1
    Last Post: 04-15-2010, 02:07 AM
  4. FE & BE Updating
    By mastromb in forum Access
    Replies: 5
    Last Post: 02-12-2010, 11:55 AM
  5. inserting values in column based another column
    By wasim_sono in forum Database Design
    Replies: 1
    Last Post: 06-27-2006, 05:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums