If Tables("采购申请明细_采购申请明细").current.IsNull("下达日期") Then
Tables("采购申请明细_采购申请明细").current("材料编码") = Nothing
Else
Tables("采购申请明细_采购申请明细").current.save
Dim bg As String = "fl" & Format(Tables("采购申请明细_采购申请明细").current("下达日期"),"yyMM") ' 取得编号的8位前缀
If Tables("采购申请明细_采购申请明细").current("材料编码").StartsWith(bg) = False '如果编号的前8位不符
Dim dt2 As DataTable = DataTables("采购申请明细")
Dim max As String
Dim idx As Integer
max = Tables("采购申请明细").DataTable.SQLCompute("Max(材料编码)","下达日期 = '" & Tables("采购申请明细_采购申请明细").current("下达日期") & "' and 材料编码 like '" & bg & "%'") '取得该天的最大编号
If max > "" Then '如果存在最大编号
idx = CInt(max.Substring(bg.Length - 3,3)) + 1 '获得最大编号的后三位顺序号,并加1
Else
idx = 1 '否则顺序号等于1
End If
Tables("采购申请明细_采购申请明细").current("材料编码") = bg & Format(idx,"000")
Tables("采购申请明细_采购申请明细").current.save
End If
End If