Private Sub mnuEmpRemove_Click()
   Dim rs As ADODB.Recordset
   Dim str1 As String
   str1 = InputBox("请输入要删除的员工的编号", "删除员工")
   Set rs = g_DBCon.Execute("select * from EmpList where ENumber='" & str1 & "'")
   If Not (rs.EOF And rs.BOF) Then
      Set rs = Nothing
      g_DBCon.Execute ("delete from EmpList where ENumber='" & str1 & "'")
      MsgBox "编号为" & str1 & "的员工信息已经被删除... ", vbInformation, "删除员工信息"
   Else
      MsgBox "编号为" & str1 & "的员工信息不存在", vbInformation, "编号错误"
   End If
End Sub
这是删除员工信息的代码,当从对话框输入的编号与记录中匹配时就删除该条记录,否则
提示该记录不存在,现在有一个问题:当点击inputbox的取消按钮时也会弹出else后的msgbox,
要想点击取消时直接关闭而不弹出msgbox,该怎么做呢?

解决方案 »

  1.   

    当你点取消的时候inputbox返回的就是一个空值,也就是"",所以在inputbox后面加一句判断的语句就行了塞!代码如下:
    Private Sub mnuEmpRemove_Click()
       Dim rs As ADODB.Recordset
       Dim str1 As String
       str1 = InputBox("请输入要删除的员工的编号", "删除员工")
    if str1<>"" then               '首先判断有没有点取消按钮
       Set rs = g_DBCon.Execute("select * from EmpList where ENumber='" & str1 & "'")
       If Not (rs.EOF And rs.BOF) Then
          Set rs = Nothing
          g_DBCon.Execute ("delete from EmpList where ENumber='" & str1 & "'")
          MsgBox "编号为" & str1 & "的员工信息已经被删除... ", vbInformation, "删除员工信息"
       Else
          MsgBox "编号为" & str1 & "的员工信息不存在", vbInformation, "编号错误"
       End If
    end if
    End Sub
      

  2.   

    if strptr(str1)=0 then '说明点击取消
    else
       if str1="" then  '说明是输入空字串,点确认
       else
       end if
    end if
      

  3.   

    Private Sub mnuEmpRemove_Click()
       Dim rs As ADODB.Recordset
       Dim str1 As String
       str1 = InputBox("请输入要删除的员工的编号", "删除员工")
       If str1="" Then goto handler:
       Set rs = g_DBCon.Execute("select * from EmpList where ENumber='" & str1 & "'")
       If Not (rs.EOF And rs.BOF) Then
          Set rs = Nothing
          g_DBCon.Execute ("delete from EmpList where ENumber='" & str1 & "'")
          MsgBox "编号为" & str1 & "的员工信息已经被删除... ", vbInformation, "删除员工信息"
       Else
          MsgBox "编号为" & str1 & "的员工信息不存在", vbInformation, "编号错误"
       End If
    handler:
    End Sub
      

  4.   

    首先更正楼上的错误应该是:str1<>" "
    不好意思按楼上的修改后还是老样子
      

  5.   

    不能吧! 
    如果你点了"取消",这时input返回的是"一个空串"",利用goto跳到End Sub前,随后退出!
    就是If str1="" then goto 
    你再试下
      

  6.   

    另外还请教一个问题:
    Private Sub ShowData()                       '显示员工信息的函数
       Dim intindex As Long
       TxtCode.Text = rs.Fields("ENumber").Value
       TxtName.Text = rs.Fields("EName").Value
       TxtAge.Text = rs.Fields("EAge").Value
       CmbDate.Text = rs.Fields("EDate").Value
       TxtAddress.Text = rs.Fields("EAddress").Value
       For intindex = 0 To CmbDate.ListCount - 1
          '模式为2-DropDown List的组合框不能直接通过文本赋值
          '只能通过指定ListIndex值来选择某个项
           If CmbDate.List(intindex) = Trim(rs.Fields("EDate").Value) Then
              CmbDate.ListIndex = intindex
           End If
       Next intindex
    End SubPrivate Sub cmdDelete_Click()             '删除员工信息
       If MsgBox("确认删除该员工记录信息吗?", vbQuestion + vbYesNo, "删除员工信息") = vbYes Then
          rs.Delete
          Call cmdNext_Click
       End If
    End Sub
    当点击删除按钮后该员工记录仍然存在,不知是什么原因?(在模块中已经定义了recordset和
    connection对象)