代码如下:
FileName = "2011-11"
If Form2.Command1 Then
  sj1 = Format(Form2.DTPicker3, "hh:nn:ss")
  sj2 = Format(Form2.DTPicker5, "hh:nn:ss")
  Else
  sj1 = "00:00:00"
  sj2 = Format(Now, "hh:nn:ss")
  End If
chaxun1 = " Select " & CXL & " From 振动量 Where 时间 Between # " & Form2.DTPicker1.Value & " " & sj1 & " # And # " & Form2.DTPicker2.Value & " " & sj2 & " # order by 时间"
YZ1 = chaxun1
XZ = " Select 时间 From 振动量 Where 时间 Between # " & Form2.DTPicker1.Value & " " & sj1 & " # And # " & Form2.DTPicker2.Value & " " & sj2 & " # order by 时间"
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\VB\" & FileName & ".mdb;Persist Security Info=False"
Adodc2.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\VB\" & FileName & ".mdb;Persist Security Info=False"
Adodc1.RecordSource = YZ1
Adodc2.RecordSource = XZ
Adodc1.Refresh
Adodc2.Refresh
Ma = " select max(" & CXL & ") from 振动量 Where 时间 Between  # " & Form2.DTPicker1.Value & " " & sj1 & " #  And # " & Form2.DTPicker2.Value & " " & sj2 & " # "
Mi = " select min(" & CXL & ") from 振动量 Where 时间 Between # " & Form2.DTPicker1.Value & " " & sj1 & " # And # " & Form2.DTPicker2.Value & " " & sj2 & " # "
ts = " select count(*) from 振动量  Where 时间 Between  # " & Form2.DTPicker1.Value & " " & sj1 & " #  And # " & Form2.DTPicker2.Value & " " & sj2 & " # "
ymax1 = sql_result(Ma)
ymin1 = sql_result(Mi)
zl = sql_result(ts)
模块:
Public Function sql_result(sql As String) As String
 Dim conn As New ADODB.Connection
 Dim rs As New ADODB.Recordset
 Set conn = CreateObject("adodb.connection")
 conn.Provider = "Provider=Microsoft.Jet.OLEDB.4.0"
 conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\VB\" & FileName & ".mdb;Persist Security Info=False"
 rs.Open sql, conn, adOpenStatic, adLockOptimistic
 conn.Execute (sql)
 sql_result = rs(0).Value
conn.Close
End Function
首先是执行后rs.value这里使用无效null,其次之前试过后zl得不到时间范围内记录条数,zl=0.请各位帮忙看看,谢谢了。

解决方案 »

  1.   

    hh:nn:ss 改为 hh:mm:ss 试试
      

  2.   

    chaxun1 = "Select " & CXL & " From 振动量 Where 时间 Between #" & Format(Form2.DTPicker1.Value, "yyyy-mm-dd") & " " & sj1 & "# And #" & Format(Form2.DTPicker2.Value, "yyyy-mm-dd") & " " & sj2 & "# order by 时间"
      

  3.   

    还是老样子“使用无效null”,是不是between...and ...这中间标点上有毛病
      

  4.   

    Public Function sql_result(sql As String) As String
     Dim conn As New ADODB.Connection
     Dim rs As New ADODB.Recordset
     Set conn = CreateObject("adodb.connection")
     conn.Provider = "Provider=Microsoft.Jet.OLEDB.4.0"
     conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\VB\" & FileName & ".mdb;Persist Security Info=False"
     rs.Open sql, conn, adOpenStatic, adLockOptimistic
     'conn.Execute (sql)  '不需要此句'必须对执行结果进行判断
    if rs.recordcount>0 then
    if not isnull(rs(0)) then 
     sql_result = rs(0)
    end if
    end if
    conn.Close
    End Function另外,前述的" # "在井号前后加有空格,是否会影响执行条件,没有测试,将其前后空格去掉试试看
    Ma = "select max(" & CXL & ") from 振动量 Where 时间 Between #" & Form2.DTPicker1.Value & " " & sj1 & "# And #" & Form2.DTPicker2.Value & " " & sj2 & "#"
      

  5.   

    打个断点,看看你在 SQL 命令字符串不就知道了吗?
      

  6.   

    查询语句修改后可返回所需数值,但模块里依旧显示rs.value 使用无效null,接下来该怎么办?
      

  7.   

    查询语句修改后可返回所需数值,但模块里依旧显示rs.value 使用无效null,接下来该怎么办?
      

  8.   

    sql_result = rs(0).Value & ""
      

  9.   

    '或者这样:
    if not isnull(rs(0).value) then
        sql_result = rs(0).Value
    end if