tags:

views:

36

answers:

1

I have two fields, one is just the number of the other one, e.g. Field 1 = "12AB" and Field 2 is "12". I'm trying to make Field 2 auto-update but I'm not sure how.

I'm trying to get:

ClassName ClassYear
12AB      12
13BU      13
15BE      15

But instead the whole fields update to the number in the last one:

ClassName ClassYear
12AB      15
13BU      15
15BE      15

The code I currently have is

  Dim db As DAO.Database
  Dim rst As DAO.Recordset
  Set db = CurrentDb
  Set rst = db.OpenRecordset("MasterTable")

  With rst
  Do Until .EOF
    .Edit
    If (Left(ClassName.Text, 1) = "1") Then
      !ClassYear = Left(ClassName.Text, 2)
    Else
      !ClassYear = Left(ClassName.Text, 1)
    End If
    .Update
    .MoveNext
  Loop

  .Close
  End With

Help is much appreciated! Thank you :)

+3  A: 

How about something on the lines of:

 sSQL = "UPDATE MasterTable SET ClassYear=Left(ClassName,2) " _
      & "WHERE Left(ClassName,1)='1'"
 CurrentDB.Execute sSQL, dbFailOnError

 sSQL = "UPDATE MasterTable SET ClassYear=Left(ClassName,1) " _
      & "WHERE Left(ClassName,1)<>'1'"
 CurrentDB.Execute sSQL, dbFailOnError
Remou
I can't use an SQL query as the form already has the database open, Access wont let me update it via any other way as the database is already in use.
Tim
Not if ClassName refers to a form control, which it apparently does.
Tomalak
But !ClassYear is in the database and that's what I'm trying to update.
Tim
Are you sure? I have just tested with a table bound to a form and it updates without any problem.
Remou
Ah, I had something extra in there that was breaking it. This works! Thanks a lot Remou :)
Tim
You are welcome.
Remou
There is no reason you should be prevented from running a SQL UPDATE that edits tables you have open in forms as long as there are no unsaved edits in any of those forms. You can save data in the current form with Me.Dirty = False or in any other form with Forms!FormName.Dirty = False. In a subform it would be Me!MySubform.Form.Dirty = False.
David-W-Fenton