看不懂你的代码,很乱,这么简单的要求,并不应该写这么复杂的代码。
红色的部分帮你修正了一下,可以避免你说的问题。
Dim trv As WinForm.TreeView = e.form.Controls("TreeView1")
If trv.SelectedNode.Text <> "全部" Then
Dim t As Integer = trv.SelectedNode.Text.LastIndexOf(" ")
Dim s As String = trv.SelectedNode.Text.SubString(0,t)
Dim i As Integer = s.Length - 2
Dim s2,s3,s4 As String
Dim dr,dr1,dr2 As DataRow
Dim n,n1 As Integer
Dim dt As DataTable = Tables("表A").DataTable
dr = dt.AddNew
s2 = "SubString(分类编号, 1, " & t & ") = '" & s & "' And len(分类编号) = " & t+2
dr1 = dt.Find(s2)
If dt.DataRows.count =1 OrElse dr1 Is Nothing Then 'dt.DataRows.count =1 是因为有dr = dt.AddNew,增加了一行
dr("分类编号") = s & "01"
Else
For Each dr2 In dt.Select(s2)
n = right(dr2("分类编号"),2)
n1= iif(n1<n,n,n1)
Next
For n = 1 To n1
If dt.Find("分类编号 = '" & s & Functions.Execute("数值转换2",n) & "'") Is Nothing Then
dr("分类编号") = s & Functions.Execute("数值转换2",n)
End If
Next
If dr.Isnull("分类编号") Then
dr("分类编号") = s & Functions.Execute("数值转换2",n1+1)
End If
End If
End If
With Tables("表A")
.Position = .Rows.Count - 1
End With