【问题标题】:VBA unable to execute codeVBA 无法执行代码
【发布时间】:2015-02-05 13:26:32
【问题描述】:

我的问题是,无论我在文本框中输入什么,即使数据库中不存在该记录,它也只会执行MsgBox ("Estimate No" & editform.edittext.Value & " is found")。它应该执行代码 MsgBox ("Unable to find '" & editform.edittext.Value & "'") 在数据库中找不到记录时。

If editform.OptionButton1.Value = True Then
    strConnectStr = "something"
    Qry = "SELECT * " & _
          "FROM test.dbo.estimateRegister " & _
          "WHERE test.dbo.estimateRegister.estimateNo = '" & edittext.Text & "%'"

    cnt.Open strConnectStr
    rst.Open Qry, cnt

    If Not rst.EOF Then
        MsgBox ("Estimate No" & editform.edittext.Value & " is found")
        Unload Me
        UserForm2.Show
    Else
        MsgBox ("Unable to find '" & editform.edittext.Value & "'")
    End If
End If

【问题讨论】:

  • 尝试删除大括号...只是 MsgBox "Message"
  • 没有区别。我试过了。
  • 您在代码中 If Not rst.EOF Then ... 之前的某个位置是否有 On Error Resume Next?如果是这样,那么rst 可能是Nothing 或另一个此时没有.EOF 的对象。
  • 不。我没有任何代码
  • 那么此时rst是一个什么样的对象呢?您是否尝试过单步调试并在本地窗口中查看rst?或者将rst.RecordCount 分配给一个变量并观察这个变量?也许有记录集?

标签: vba excel recordset


【解决方案1】:

尝试替换这一行:

If Not rst.EOF Then

用这条线

If rst.RecordCount <> 0 Then

【讨论】:

  • @Reeve 然后会发生什么?你有错误吗?你能在It Doesn't work更具体吗?
  • 还是和以前一样
  • @Reeve Strange 因为它对我有用。顺便说一句,您的第一个代码也可以正常工作。
  • 这很奇怪,让我解决一下,我会尽快回复您。
猜你喜欢
  • 1970-01-01
  • 2017-05-26
  • 1970-01-01
  • 2018-11-16
  • 1970-01-01
  • 1970-01-01
  • 2015-10-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多