'==================================================================
'- USERFORM TO UPDATE EXCEL WORKSHEET DATABASE
'- basic info. for table navigation only - will need improvement
'- by Brian Baulsom
'- ** NEXT MESSAGE** added button to FIND a record **
'-----------------------------------------------------------------
'- This example has 3 textboxes named TextBox1, ...etc
'- + 3 buttons - [Next],[Previous],[New]
'- Uses variable 'CurrentRow' to refer to worksheet table
'==================================================================
Dim DataSheet As Worksheet
Dim CurrentRow As Long
Dim LastRow As Long
'==========================================================
'- BUTTON SUBROUTINE : ADD NEW RECORD BOTTOM OF TABLE
'==========================================================
Private Sub NewRecordButton_Click()
UpdateTable
LastRow = LastRow + 1
CurrentRow = LastRow
UpdateForm
End Sub
'==========================================================
'- BUTTON SUBROUTINE : GO TO NEXT RECORD
'==========================================================
Private Sub NextRecordButton_Click()
UpdateTable
If CurrentRow < LastRow Then
CurrentRow = CurrentRow + 1
UpdateForm
End If
End Sub
'==========================================================
'- BUTTON SUBROUTINE : GO TO PREVIOUS RECORD
'==========================================================
Private Sub PreviousRecordButton_Click()
UpdateTable
If CurrentRow > 2 Then
CurrentRow = CurrentRow - 1
UpdateForm
End If
End Sub
'==========================================================
'- INITIALISE FORM WITH FIRST RECORD
'==========================================================
Private Sub UserForm_Initialize()
Set DataSheet = Worksheets("Database")
CurrentRow = 2 ' first row with data
LastRow = DataSheet.Range("A65536").End(xlUp).Row
UpdateForm
End Sub
'==========================================================
'- SUBROUTINE : WORKSHEET TABLE DATA TO FORM
'==========================================================
Private Sub UpdateForm()
TextBox1.Value = DataSheet.Cells(CurrentRow, 1).Value
TextBox2.Value = DataSheet.Cells(CurrentRow, 2).Value
TextBox3.Value = DataSheet.Cells(CurrentRow, 3).Value
End Sub
'==========================================================
'- SUBROUTINE : FORM DATA TO WORKSHEET TABLE
'- changes all cells even though data may be the same
'==========================================================
Private Sub UpdateTable()
DataSheet.Cells(CurrentRow, 1).Value = TextBox1.Value
DataSheet.Cells(CurrentRow, 2).Value = TextBox2.Value
DataSheet.Cells(CurrentRow, 3).Value = TextBox3.Value
End Sub