|
本帖最后由 mmx1122 于 2018-9-14 15:05 编辑
'我这个可以实现。可能有点复杂,但是很简单。原理是:每次先去数据库查询最后一个单据编号,如果有就是加1,如果没有,就用新的日期& 0001
我的格式:HL2017080501
Dim Y As String
Dim m As String
Dim D As String
Dim s As String
Sub 新增核料单()
Y = Year(Date)
m = Format(Month(Date), "00")
D = Format(Day(Date), "00")
s = Y & m & D
On Error GoTo er
' cn.Open "provider=Microsoft.jet.OLEDB.4.0;data source=" & App.Path & "\Data.mdb" & ";User ID=; Jet OLEDB:database Password = 6318"
With Form1
sq1 = "SELECT TOP 1 单据编号 FROM 核料表 ORDER BY 单据编号 DESC"
rs1.Open sq1, cn, adOpenKeyset, adLockOptimistic
If Mid(rs1.Fields("单据编号"), 3, 8) = s Then
.单据编号 = "HL" & Format(Val(Right(rs1.Fields("单据编号").Value, 10)) + 1, "0000000000")
Else
.单据编号 = "HL" & s & "01"
End If
rs1.Close
cn.Close
Set rs1 = Nothing
Set cn = Nothing
Exit Sub
er:
rs1.Close
cn.Close
Set rs1 = Nothing
Set cn = Nothing
.单据编号 = "HL" & s & "01"
End With
End Sub
|
|