private FistDay as string
Private Sub CountMoney() '如果今天是1号,则要将数据插入财务收支表
   Dim D As Integer
   Dim Y, M As String
   D = CInt(Day(Date))
   If D = 1 Then
      Y = CStr(Year(Date))
      M = CStr(Month(Date))
   End If
   Debug.Print CDate(Y & "-" & M & "-" & "1")'此处就有错误(类型错误),该怎么写呢?
   FirstDay = Format(CDate(Y & "-" & M & "-" & "1"), "yyyy-mm-dd")
End Sub
Private Sub SeekRepair()
Dim M As String
If Month(Date) < 10 Then
   M = "0" + CStr(Month(Date))
End If
 YTime = CStr(Year(Date)) + M
Call execDatabase
Call CountMoney
 txtSQL = "select sum(维修费用) from 维护维修表 where 维修时间>'" & CDate(FirstDay) - 31 & "'and 维修时间<'" & FirstDay & "'"
 Debug.Print txtSQL
 conn.Execute txtSQL
 rs.Open txtSQL, conn, adOpenStatic, adLockOptimistic
End Sub
是为了统计上个月维修费用的总和.
这样统计有些可能不够准确,有什么别的好方法吗?

解决方案 »

  1.   

    Debug.Print 只能跟字符串
    Debug.Print Y & "-" & M & "-" & "1"
      

  2.   

    可是下一句FirstDay = Format(CDate(Y & "-" & M & "-" & "1"), "yyyy-mm-dd")
    仍有类型错误!
      

  3.   

    Dim D As Integer
       Dim Y As String, M As String
       D = CInt(Day(Now))
       If D = 1 Then
          Y = CStr(Year(Now))
          M = CStr(Month(Now))
          debug CDate(Y & "-" & M & "-" & "01") '此处就有错误(类型错误),该怎么写呢?
          debug Format((Y & "-" & M & "-" & "01"), "yyyy-mm-dd")
       End If
      

  4.   

    只有D=1时Y和M才有值,其它情况都是空值,所以出现类型错误、。
      

  5.   

    要加等号的那一种SQL语句:
    txtSQL = "select sum(维修费用) from 维护维修表 where 维修时间>='" & CDate(FirstDay) - 31 & "'and 维修时间<='" & FirstDay & "'"
      

  6.   

    Debug.Print CDate(format(Y & "-" & M & "-" & "1","YYYY-MM-DD")'此处就有错误(类型错误),该怎么写呢?
      

  7.   

    谢谢大家的回答,对不起,这么久才回答大家的问题.
    原来是if d=1 then出了问题(原来本来是用来判断的结果没删除这句,所有才会出错),晴明说的正确.
    各位有没有别的什么方法得到比较精确的值呢?(最好是不要去判断这个月是否有31天的那种.)
    我是指维修时间>='" & CDate(FirstDay) - 31 & "'
    回答出来就解贴.
      

  8.   

    写成:
    txtSQL = "select sum(维修费用) from 维护维修表 where datepart(month,维修时间)=datepart(month,dateadd(month,-1,getdate()))"
      

  9.   

    CDate(format(Y & "-" & M & "-" & "1","YYYY-MM-DD")此处并没有错误!!
    那条件多余!!
      

  10.   

    维修时间>='" & dateadd("M",-1,CDate(FirstDay)) & "'
      

  11.   

    private FistDay as string
    Private Sub CountMoney() '如果今天是1号,则要将数据插入财务收支表
       Dim D As Integer
       Dim Y, M As String
       D = CInt(Day(Date))
       If D = 1 Then
          Y = CStr(Year(Date))
          M = CStr(Month(Date))
       End If
       Debug.Print CDate(Y & "-" & M & "-" & "1")'此处就有错误(类型错误),该怎么写呢?
       FirstDay = Format(CDate(Y & "-" & M & "-" & "1"), "yyyy-mm-dd")
    End Sub
    Private Sub SeekRepair()
    Dim M As String
    If Month(Date) < 10 Then
       M = "0" + CStr(Month(Date))
    End If
     YTime = CStr(Year(Date)) + M
    Call execDatabase
    Call CountMoney
     txtSQL = "select sum(维修费用) from 维护维修表 where 维修时间>'" & CDate(FirstDay) - 31 & "'and 维修时间<'" & FirstDay & "'"
     Debug.Print txtSQL
     conn.Execute txtSQL
     rs.Open txtSQL, conn, adOpenStatic, adLockOptimistic
    End Sub
    是为了统计上个月维修费用的总和.
    这样统计有些可能不够准确,有什么别的好方法吗?