我写的以下代码要实现的功能是:当我根据从text2和combo1输入的信息,在光盘信息表中查询信息,然后将查询到的信息对应的显示到text6至text16,然后再将这些信息添加到datagrid1中,如果在光盘信息表中查到对应的信息时,以上功能能够实现,但当光盘信息表中没有对应的信息,就没反应了,我希望当没有找到对应信息时返回("无此光盘信息,是否重新填写!", )提示信息。但下面的代码怎么也实现不了。问题出在哪里了?希望大家帮帮忙!
Private Sub Text2_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
 If Text2.Text = "" Or Combo1.Text = "" Then
 MsgBox "请将信息填写完整!", vbOKOnly + vbExclamation, "提示"
 Exit Sub
 End If
 Set cn1 = New ADODB.Connection
  If cn1.State <> adStateClosed Then cn1.Close
  cn1.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\数据库\总数据.mdb;Persist Security Info=False"
  Set rs3 = New ADODB.Recordset
  If rs3.State <> adStateClosed Then rs3.Close
  rs3.CursorLocation = adUseClient
  rs3.Open "select 编号,名称,类型,出版社,供货商,架位,拼音简写,库存数量,现存数量,进货价,租赁单价  from 光盘信息表 where  供货商 ='" & Combo1.Text & "'and 名称 ='" & Text2.Text & "' ", cn1, adOpenDynamic, adLockOptimistic
  'Set DataGrid1.DataSource = rs3
  If rs3.RecordCount > 0 Then 'EOF = False
    If rs3.Fields(1) <> Text2.Text And rs3.Fields(4) <> Combo1.Text Then
    If MsgBox("无此光盘信息,是否重新填写!", vbOKCancel + vbExclamation, "提示") = vbOK Then
     Text2.Text = ""
     Combo1.Text = ""
     Text2.SetFocus  'Exit Sub
  End If
  Else
  Text6.Text = rs3.Fields(0)
  Text7.Text = rs3.Fields(1)
  Text8.Text = rs3.Fields(2)
  Text9.Text = rs3.Fields(3)
  Text10.Text = rs3.Fields(4)
  Text11.Text = rs3.Fields(5)
  Text12.Text = rs3.Fields(6)
  Text13.Text = rs3.Fields(7)
  Text14.Text = rs3.Fields(8)
  Text15.Text = rs3.Fields(9)
  Text16.Text = rs3.Fields(10)
  Text4.Text = rs3.Fields(9)
  Text3.Locked = False
 Command1.Enabled = True
 Command2.Enabled = True
 Text3.SetFocus
 
  'End If
  End If
  End If
End If
End Sub

解决方案 »

  1.   

    rs3.Open "select 编号,名称,类型,出版社,供货商,架位,拼音简写,库存数量,现存数量,进货价,租赁单价 from 光盘信息表 where 供货商 ='" & Combo1.Text & "'and 名称 ='" & Text2.Text & "' ", cn1, adOpenDynamic, adLockOptimistic
    If rs3.RecordCount = 0 Then 
        MsgBox "无此光盘信息,是否重新填写!"
        Text2.Text = ""
        Combo1.Text = ""
        Text2.SetFocus 
        Exit Sub
    else
        ...
    end if
      

  2.   

      rs3.Open "select 编号,名称,类型,出版社,供货商,架位,拼音简写,库存数量,现存数量,进货价,租赁单价 from 光盘信息表 where 供货商 ='" & Combo1.Text & "'and 名称 ='" & Text2.Text & "' ", cn1, adOpenDynamic, adLockOptimisticIf Not rs3.EOF Then
    MsgBox "无此光盘信息,请重新填写!", vbOKOnly + vbExclamation, "提示"
    Text2.Text = ""
    Combo1.Text = ""
    Text2.SetFocus 
    Exit Sub
    End If