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
Set cn = CurrentProject.Connection
Dim strSQL As String
strSQL = "select * from " & tblName & " order by " & fldName
rs.Open tblName, cn, 1, 2
Debug.Print rs.RecordCount
If rs.RecordCount > 0 Then
rs.MoveLast
'取得最大值的时间前缀
OldDate = Nz(Mid$(rs(fldName), intPrefix + 1, intDate), "")
'判断新增的值是否跨时间
If strToday = OldDate Then
AutoID = Prefix & strDate & Format(Val(Right$(rs(fldName), intLength) + 1), String(intLength, "0"))
Else
AutoID = Prefix & strDate & String(intLength - 1, "0") & "1"
End If
Else
AutoID = Prefix & strDate & String(intLength - 1, "0") & "1"
End If
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
End Function作者: Victor_Duane 时间: 2007-12-8 09:47
如果是要写在绑定数据窗体里
Private Sub Form_Current()
if me.newrecord then
me.id=AutoID("tbl客户", "po编号", 3, "PO-","yymm")
end if
end sub作者: sunny-xie 时间: 2007-12-8 10:12 标题: 回复 6# 的帖子 斑竹有没有调试过?
加上代码后,编号不会变动啊