When deleting an entry from my DB using a vba userform I received an error:
"An error has occurred the error number 91 object variable or with block variable not set"
With my limited knowledge of vba I'm struggling to understand what the issue is, please could somebody advise?
My data writes to the history log (sheet3) but then the error message is displayed.
thanks
"An error has occurred the error number 91 object variable or with block variable not set"
With my limited knowledge of vba I'm struggling to understand what the issue is, please could somebody advise?
My data writes to the history log (sheet3) but then the error message is displayed.
thanks
Code:
Private Sub cmdDelete_Click()
'declare the variables
Dim findvalue As Range 'done
Dim cDelete As VbMsgBoxResult 'done
Dim cNum As Integer 'done
Dim DataSH As Worksheet 'done
Dim Addme As Range 'done
Dim HistorySH As Worksheet 'done
Dim x As Integer
'error statement
On Error GoTo errHandler:
'hold in memory and stop screen flicker
Application.ScreenUpdating = False
Set DataSH = Sheet1
Set HistorySH = Sheet3
Set Addme = HistorySH.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0)
'check for values
If txtID.Value = "" Or txtLocation.Value = "" Then
MsgBox "There Is No 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 This Pallet", vbYesNo + vbDefaultButton2, "Are you sure?")
If cDelete = vbYes Then
'find the row
Set findvalue = DataSH.Range("B:B").Find(What:=Me.txtID.Value, _
LookIn:=xlValues, LookAt:=xlWhole)
'update the values in the HistoryLog
With HistorySH
'add the unique reference ID then all other values
Addme.Offset(0, -1) = txtID.Value
Addme.Offset(0, 0).Value = "Deleted"
Addme.Offset(0, 1).Value = Me.txtSize
Addme.Offset(0, 2).Value = Me.txtLocation
Addme.Offset(0, 3).Value = Me.cboType
Addme.Offset(0, 4).Value = Me.txtDescription
Addme.Offset(0, 5).Value = Me.txtContact
Addme.Offset(0, 6).Value = Me.txtDate.Value
Addme.Offset(0, 7).Value = Format(Now(), "hh:mm:")
Addme.Offset(0, 8 .Value = UserName()
End With
'delete the entire row
findvalue.EntireRow.Delete
End If
'clear the controls
cNum = 7
For x = 1 To cNum
Me.Controls("txt" & x).Value = ""
Next
'unprotect all sheets for the advanced filter
'Unprotect_All
'filter the data
DataSH.Range("B8").CurrentRegion.AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=Sheet6.Range("$L$1:$L$2"), CopyToRange:=Sheet6.Range("$N$1:$w$1"), _
Unique:=False
'if no data exists then clear the rowsource
If Sheet6.Range("N2").Value = "" Then
lstData.RowSource = ""
Else
'add the filtered data to the rowsource
lstData.RowSource = Sheet6.Range("outdata").Address(external:=True)
End If
'sort the database by "Location"
DataSH.Select
With DataSH
.Range("DataTable").Sort Key1:=Range("E9"), Order1:=xlAscending, Header:=xlGuess
End With
'sort the HistoryLog by "ID"
HistorySH.Select
With HistorySH
.Range("DataTable2").Sort Key1:=Range("B9"), Order1:=xlAscending, Header:=xlGuess
End With
'Protect all sheets
'Protect_All
'return to sheet
Sheet2.Select
'error block
On Error GoTo 0
Exit Sub
errHandler:
'Protect all sheets if error occurs
'Protect_All
'show error information in a messagebox
MsgBox "An Error has Occurred " & vbCrLf & "The error number is: " & _
Err.Number & vbCrLf & Err.Description & vbCrLf & "Please notify the administrator"
End Sub
Last edited by a moderator: