Office中国论坛/Access中国论坛

标题: [求助]0711001这样的自动编号如何做? [打印本页]

作者: sunny-xie    时间: 2007-11-29 14:04
标题: [求助]0711001这样的自动编号如何做?



效果:在库中窗体  窗体1 的 单号处,自动显示编号YYMM001(YY为年份,MM为月份,001为单号流水号)每按添加一次,流水号递增,但是年份和月份也要随着系统日期变更,也就是07年12月1日时,0712001,08年01月01日时,0801001依次类推。

请达人指教,THKS!
作者: Victor_Duane    时间: 2007-11-29 23:30
下面这段代码,就是如何在窗体中使用的方法
Private Sub Command4_Click()
Dim rs As New ADODB.Recordset
rs.Open "型体编号表", CurrentProject.Connection, 1, 3
rs.AddNew
rs("型体编号") = Me.型体编号
rs.Update
rs.Close
Set rs = Nothing
Me.型体编号 = Null
Me.型体编号 = uf_AutoNum("型体编号表", "型体编号")

End Sub

Private Sub Form_Load()
Me.型体编号.Enabled = True

Me.型体编号 = uf_AutoNum("型体编号表", "型体编号")

Me.型体编号.Enabled = False
Me.文本1.SetFocus
End Sub


'使用方法:Me.型体编号 = uf_AutoNum("型体编号表", "型体编号")
Function uf_AutoNum(strTable As String, strfldName As String)
'strtable 表名
'strfldname 字段名
Dim rst As New ADODB.Recordset
rst.Open strTable, CurrentProject.Connection, 1, 3
Dim strYY As String
Dim strMaxNum As String
Dim strMidYY As String
Dim strMidNum As String
strYY = Format(Date, "YY")
Debug.Print rst.RecordCount
If rst.RecordCount > 0 Then
    strMaxNum = DMax(strfldName, strTable)
    Debug.Print strMaxNum
    strMidYY = Mid(strMaxNum, 3, 2)
    strMidNum = Format(Val(Right(strMaxNum, 3)) + 1, "000")

        If strMidYY = strYY Then
            uf_AutoNum = "GL" & strYY & strMidNum
        Else
            uf_AutoNum = "GL" & strYY & "001"
        End If
Else
    uf_AutoNum = "GL" & strYY & "001"
End If
rst.Close
Set rst = Nothing

End Function
作者: sunny-xie    时间: 2007-11-30 08:54
下面这段代码效果如何?断号问题,怎么处理?
Private Sub Form_BeforeInsert(Cancel As Integer)
On Error GoTo Err_Form_BeforeInsert
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strSQL As String
Dim lngID 'As Long
lngID = DLookup("AutoNum", "tbl编号", "Date=#" & Date & "#")
If IsNull(lngID) Then
    Set conn = CurrentProject.Connection
    strSQL = "SELECT * FROM tbl编号;"
    rs.Open strSQL, conn, adOpenDynamic, adLockOptimistic
        rs.AddNew
        rs("AutoNum") = 0
        rs("Date") = Date
        rs.Update
    rs.Close
    Set rs = Nothing
    Set conn = Nothing
    lngID = 0
End If
    Me.PO单号 = Format("PO-") & Format(Date, "yymm") & Format(lngID + 1, "000")
Exit_Form_BeforeInsert:
    Exit Sub
Err_Form_BeforeInsert:
    MsgBox Err.Description
    Resume Exit_Form_BeforeInsert
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo Err_Form_BeforeUpdate
Dim intVal As Integer
    intVal = MsgBox("是否保存? 单击否将撤销本次输入。", vbYesNo + vbQuestion + vbDefaultButton1, "fan0217")
If intVal = vbYes Then
    MsgBox "保存成功!", 64, "fan0217"
    If Me.NewRecord = True Then
        Dim conn As New ADODB.Connection
        Dim rs As New ADODB.Recordset
        Dim strSQL As String
        
        Set conn = CurrentProject.Connection
        strSQL = "SELECT * FROM tbl编号 WHERE Date=#" & Date & "#;"
        rs.Open strSQL, conn, adOpenDynamic, adLockOptimistic
            rs("AutoNum") = CInt(Right(Me.PO单号, 3))
            rs.Update
        rs.Close
        Set rs = Nothing
        Set conn = Nothing
    End If
Else
    Me.Undo
End If
Exit_Form_BeforeUpdate:
    Exit Sub
Err_Form_BeforeUpdate:
    MsgBox Err.Description
    Resume Exit_Form_BeforeUpdate
End Sub
作者: sunny-xie    时间: 2007-11-30 08:59
标题: 回复 2# 的帖子
我那时在浏览这里的帖子,发错地方。。
作者: 小宜    时间: 2007-11-30 20:09
学习学习学习学习学习
作者: sszssz    时间: 2008-12-14 12:10
注册会员



个人空间 发短消息 加为好友 当前离线  6# 大 中 小 发表于 2007-11-30 20:09  只看该作者
学习学习
作者: tdhgj    时间: 2009-2-23 19:29
good
作者: twins    时间: 2009-2-25 03:08
要学习啊
作者: kn1394    时间: 2009-3-16 14:45
try
作者: kn1394    时间: 2009-3-16 18:57
try
作者: chaojianan    时间: 2009-10-15 21:03
学习学习。
作者: dbbygzy    时间: 2009-11-16 17:26
xuexi
作者: 文棣    时间: 2009-12-14 13:59
学习 有益
作者: kfzhigen    时间: 2009-12-15 14:24
学习
作者: sunwrsun    时间: 2010-4-18 10:24
学习学习学习
作者: yihesmxx    时间: 2010-7-12 17:12
学习学习
作者: combine38    时间: 2011-3-9 09:21
xuexi
作者: windjoy    时间: 2011-3-21 14:20
study
作者: huangzheng88    时间: 2012-5-16 10:18
kankan
作者: 灰尘    时间: 2012-7-28 19:56
看看

作者: HOCKHE    时间: 2012-8-24 16:34
看一下
作者: gaofei186    时间: 2015-11-20 15:06
看看一下
作者: tanzeyi    时间: 2015-11-20 17:43
学习一下

作者: Seathelight    时间: 2016-8-25 16:31
学习学习学习




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3