Hello
My userform displays the records when loaded or while navigating the records.Problem is if user clicks on the Save button the form enters the record again. I mean that record is already there in the sheet but as it displays the records from sheet. it also saves them again if the Save button is clicked How can I avoid to enter a duplicate record.The code is bellow:
Option Explicit
'Flag used to Enable or Disable UserForm Events
Private bGblInhibitUserForm1Events As Boolean
Private CurrentRow As Long
Private Sub cmdAdd_Click()
Dim iRow As Long
Sheets("sheet1").Activate
iRow = Application.WorksheetFunction.CountA(Range("A:A")) + 1
Cells(iRow, 1) = TextBox1.Value
Cells(iRow, 2) = TextBox2.Value
Cells(iRow, 3) = TextBox3.Value
TextBox1.Value = ""
TextBox2.Value = ""
TextBox3.Value = ""
End Sub
Private Sub cmdClose_Click()
Unload Me
End Sub
Private Sub cmdNew_Click()
TextBox1.Value = ""
TextBox2.Value = ""
TextBox3.Value = ""
ImgData.Visible = False
LabelStatus.Caption = ""
End Sub
Private Sub SpinButton1_Change()
If SpinButton1.Value = 0 Then SpinButton1.Value = 10
ListBox1.ListIndex = ListBox1.ListCount - SpinButton1.Value
Label11.Caption = SpinButton1.Value
End Sub
Private Sub UserForm_Initialize()
SpinButton1.Max = ListBox1.ListCount
SpinButton1.Min = 0
SpinButton1.Value = 10
CurrentRow = 2
Call Update_Form
LabelStatus.Caption = ""
End Sub
Private Sub cmdPre_Click()
Dim iMinimumAllowedRowNumber As Long
iMinimumAllowedRowNumber = 2
CurrentRow = CurrentRow - 1
If CurrentRow <= iMinimumAllowedRowNumber Then
CurrentRow = iMinimumAllowedRowNumber
MsgBox "This is the first Record."
End If
Call Update_Form
ImgData.Visible = True
End Sub
Private Sub cmdNext_Click()
Dim iMaximumAllowedRowNumber As Long
iMaximumAllowedRowNumber = Range("list").Rows.Count
ImgData.Visible = True
CurrentRow = CurrentRow + 1
If CurrentRow >= iMaximumAllowedRowNumber Then
CurrentRow = iMaximumAllowedRowNumber
MsgBox "This is the Last Available Record."
End If
Call Update_Form
End Sub
Private Sub cmdFirst_Click() 'FIRST RECORD BUTTON
'Calculate the First Row Number
CurrentRow = Range("list").Row + 1
ImgData.Visible = True
Call Update_Form
LabelStatus.Caption = ""
End Sub
Private Sub cmdLast_Click() 'LAST RECORD BUTTON
'Calculate the Last Row Number
CurrentRow = Range("list").Rows.Count
ImgData.Visible = True
Call Update_Form
LabelStatus.Caption = "This is the Last Available Record."
End Sub
Private Sub ListBox1_Click()
Dim iListIndex As Long
'Do nothing if 'UserForm Events' are disabled
If bGblInhibitUserForm1Events = False Then
iListIndex = ListBox1.ListIndex
ImgData.Visible = True
If iListIndex <> -1 Then
CurrentRow = iListIndex + 1
Call Update_Form
End If
End If
End Sub
Sub Update_Form()
Dim fPath As String
Dim sClass As String
Dim sFullName As String
Dim sRoll As String
sFullName = Worksheets("Sheet1").Cells(CurrentRow, "A").Value
sClass = Worksheets("Sheet1").Cells(CurrentRow, "B").Value
sRoll = Worksheets("Sheet1").Cells(CurrentRow, "C").Value
If Len(sFullName) = 0 Then
MsgBox "Data Integrity Error - No Name in Column 'A' of Row " & CurrentRow
Debug.Assert False
Exit Sub
End If
TextBox1.Value = sFullName
TextBox2.Value = sClass
TextBox3.Value = sRoll
'Calculate the ListIndex (inhibit 'UserForm' Events when the 'ListIndex' Changes)
bGblInhibitUserForm1Events = True
UserForm1.ListBox1.ListIndex = CurrentRow - 1
bGblInhibitUserForm1Events = False
fPath = ThisWorkbook.Path & "\"
If Dir(fPath & sFullName & ".jpg") <> "" Then
ImgData.Picture = LoadPicture(fPath & sFullName & ".jpg")
Else
ImgData.Picture = LoadPicture(fPath & "Capture.jpg")
End If
End Sub
Best Regards
Imran Bhatti
Bookmarks