Results 1 to 15 of 15
  1. #1
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529

    Getting a compile error

    I am trying to run a select case scenario to update a recordset based upon the value in a particular field.



    Code:
    Private Sub Command4_Enter()
    Dim mydb As DAO.Database
    Dim myset As DAO.Recordset
    Dim mycategory As String
    
    Set mydb = CurrentDb
    Set myset = mydb.OpenRecordset("Table1")
    Do Until myset.EOF
    
    Select Case [myset]![Mileage]
        Case 0 To 24000
        [mycategory] = "24,000"
        Case 24001 To 36000
        [mycategory] = "36,000"
        Case 36001 To 50000
        [mycategory] = "50,000"
        Case 50001 To 60000
        [mycategory] = "60,000"
        Case Else
        [mycategory] = "Greater than 60K"
        End Select
    myset!Category = mycategory
    myset.MoveNext
    Loop
    End Sub
    It errors out on the line highlighted. Can't seem to see what I've done wrong here.

    Thanks

  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,521
    Don't you need

    myset.Edit

    first?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Unrelated, but I don't think you want mycategory bracketed inside the Select/Case, do you? Or myset for that matter.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725
    I agree with Paul and would also suggest

    myset.edit 'to set up for update
    ....
    myset.update ' to do/commit the update

  5. #5
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    Thanks guys. Got that included. Still bombing out on the myset.category=mycategory line. Somehow I need to tell Access to update the Category Field in Table1. Where am I going wrong on that.

    Code:
    Private Sub Command4_Enter()
    Dim mydb As DAO.Database
    Dim myset As DAO.Recordset
    Dim mycategory As String
    
    Set mydb = CurrentDb
    Set myset = mydb.OpenRecordset("Table1")
    Do Until myset.EOF
    
    myset.Edit
    Select Case myset!Mileage
        Case 0 To 24000
        [mycategory] = "24,000"
        Case 24001 To 36000
        [mycategory] = "36,000"
        Case 36001 To 50000
        [mycategory] = "50,000"
        Case 50001 To 60000
        [mycategory] = "60,000"
        Case Else
        [mycategory] = "Greater than 60K"
        End Select
    myset.Update
    myset!Category = mycategory
    
    myset.MoveNext
    Loop
    End Sub

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You'd want that line before the Update line. It's

    rs.Edit
    rs!Field = whatever
    rs.Update
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    Thanks, just figured that out but now it is only updating the first record and not any of the others. When I F8 through the code, it doesn't error out but it doesn't update the other records. Am posting db for examination.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    It updated all records for me (though it took me a minute to realize you had the code in the enter event rather than the click event as I'd assumed).
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    Thanks Paul. That was a mistake on my part. I thought I had put it in the On Click event. Fat Fingers do me in regularly. I will change it and see what happens. Thanks for looking at it and pointing that out to me.

  10. #10
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    Runs fine. Thanks Paul and Orange.

  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,521
    Happy to help.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    A new wrinkle to the issue has arisen. Added an additional condition to the code and now the compile error is: Compile Error Loop without Do

    Code:
    Private Sub Command4_Click()
    Dim mydb As DAO.Database
    Dim myset As DAO.Recordset
    Dim mycategory As String
    
    Set mydb = CurrentDb
    Set myset = mydb.OpenRecordset("Table1")
    Do Until myset.EOF
    
    myset.Edit
    If myset!Coverage = "CCPLAT" Then
    Select Case myset!Mileage
    Case 0 To 24000
    [mycategory] = "24,000"
    Case 24001 To 36000
    [mycategory] = "36,000"
    Case 36001 To 50000
    [mycategory] = "50,000"
    Case 50001 To 60000
    [mycategory] = "60,000"
    Case Else
    [mycategory] = "N/A"
    End Select
    Else
    If myset!Coverage = "CCDIAM" Then
    Select Case myset!Mileage
    Case 0 To 50000
    [mycategory] = "50,000"
    Case 50001 To 75000
    [mycategory] = "75,000"
    Case 75001 To 100000
    [mycategory] = "100,000"
    Case 100001 To 125000
    [mycategory] = "125,000"
    Case 125001 To 150000
    [mycategory] = "150,000"
    Case Else
    [mycategory] = "N/A"
    End Select
    End If
    myset("Category").Value = mycategory
    myset.Update
    
    myset.MoveNext
    Loop
    End Sub
    Not sure if this requires two do loops to work and if so, would it also require two edits and updates. I'm not sure of the correct syntax when using multiple loops.

  13. #13
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725
    Alan,
    Just a guess, but I think it isn't finding the Enf If, and outputs the Do--Loop error.

    I'd try bringing your Else and If together

    ElseIf myset!Coverage = "CCDIAM" Then
    again just a quick look and guess.

  14. #14
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    These are actually fairly easy to figure out if you copy/paste out to Word or something and start deleting stuff that's okay. Yours gets down to this:

    If myset!Coverage = "CCPLAT" Then
    Else
    If myset!Coverage = "CCDIAM" Then
    End If


    You never end the first If/Then.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  15. #15
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    Seems to be working now. Thank you both for your help.

    Alan

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

Similar Threads

  1. Why do I get a compile error?
    By shabbaranks in forum Programming
    Replies: 24
    Last Post: 10-18-2011, 07:51 PM
  2. Compile Error Syntax Error
    By Mohamed in forum Access
    Replies: 3
    Last Post: 10-06-2011, 10:12 AM
  3. Replies: 6
    Last Post: 09-28-2011, 09:20 PM
  4. new compile error!
    By darklite in forum Access
    Replies: 6
    Last Post: 09-02-2010, 05:13 PM
  5. compile error
    By darklite in forum Access
    Replies: 6
    Last Post: 08-31-2010, 04:27 PM

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