Results 1 to 3 of 3

Thread: Open Access Recordset

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Posts
    76

    Open Access Recordset

    I know this is really basic, but it's driving me crazy...

    I am trying to open a recordset, and based on the data in
    one field, save a value in another field in the same
    table. I need to do this for each record in the table.

    This is the code I have written:

    Dim DB As Database
    Dim RS As Recordset
    Dim Col As Field
    Dim Col2 As Field
    Set DB = CurrentDb()
    Set RS = DB.OpenRecordset("tblPTOBenefits")
    Set Col = DB.OpenRecordset("tblPTOBenefits").Fields
    ("LOS")
    Set Col2 = DB.OpenRecordset("tblPTOBenefits").Fields
    ("YearlyPTOBenefit")

    For Each Col2 In RS
    If Col > 0 Then
    Col2 = "$2000"
    Else
    Col2 = "$0"
    Exit For
    End If
    Next

    It is giving me a Data mismatch error and highlighting the
    line "Set RS = DB.OpenRecordset("tblPTOBenefits")".

    Can someone help me here?

    Thanks, Lee

  2. #2
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    ADO replaced DAO. Access 2000 and beyond doesn't seem to like a record set. I think this is what you want:

    VB Code:
    1. Dim DB As Database
    2. Dim RS As Variant
    3. Dim i as Long
    4.  
    5. Set DB = CurrentDb
    6. Set RS = DB.OpenRecordset("tblPTOBenefits")
    7.  
    8. For i = 1 To RS.RecordCount
    9.     With RS
    10.         .Edit
    11.         If .Fields("LOS") > 0 Then
    12.             .Fields("YearlyPTOBenefit") = "$2000"
    13.         Else
    14.             .Fields("YearlyPTOBenefit") = "$0"
    15.         End If
    16.         .Update
    17.         .MoveNext
    18.     End With
    19. Next

  3. #3
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    You need to set the references to DAO to use Dao (and remove ADO if req.

    You should be able to run Workhorses solution, or if you don't have too many records (like <10000) you could just run an Update Sql statement.

    Update [tblPTOBenefits] ( [YearlyPTOBenefit] )
    Select iif(nz([tblPTOBenefits].[LOS],0)>0,2000,0) as CalcResult from [tblPTOBenefits]

    There are many ways to do what you need, use which ever you feel more comfortable with.


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

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