Try this:
Private Sub CmdUpdate_Click()
Const PW As String = "000" 'using const for fixed values
Dim allTables(), lo As ListObject, rw As ListRow, tName
allTables = Array("T_Food", "T_Beverage", "T_Other")
If Me.comDepartment.Text = "" Or Me.txtItemName.Text = "" Or _
Me.txtPrice.Text = "" Or Me.comUnit.Text = "" Then
MsgBox "Please enter data ", vbCritical, "Inventory program "
Exit Sub
End If
old_name = Me.TxtEditItem.Value
new_name = Me.txtItemName.Text
If new_name <> old_name Then
'see if the new name already exists in any of the tables
If Not AnyTableRowMatch(allTables, "Item Name", new_name) Is Nothing Then
MsgBox "The new name '" & new_name & "' already exists ", _
vbCritical, "Inventory Program "
Exit Sub
End If
End If
'select the correct table
Select Case Me.comDepartment.Value
Case "Food": tName = "T_Food"
Case "Beverage": tName = "T_Beverage"
Case "Other": tName = "T_Other"
End Select
'find the record being edited
Set rw = TableRowMatch(Data.ListObjects(tName), "Item Name", old_name)
If Not rw Is Nothing Then
Data.Unprotect PW
'update the row
rw.DataBodyRange.Value = Array(Me.txtCode.Text, new_name, _
Me.txtCode.Text, Me.comUnit.Text, _
Me.txtPrice.Text, Me.TxtSalePrice.Text, _
Me.comDepartment.Text)
Data.Protect Password:=PW
Else
MsgBox "Edited row not found!"
End If
End Sub
'find any matching row in tables with names in `arrTableNames`
Function AnyTableRowMatch(arrTableNames, colName, colValue) As ListRow
Dim el, rw As ListRow
For Each el In arrTableNames
Set rw = TableRowMatch(Data.ListObjects(el), colName, colValue)
If Not rw Is Nothing Then
Set AnyTableRowMatch = rw
Exit Function
End If
Next el
End Function
'Find any matching row for value `colValue` in listobject `1o`, column `colName`
' Returns Nothing if no match
Function TableRowMatch(lo As ListObject, colName, colValue) As ListRow
Dim el, lo As ListObject, loCol As ListColumn, lr As ListRow, m
Set loCol = lo.ListColumns(colName)
m = Application.Match(colValue, loCol.DataBodyRange, 0)
If Not IsError(m) Then Set TableRowMatch = lo.ListRows(m)
End Function