您的位置:程序门 -> vb -> 基础类



自动生成编号的问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


自动生成编号的问题[已结贴,结贴人:wyhlany]
发表于:2007-09-24 10:35:33 楼主
在窗体中有一个按钮和一个文本框,点击按钮时,在文本框中自动生成消费单据编号,这个单据编号是由每次判断consume表中的最后一条记录的后四个字符是否重复,而生成的,可是程序运行没有错误,就是不自动执行+1。我已经试过了,它执行的就是else后面的那句。请大家帮我看看!
private   sub   command1_click()

dim   djbh   as   long
dim   strcnn   as   new   adodb.connection
set   strcnn   =   new   adodb.connection
          strcnn.open   "provider=sqloledb.1;integrated   security=sspi;persist   security   info=false;initial   catalog=card;data   source=wyh "
dim   rs1   as   new   adodb.recordset
rs1.cursorlocation   =   aduseclient
rs1.open   "select   *   from   consume   order   by   listno1 ",   strcnn,   adopendynamic,   adlockoptimistic

if   rs1.recordcount   >   0   then
            if   rs1.eof   =   false   then
              rs1.movelast
              djbh   =   val(right(rs1.fields( "listno1 "),   4))+1                           ——〉就是这里不执行
             
              text1.text   =   "xf "   &   format(date,   "yyyymmdd ")   &   "- "   &   format(djbh,   "0000 ")

            end   if
else
          text1.text   =   "xf "   &   format(date,   "yyyymmdd ")   &   "-0001 "

rs1.close

end   if
end   sub
发表于:2007-09-24 10:43:151楼 得分:3
'这样试试:
rs1.open   "select   max(listno1)   as   listno1   from   consume ",   strcnn,   adopendynamic,   adlockoptimistic

if   rs1.recordcount   >   0   then
        if   not   isnull(rs1.fields( "listno1 "))   then
                djbh   =   clng(rs1.fields( "listno1 "))+1              
                text1.text   =   "xf "   &   format(date,   "yyyymmdd ")   &   "- "   &   format(djbh,   "0000 ")
        end   if
else
        text1.text   =   "xf "   &   format(date,   "yyyymmdd ")   &   "-0001 "

end   if
rs1.close
发表于:2007-09-24 11:44:112楼 得分:0
如果直接执行else后面的语句,说明数据库的表consume记录为空,你是不是初始记录显示到text1后没回写到表中?
发表于:2007-09-24 11:53:453楼 得分:0
我的单据编号是类似于“xf20070924-0001”这种的字符串,不能取最大值的。
发表于:2007-09-24 11:56:474楼 得分:0
我打开数据库看了,里面有5、6条记录,而且还有一条编号为xf20070924-0001的,它又在text1中显示了这个编号。
发表于:2007-09-24 12:49:385楼 得分:2
我觉得你这样的情况最好用一张表来保存编号信息。比如这张二个字段,日期和编号,一行数据,存取当日日期和最大编号。每次程序启动时这样处理一下:
cn.EXECute   "update   编号表   set   日期= ' "&   date   & " ',编号=0   where   日期 < ' "&   date   & " ' "

插入数据的时候,同时更新这个表的最大值,放在一个事务中处理,这样处理效率应该好
发表于:2007-09-25 08:44:266楼 得分:0
rs1.open   "select   max(right(listno1,4))   as   listno1   from   consume ",   strcnn,   adopendynamic,
发表于:2007-09-25 09:52:347楼 得分:15
'以下代码在access2000+vb6调试通过,数据库test.mdb中表tb字段-bh

private   sub   command1_click()

        dim   db   as   adodb.connection
        dim   rs   as   adodb.recordset
        dim   tempbh   as   string,   tempyear   as   string,   bhnum   as   string

        set   db   =   new   adodb.connection
        db.cursorlocation   =   aduseclient
        db.connectionstring   =   "provider=microsoft.jet.oledb.4.0; "   +   "data   source= "   +   app.path   +   "\test.mdb "
        db.open   "data   source= "   +   app.path   +   "\test.mdb; "

        set   rs   =   new   adodb.recordset

        rs.open   "tb   order     by   bh ",   db,   adopenkeyset,   adlockoptimistic

        if   rs.recordcount   =   0   then                                                       '第一次建立编号

                bhnum   =   "xf "   &   format(date,   "yyyymmdd ")   &   "-0001 "

        else

                tempdate   =   format(date,   "yyyymmdd ")
               
                'msgbox   tempdate

                if   rs.state   =   adstateopen   then   rs.close

                rs.open   "select   *   from   tb   where   bh   like   'xf '+ ' "   &   tempdate   &   " '+ '% '   order   by   bh ",   db,   adopendynamic,   adlockoptimistic

                'msgbox   rs.recordcount

                  if   rs.recordcount   >   0   then                                       '当天有记录时

                        if   rs.state   =   adstateopen   then   rs.close

                        rs.open   "tb   order   by   bh ",   db,   adopendynamic,   adlockoptimistic

                        rs.movelast

                        tempyear   =   left(rs!bh,   11)                         '左边11位

                        tempbh   =   right(rs!bh,   4)   +   1                     '右边4位

                        if   len(tempbh)   =   1   then
                                tempbh   =   "000 "   &   tempbh
                        end   if

                        if   len(tempbh)   =   2   then
                                tempbh   =   "00 "   &   tempbh
                        end   if

                        if   len(tempbh)   =   3   then
                                tempbh   =   "0 "   &   tempbh
                        end   if

                        msgbox   tempbh

                        bhnum   =   tempyear   &   tempbh

                else

                        bhnum   =   "xf "   &   format(date,   "yyyymmdd ")   &   "-0001 "       '当天初始临时编号

                end   if

        end   if

        rs.addnew
        rs!bh   =   bhnum
        rs.update

        rs.close
        set   db   =   nothing

end   sub
发表于:2007-09-25 10:21:488楼 得分:0
我用断点调试发现了:原来是赋值的问题。djbh   =   val(right(rs1.fields( "listno1 "),   4))+1这句中rs1.fields( "listno1 ")可以取到值为“xf20070925-0001”,但是赋值给djbh就不行了,djbh的值为0,不知道是怎么回事?
我再试试jieweibin(jwb)的方法。
发表于:2007-09-25 11:44:169楼 得分:0
谢谢jieweibin(jwb),你的代码我改了改,果然成功了。
发表于:2007-09-25 11:57:4010楼 得分:0
呵,不客气~~   程序一般都要调试才能发现问题,比如关键地方设置弹出消息
发表于:2007-09-26 10:46:5811楼 得分:0
又学会了一招,呵呵!
发表于:2007-10-09 14:43:5912楼 得分:0
代码再优化一下,tempbh   =   format(right(rs!bh,   4)   +   1,   "0000"),这样下面的几个补0的if语句就不需要了    


快速检索

最新资讯
热门点击