gmr4evr1 is right.
In this type of scenario
I Declare a public ChangeFlag as Boolean
I then set ChangeFlag as True before I perform a function that could create an error
I then set this back to False when I know that the risk of error has passed.
This code works:-
'**************************************
Public Changeflag As Boolean
'**************************************
Private Sub cmdAdd_Click()
Dim X As Integer
Dim nextrow As Range
On Error GoTo cmdAdd_Click_Error
Set nextrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
For X = 1 To 4
If Me.Controls("Info" & X).Value = "" Then
MsgBox "You must add Name ,Address ,State and Zipe"
Exit Sub
End If
Next
If WorksheetFunction.CountIf(Sheet1.Range("A:A"), Me.Info1.Value) > 0 Then
MsgBox "This Name already exists"
Exit Sub
End If
For X = 1 To 6
nextrow = Me.Controls("Info" & X).Value
Set nextrow = nextrow.Offset(0, 1)
Next
'clear
For X = 1 To 6
Me.Controls("Info" & X).Value = ""
Next
On Error GoTo 0
Exit Sub
cmdAdd_Click_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdAdd_Click of Form frmCustomer"
End Sub
Private Sub cmdClear_Click()
Unload Me
frmaddress.Show
End Sub
Private Sub cmdClose_Click()
Unload Me
End Sub
Private Sub cmdDelete_Click()
Dim findvalue As Range
Dim cDelete As VbMsgBoxResult
Dim cNum As Integer
'check for values
If Info1.Value = "" Or Info2.Value = "" Then
MsgBox "There is not data to delete"
Exit Sub
End If
'give the user a chance to change their mind
cDelete = MsgBox("Are you sure that you want to delete Select Data", vbYesNo + vbDefaultButton2, "Are you sure????")
If cDelete = vbYes Then
'delete the row
'**************************************
Changeflag = True
Set findvalue = Sheet1.Range("B:B").Find(What:=Info2, LookIn:=xlValues)
findvalue.EntireRow.Delete
End If
'clear the controls
cNum = 6
For X = 1 To cNum
Me.Controls("Info" & X).Value = ""
Next
'refresh the listbox
Unload Me
frmaddress.Show
'**************************************
Changeflag = False
End Sub
Private Sub ListBox1_Click()
'**************************************
If Changeflag = True Then Exit Sub
Dim say As Long, A As Byte, r As Long
Dinfo1 = ListBox1.List(ListBox1.ListIndex, 0)
For A = 1 To 6
Controls("Info" & A) = ListBox1.List(ListBox1.ListIndex, A - 1)
Next
Sheets("Data").Range("A:A").Find(ListBox1.Text).Activate
say = ActiveCell.Row
Sheets("Data").Range("A" & say & ":I" & say).Select
TextBox1 = ListBox1.ListIndex + 1
End Sub
Private Sub TextBox1_Change()
TextBox1 = ListBox1.ListIndex + 1
End Sub
Bookmarks