当第一条记录输入后再输入一条记录与刚输入的一样,那么能成功,提示不能输入相同数据,但是添加第二天记录后再添加与第2条相同的记录也会提示,但是数据也被加入了。。为什么?以后的记录都是这样。。
 dim i as integer
  
  if input.text =""    then
    msgBox "不能输入空"
  elseif input.text=list. text then
    msgBox "输入不能与已有记录相同"
  else
    if list.listcount=0 then
      list.addRow input.text
    else
      for i=0 to list.listcount-1
        if input.text = list.list(i) then
          msgBox "不能输入与列表中相同的数据"
        else
          list.addRow input.text
          list.cell(list.listcount-1,1)=help.text
          save.enabled = true
        end if
      next
    end if
  end if

解决方案 »

  1.   

    你试试用下面的代码,你的嵌套写得也太复杂了
        Dim i As Integer
      
      if input.text ="" then
        MsgBox "不能输入空"
        GoTo ExitHandler
      End If
      
      if input.text=list.text then
        MsgBox "输入不能与已有记录相同"
        GoTo ExitHandler
     End If
      
        If List.ListCount = 0 Then
          list.addRow input.text
        Else
          For i = 0 To List.ListCount - 1
            if input.text = list.list(i) then
              MsgBox "不能输入与列表中相同的数据"
              GoTo ExitHandler
            Else
              list.addRow input.text
              List.cell(List.ListCount - 1, 1) = help.Text
              save.Enabled = True
            End If
          Next
        End If
      
    ExitHandler:
      

  2.   


     dim i as integer
      
      if input.text =""    then
        msgBox "不能输入空"
      elseif input.text=list. text then
        msgBox "输入不能与已有记录相同"
      else
        if list.listcount=0 then
          list.addRow input.text
        else
          dim norepeat
          norepeat = true
          for i=0 to list.listcount-1
            if input.text = list.list(i) then
              msgBox "不能输入与列表中相同的数据"
              norepeat = false
            end if
          next i
          if norepeat then
            list.addRow input.text
            list.cell(list.listcount-1,1)=help.text
            save.enabled = true
          end if
        end if
      end if
      

  3.   

    用的什么控件(input list help)?
      

  4.   

    嘿嘿,我没有用VB啊。。定义必须放在所有IF,FOR外面。谢谢,搞定了。
      

  5.   

    是控制问题,没有及时结束循环,用exit for跳出就可以了
          For i = 0 To List.ListCount - 1
            if input.text = list.list(i) then
              MsgBox "不能输入与列表中相同的数据"
              exit for    '<------------------------------------此处用跳出循环
            Else
              list.addRow input.text
              List.cell(List.ListCount - 1, 1) = help.Text
              save.Enabled = True
            End If
          Next