📜  DataUsfManager - 任何代码示例

📅  最后修改于: 2022-03-11 14:59:24.541000             🧑  作者: Mango

代码示例1
VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "DataUSFManager"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Private mIndex As Long
Private mTable As ListObject
Private mUsf As UserForm
Private mMap

Public Sub Init(Rng As Range, Usf As UserForm, Map)
  Set mTable = Rng.ListObject
  Set mUsf = Usf
  mMap = Map
End Sub

Property Get Index() As Long
  Index = mIndex
End Property

Function GoToPrevious() As Long
  If mIndex > 1 Then
    mIndex = mIndex - 1
    updateUserform
  Else
    GoToPrevious = 1
  End If
End Function

Function GoToNext() As Long
  If mIndex < mTable.ListRows.Count Then
    mIndex = mIndex + 1
    updateUserform
  Else
    GoToNext = 1
  End If
End Function

Function GotoRecord(Index As Long) As Long
  If mTable.ListRows.Count >= Index And mTable.ListRows.Count > 0 And Index >= 1 Then
    mIndex = Index
    updateUserform
  Else
    GotoRecord = 1
  End If
End Function

Sub GotoNew()
  mIndex = 0
End Sub

Function Delete() As Long
  mTable.ListRows(mIndex).Delete
  If mTable.ListRows.Count > 0 Then
    If mIndex > mTable.ListRows.Count Then mIndex = mTable.ListRows.Count
    updateUserform
  Else
    Delete = 1
  End If
End Function

Sub updateUserform()
  Dim r As ListRow
  Dim Counter As Long
  Dim ControlName As String
  Dim ColumnName As String
  
  Set r = mTable.ListRows(mIndex)
  For Counter = 1 To UBound(mMap)
    mUsf.Controls(mMap(Counter, 1)).Value = r.Range(mTable.ListColumns(mMap(Counter, 2)).Index).Value
  Next
End Sub

Sub UpdateTable()
  Dim r As ListRow
  Dim Counter As Long
  Dim ControlName As String
  Dim ColumnName As String
  
  If mIndex = 0 Then
    mTable.ListRows.Add
    mIndex = mTable.ListRows.Count
  End If
  Set r = mTable.ListRows(mIndex)
  For Counter = 1 To UBound(mMap)
    r.Range(mTable.ListColumns(mMap(Counter, 2)).Index).Value = mUsf.Controls(mMap(Counter, 1)).Value
  Next
End Sub

Function GotoFirst()
  GotoFirst = GotoRecord(1)
End Function

Function GoToLast()
  GoToLast = GotoRecord(mTable.ListRows.Count)
End Function

Function GotoIndexFromCell(Cell As Range)
  Dim Index As Long
  
  Index = Cell.Row - mTable.Range.Row
  GotoIndexFromCell = GotoRecord(Index)
End Function