设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 1309|回复: 6
打印 上一主题 下一主题

[Access本身] 自动编号代码,觉得太长了~可以简化吗?

[复制链接]
跳转到指定楼层
1#
发表于 2006-3-27 19:09:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
Sub get_new_number()
Dim mydb As New ADODB.Connection
Dim temp_id As Double
Dim temp_no As String
Dim temp_int As Double
Dim temp_len As Double
mydb.Open SqlConnection
Dim rsX As ADODB.Recordset
Set rsX = New ADODB.Recordset
Set rsX.ActiveConnection = mydb
rsX.Source = "select max([swatch_no]) as max_id from [T - color window]"
rsX.Open , , adOpenForwardOnly, adLockReadOnly

If IsNull(rsX![max_id]) = True Or Trim(rsX![max_id]) = "" Then
   rsX.Close
   serial_no = "G00001"
   Else
   temp_no = rsX![max_id]
   rsX.Close
   temp_int = CDbl(Right(temp_no, 5)) + 1
   temp_len = Len(CStr(temp_int))
   If temp_len = 1 Then
   serial_no.Value = "G0000" & temp_int
   End If
   If temp_len = 2 Then
     serial_no.Value = "G000" & temp_int
   End If
   If temp_len = 3 Then
    serial_no.Value = "G00" & temp_int
   End If
   If temp_len = 4 Then
     serial_no.Value = "G0" & temp_int
   End If
   If temp_len = 5 Then
     serial_no.Value = "G" & temp_int
   End If
   
End If

mydb.Close
End Sub

这是我一位同事写的自动编号按钮代码,觉得太多条件了~能否简化呢?[em06]
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
 楼主| 发表于 2006-3-28 16:01:00 | 只看该作者
怎麼沒人理啊???[em06]
3#
发表于 2006-3-28 23:03:00 | 只看该作者
他的代码是编号不够位数以0补齐,写得很好嘛
4#
发表于 2006-3-30 05:20:00 | 只看该作者
Sub get_new_number()
Dim mydb As New ADODB.Connection
Dim rsX As New ADODB.Recordset
Dim temp_no As String
Dim temp_id As Double, temp_int As Double

mydb.Open SqlConnection
rsX.Open "select max([swatch_no]) as max_id from [T - color window]", mydb, adOpenForwardOnly, adLockReadOnly


If IsNull(rsX![max_id]) Or Trim(rsX![max_id]) = "" Then
   serial_no = "G00001"
Else
   temp_no = rsX![max_id]
   temp_int = CDbl(Right(temp_no, 5)) + 1
   If Len(CStr(temp_int)) <= 5 Then serial_no.Value = "G" & String("0", 5 - Len(CStr(temp_int))) & temp_int
End If
rsX.Close
mydb.Close
End Sub
5#
发表于 2006-3-30 06:04:00 | 只看该作者
serial_no.Value = "G" & right("0000" & temp_int,5)

temp_int不要超过5位
6#
 楼主| 发表于 2006-3-30 17:01:00 | 只看该作者
麻煩海狸先生了,我收好了呵呵~

AND Why temp_int不要超过5位 ?

我是個超級小白不太懂VBA啊......

我同事如果用這段呢?   serial_no.value="G" & format("00000",temp_int)


[此贴子已经被作者于2006-3-30 9:15:30编辑过]

7#
发表于 2006-4-8 06:52:00 | 只看该作者
正好我需要这样的代码,谢谢了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-27 14:02 , Processed in 0.098736 second(s), 30 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表