Private Sub Text2_Validate(Cancel As Boolean)  Dim gcbh As String
  Dim gcmc As String
  gcbh = Trim(Text2.Text)
  rec.Open "select * from zdgc where gcdm='"& trim(text2.text)&"' ", serverconnect, adOpenKeyset, adLockOptimistic,adcmdtext
   If not rec.bof Then
       gcmc = rec.Fields("gcmc").Value &""
   Else
       Text2.Text = ""
   End If
  rec.Close
  if gcbh<>"" then  Text3.Text = gcmc
End Sub
试试看,换一个事件

解决方案 »

  1.   

    在SQL语句中加入“where”直接判断是否有记录,然后直接存入Text3中rec.Open "select * from zdgc Where gcdm=" & Text2 , serverconnect, adOpenKeyset, adLockOptimistic
    If rec.recordcount Then   
      Text3.Text= rec.Fields("gcmc").Value
    Else if
      Text2.Text = ""
    End If
    rec.Close
      

  2.   

    如果gcdm是数值型的
    "select * from zdgc where gcdm='"& trim(text2.text)&"' "就不行了还是
    Private Sub Text2_Validate(Cancel As Boolean)
    Dim gcmc As String
    Dim gcbh As String
      
      gcbh = Trim(Text2.Text)
      gcmc = ""
      
      rec.Open "select * from zdgc ", serverconnect, adOpenKeyset, adLockOptimistic
      Do Until rec.EOF
        If rec!gcdm = gcbh Then gcmc = rec!gcmc
        rec.MoveNext
      Loop
      rec.Close
       
      Text3.Text = gcmc
    End Sub