我是想在FORM1 LOAD时在FORM1 里的一个TEXT1里显示当前月份的最大号,如下代码错哪?
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim sq1 As String
cnn.Open "provider=Microsoft.jet.OLEDB.4.0;data source= " & App.Path & "\分录表.mdb"
sq1 = "Select max(号) as hao from flb where 月='" & Month(DTPicker1) & "'"
 rst.Open sq1, cnn, adOpenKeyset, adLockOptimistic
    
         rst.MoveLast
                   
    gethao = Format(Val(hao.Value) + 1)
    rst.Close
    
    Set rst = Nothing
TEXT1.TXT=gethao
错误:数据类型不正确,以下句子出错
rst.Open sq1, cnn, adOpenKeyset, adLockOptimistic
    
如何得到这个号呀?求大家看看

解决方案 »

  1.   

    sq1 = "Select max(号) as hao from flb where 月=" & Month(DTPicker1) & ""
      

  2.   

    如果是整数的话
    sq1 = "Select max(号) as hao from flb where 月 = " & Month(DTPicker1)就可以了
      

  3.   

    就比如有一个A表,有个a 字段,是数字型的,1,2,3是它的值。我用 select max(a) from A,能搜索出3。现在我想搜索出A表a值最大再加1的值。我应该怎么写?
      

  4.   

    Dim cnn As New ADODB.Connection
    Dim rst As New ADODB.Recordset
    Dim sq1 As String
    cnn.Open "provider=Microsoft.jet.OLEDB.4.0;data source= " & App.Path & "\分录表.mdb"
    sq1 = "Select max(号) as hao from flb where 月=" & Month(DTPicker1)
     rst.Open sq1, cnn, adOpenKeyset, adLockOptimistic
        
      'rst.MoveLast
        
      gethao = Format(Val(rst!hao) + 1)
      rst.Close
        
      Set rst = Nothing
    TEXT1.TXT = gethao
      

  5.   

    if not isnull(rst!hao) then
    gethao = rst!hao + 1
    else
    gethao = 1
    end if 
      

  6.   


    rst.open "SELECT MAX(a)+1 AS NEWFIELD FROM A",cnn,adOpenStatic
    rst.filed(0)即为你想要的
    因为用了MAX,所以查出来的是最大值,所以查出来的记录集只有一条,也就不必用movefirst or movelast
      

  7.   

    sorry 
    rst.filed(0)应改为rst.fileds(0)