2017-11-16 3 views
1

Access 2010을 사용하고 있습니다. 이라는 데이터 시트 양식이 있습니다. [프로젝트 이름]과 [우선 순위]의 두 필드가있는 프로젝트가 있습니다. 레코드 중 하나의 우선 순위 번호를 업데이트하고 다른 모든 우선 순위 번호를 자동으로 업데이트 할 수 있기를 바랍니다. 예를 들어 Project Red는 우선 순위 1입니다. Project Orange는 우선 순위 2이고 Project Blue는 우선 순위 3입니다. Blue를 1로 업데이트하면 Red가 2로 업데이트되고 Orange는 3으로 업데이트됩니다. 가능합니까? 가능업데이트 될 때 모든 레코드 번호 재 지정

Projects Form

답변

0

.

사용 우선 순위와 텍스트 상자의 대한 AfterUpdate 이벤트 :

Private Sub Priority_AfterUpdate() 

    Dim rst    As DAO.Recordset 
    Dim lngId   As Long 
    Dim lngPriorityNew As Long 
    Dim lngPriorityFix As Long 

    ' Save record. 
    Me.Dirty = False 

    ' Prepare form. 
    DoCmd.Hourglass True 
    Me.Repaint 
    Me.Painting = False 

    ' Current Id and priority. 
    lngId = Me!Id.Value 
    lngPriorityFix = Nz(Me!Priority.Value, 0) 
    If lngPriorityFix <= 0 Then 
     lngPriorityFix = 1 
     Me!Priority.Value = lngPriorityFix 
     Me.Dirty = False 
    End If 

    ' Rebuild priority list. 
    Set rst = Me.RecordsetClone 
    rst.MoveFirst 
    While rst.EOF = False 
     If rst!Id.Value <> lngId Then 
      lngPriorityNew = lngPriorityNew + 1 
      If lngPriorityNew = lngPriorityFix Then 
       ' Move this record to next lower priority. 
       lngPriorityNew = lngPriorityNew + 1 
      End If 
      If Nz(rst!Priority.Value, 0) = lngPriorityNew Then 
       ' Priority hasn't changed for this record. 
      Else 
       ' Assign new priority. 
       rst.Edit 
        rst!Priority.Value = lngPriorityNew 
       rst.Update 
      End If 
     End If 
     rst.MoveNext 
    Wend 

    ' Reorder form and relocate record. 
    Me.Requery 
    Set rst = Me.RecordsetClone 
    rst.FindFirst "Id = " & lngId & "" 
    Me.Bookmark = rst.Bookmark 

    ' Present form. 
    Me.Painting = True 
    DoCmd.Hourglass False 

    Set rst = Nothing 

End Sub