我用的数据库是sql server2000
我写的strSql = "select distinct p_YearMonth from tbl_PlanData" 其中p_YearMonth为字段名,类型为char型,这个语句返回的结里总是为-1,不知为什么,不过这个语句在access的数据库下好用,请高手帮忙!!!

解决方案 »

  1.   

    strSql = "select distinct p_YearMonth from tbl_PlanData"
    if rs.state=adstateopen then rs.close
    rs.open strsql,adopenkeyset,adlockreadonly
    msgbox rs.recordcount
    rs.close
      

  2.   

    我写的整个代码为:
    If mAdoYM.State = adStateOpen Then mAdoYM.Close
    strSql = "select distinct p_YearMonth from tbl_PlanData"
    mAdoYM.Open strSql, mAdoConn, adOpenKeyset, adLockOptimistic
    If mAdoYM.RecordCount <> 0 Then
    .
    .
    .
    End IfLeftie(左手,为人民币服务) 老兄,我说的是我在sql语句中,加入distinct就返回-1的,去掉是好用的
      

  3.   

    晕,楼主说的是mAdoYM.RecordCount=-1吧,把语句
    mAdoYM.Open strSql, mAdoConn, adOpenKeyset, adLockOptimistic
    中的adOpenKeyset改成adOpenStatic就可以了。
    另一种方法是打开记录集前,设置:mAdoYM.CursorLocation=adUseClient
      

  4.   

    原因上面的有解释,实际记录集是存在的,你可以这样判断
    if not mAdoYM.eof then end if
      

  5.   

    madoym.CursorLocation = adUseClient '加这句试试
    If mAdoYM.State = adStateOpen Then mAdoYM.Close
    strSql = "select distinct p_YearMonth from tbl_PlanData"
    mAdoYM.Open strSql, mAdoConn, adOpenKeyset, adLockOptimistic
    If mAdoYM.RecordCount <> 0 Then
    .
    .
    .
    End If
      

  6.   

    同意楼上各位大哥的说法,是因为记录集的游标应该设置成adUseClient另外可以试着rs.movefirst
    rs.movelast
    msgbox rs.recordcount移到头,再移到尾。我没试过,看到别人的代码里有这么写的!:)