如下,定义的rsPsHA有.UpdateBatch和MoveLast和MovePrevious.....,我现在要查询一个表里的某一个数据该如何做阿,比如有两列,type_id和type_name,我要查询typeid=111对应的type_name,该如何做,忘高手赐教!
Private rsPsHA As ADODB.Recordset
With rsPsHA
        Select Case Index
            Case 0
                .UpdateBatch
                .MoveLast
            Case 1
                .CancelUpdate
                If .RecordCount <> 0 Then
                    .MoveFirst
                End If
        End Select
End With

解决方案 »

  1.   

    if rspsha.state=adstateopen then rspsha.close
    rspsha.open "select type_name from tablename where type_id='111'",conn,adopenkeyset,adlockreadonly
    if rspsha.reocrdcount>0 then
        msgbox rspsha!type_name &""
    else
        msgbox "没有记录!",48,"提示"
    end if
    rspsha.close
    set rspsha=nothing
      

  2.   

    "select type_name from tablename where type_id='111'",conn,adopenkeyset,adlockreadonly
      

  3.   

    高手不行啊,显示conn那里有错误
      

  4.   

    Option Explicit
        Public mCnnString As String   '定义连接字符串变量Private Sub Form_Load()
        mCnnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db1.mdb" & ";Persist Security Info=False"   '给连接字符串赋值
    End SubPrivate Sub Command1_Click()    '查找
        Dim rsPsHA As New ADODB.Recordset
        Dim cnPsHA As New ADODB.Connection
        cnPsHA.Open mCnnString
        cnPsHA.CursorLocation = adUseClient
        rsPsHA.Open "Select type_name From Table1 Where type_id = '" & "111'", mCnnString, adOpenStatic, adLockOptimistic, adCmdText
        If Not (rsPsHA.Bof And rsPsHA.Eof) Then
            MsgBox rsPsHA("type_name")
        Else
            MsgBox "无该记录!"
        End If
        rsPsHA.Close
        cnPsHA.Close
        Set rsPsHA = Nothing
        Set cnPsHA = Nothing
    End Sub
      

  5.   

    以上要引用Microsoft ActiveX Data Objects 2.x Library
      

  6.   

    再问高手,在select .... where type_id = '111' 的地方我可以用一个变量替换'111'吗,比如替换成select ..... where type_id = xxx ....,我试了好像不行,那里的'111'是一个字符串还是数字啊
      

  7.   

    可以的,例如:
    Dim mTemp As String
    rsPsHA.Open "Select type_name From Table1 Where type_id = '" & mTemp & "'", mCnnString, adOpenStatic, adLockOptimistic, adCmdText
      

  8.   

    如果 type_id 的字段类型是 数值型,那就不要加 引号.如果要用一个变量替换 111,那么这样写:
    字段 type_id 为数值型时:SQLSTR="select ..... where type_id =" & xxx & "...."
    字段 type_id 为文本类时:SQLSTR="select ..... where type_id ='" & xxx & "'...."试一下,有问题再牛皮
      

  9.   

    //在select .... where type_id = '111' 的地方我可以用一个变量替换'111'吗
    可以
      

  10.   

    搞定了,在vb里是..... where type_id =" + xxx + "...."