Access交流中心

北京 | 上海 | 天津 | 重庆 | 广州 | 深圳 | 珠海 | 汕头 | 佛山 | 中山 | 东莞 | 南京 | 苏州 | 无锡 | 常州 | 南通 | 扬州 | 徐州 | 杭州 | 温州 | 宁波 | 台州 | 福州 | 厦门 | 泉州 | 龙岩 | 合肥 | 芜湖 | 成都 | 遂宁 | 长沙 | 株洲 | 湘潭 | 武汉 | 南昌 | 济南 | 青岛 | 烟台 | 潍坊 | 淄博 | 济宁 | 太原 | 郑州 | 石家庄 | 保定 | 唐山 | 西安 | 大连 | 沈阳 | 长春 | 昆明 | 兰州 | 哈尔滨 | 佳木斯 | 南宁 | 桂林 | 海口 | 贵阳 | 西宁 | 乌鲁木齐 | 包头 |

[5分]for循环不返回外层继续循环,sql执行无效

学习者  发表于:2018-06-01 15:03:06  
复制

很纳闷2个问题,

第一个是内循环后,不返回外层继续循环,我查了很久感觉没错啊,

第二个是sql执行无效(第二个update sql),同样的sql在sql server中执行成功,下面是我的代码,大神帮我看下;


Private Sub cmdAddFA_Click()
   
   Dim strSrc As String
   Dim rstSrc As ADODB.Recordset
   
   Dim strBin As String
   Dim rstBin As ADODB.Recordset
   
   Dim iLoop As Integer
   Dim jLoop As Integer
   
   Dim blnSrc As Boolean
   Dim strUpdate As String
   Dim strNth As String
   
   Dim rst As DAO.Recordset
   
   Dim strMsgBox As String
 
 '************************************************************************************************************

    
    '做bin判断
    '获取bin参数
    strBin = "select * from bas_bin_para where bin_ver=1"
    Set rstBin = New ADODB.Recordset
        rstBin.Open strBin, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
    
    '获取需要比对的数据tb_ht_src
    strSrc = "select * from tb_ht_src"
    Set rstSrc = New ADODB.Recordset
        rstSrc.Open strSrc, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
        
        
    For iLoop = 0 To CInt(rstSrc.recordcount) - 1    'src循环               
        For jLoop = 0 To CInt(rstBin.recordcount) - 1    'bin循环
        
               '设置blnSrc初始值,表示找到符合bin
                blnSrc = True
    
            If rstBin!VF1_L > -999 Then     '空时不做判定
               If rstSrc!VF1 >= rstBin!VF1_L Then   '实际值<设置最小值,blnSrc设置为Flase,bln不符合
                  blnSrc = True
               Else
                  blnSrc = False
               End If
             End If
             
              '符合条件即写入bin值
              If blnSrc = True Then
                 strUpdate = "update tb_ht_src set Bin1_Name=" & rstBin!BIN_NAME & ""
                 strUpdate = strUpdate & " where "
                 strUpdate = strUpdate & "TEST =" & iLoop + 1
                 Debug.Print strUpdate
                 DoCmd.RunSQL strUpdate               
                 '当找到符合的bin时,赋值bin_name后,开始下一个ht_src校验,跳出循环
                 jLoop = CInt(rstBin.recordcount) + 1
              Else
                 rstBin.MoveNext
              End If
              
              'jLoop = CInt(rstBin.recordcount) + 1  '当符合条件后,跳出循环
              Debug.Print jLoop + 1
              
              '当寻找的条数与记录数一致时,跳出循环,找不到任何符合的bin,给最大bin+1
              If CInt(rstBin.recordcount) = jLoop + 1 Then
                 strNth = "update tb_ht_src set Bin1_Name=" & "(select max(BIN_NAME)+1 from bas_bin_para) "
                 strNth = strNth & " where "
                 strNth = strNth & "TEST =" & iLoop + 1
                 Debug.Print strNth
                 DoCmd.RunSQL strNth
                 jLoop = CInt(rstBin.recordcount) + 1
              End If
               
         Next jLoop   '进入下一个循环
              rstSrc.MoveNext
     Next iLoop   '进入下一个循环
         
End Sub

 

Top
总记录:0篇  页次:0/0 9 1 :