附件为狐爸爸的
高级自动编号示例,我现在应该的SQL数据库,表名为定单主表,'systemdata 为外部数据源
不知道还有错误吗?
If e.DataRow.IsNull("产品") Then
Return
ElseIf e.DataRow.IsNull("编号") = False AndAlso e.DataRow("编号").StartsWith(e.DataRow("产品")) Then
Return
End If
Dim cmd1 As New SQLCommand
Dim cmd2 As New SQLCommand
Dim cmd3 As New SQLCommand
Dim Key As Integer
Dim T1 As String
cmd1.Con nectionN ame = "systemdata" 'systemdata 为外部数据源
cmd2.Con nectionN ame = "systemdata" 'systemdata 为外部数据源
cmd3.Con nectionN ame = "systemdata" 'systemdata 为外部数据源
cmd1.commandText = "Select [编号] From [定单主表] Where [产品] = '" & e.DataRow("产品") & "'"
Key = cmd1.ExecuteScalar()
T1 = Format(Date.today,"yyMM")
cmd2.commandText = "Update [编号] Set [编号] = " & (Key + 1) & " Where [编号] = " & Key & " And [产品] = '" &e.DataRow("产品") & "'"
cmd3.commandText = "Select Count(*) From [定单主表] Where [产品] = '" & e.DataRow("产品") & "'"
If cmd3.ExecuteScalar = 0 Then
cmd3.commandtext = "Insert Into 编号 (产品, 编号) Values('" & e.DataRow("产品") & "',1)"
cmd3.ExecuteNonQuery
End If
Do
If cmd2.ExecuteNonQuery() > 0 Then
Exit Do
Else
Key = cmd1.ExecuteScalar()
cmd2.commandText = "Update [编号] Set [编号] = " & (Key + 1) & " Where [编号] = " & Key & " And [产品] = '" &e.DataRow("产品") & "'"
End If
Loop
e.DataRow("编号") = e.DataRow("产品") & "-" & T1 & Format(Key,"0000")
[此贴子已经被作者于2009-9-5 9:25:19编辑过]
用于编号的外部数据源,其编号列是数值型的,你的SQL语句中,关于编号的部分,有的用了单引号,等于作为字符了。
cmd2.commandText = "Update [编号] Set [编号] =
" & (Key + 1) & " Where [编号] =
" & Key & " And [产品] = '" &e.DataRow("产品") & "'"