Public Function find_str() As String
  If Text1.Text = Empty & Text2.Text = Empty Then
    MsgBox "cannot be empty"
    Form1.Text1.SetFocus
  Else
    rs.open "select field_name from table1 where col1='" _
           & Form1.Text1.Text & "' or col2 = '" & Form1.Text2.Text & "'",", cn, adOpenDynamic, adLockOptimistic
    if rs.BOF <> rs.EOF then
      rs.movefirst
      find_str=rs(field_name)
    end if
  End If
End Function
或者把上面的
 rs.Open "table", cn, adOpenDynamic, adLockOptimistic, adCmdTable
改写为:
 rs.Open find_str(), cn, adOpenDynamic, adLockOptimistic

解决方案 »

  1.   


    在窗体模块中定义就可以了。也可以这样:
     Function find_str(ByVal txt1 As String, ByVal txt2 As String) As String
      If txt1 = "" & txt2 = "" Then
        MsgBox "cannot be empty"
        Else
        rs.open "select field_name from table1 where col1='" _
               & txt1 & "' or col2 = '" & txt2 & "'", ", cn, adOpenDynamic, adLockOptimistic"
        If Not rs.EOF Then
          rs.movefirst
          find_str = rs(field_name)
        End If
      End If
    End Function