|
下面这段代码,就是如何在窗体中使用的方法
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 |
|