Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 1948|回复: 7

[已解决]查找功能的问题点

[复制链接]
发表于 2013-7-24 09:19 | 显示全部楼层 |阅读模式
   
       Public Sub Command1_Click()
         Dim strSql As String
         Adodc1.ConnectionString = "rovider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path & "\入库信息管理系统.mdbersist Security Info=False"
         strSql = "select * from 入库批量返品统计 where 返品日期 between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "#"
         If Text1 <> "" And Text2 <> "" Then
         strSql = strSql & "and 供应商='" & Text1.Text & "' and 车种 ='" & Text2.Text & "'"
         ElseIf Text1 <> "" And Text2 = "" Then
         strSql = strSql & "and 供应商='" & Text1.Text & "'"
         ElseIf Text1 = "" And Text2 <> "" Then
         strSql = strSql & "and 车种='" & Text2.Text & "'"
         End If
  在这个编辑中  当按照 DTPicker1和 DTPicker2时间段查找时不精确,似乎失效
     举例:1.按照时间段7月1~7月23日查找时 查找结果不全面,数据库的7月5日~9日的结果没有
               2.按照时间段7月1~7月9日查找时 查找结果会出现时间段之外的结果。
    附件有实例:“批量返品.frm”
最佳答案
2013-7-25 05:06
本帖最后由 adders 于 2013-7-24 19:55 编辑
qihaizhong2013 发表于 2013-7-24 02:58
你看看 一下是完整的查询编辑
     Public Sub Command1_Click()
         Dim strSql As String


  strSql = "select * from 入库批量返品统计 where 返品日期 between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "#"



  strSql = "select * from 入库批量返品统计 where 返品日期 between '" & DTPicker1.Value & "' and '" & DTPicker2.Value & "' "

在Table "入库批量返品统计"中, Field "返品日期"设置的是"文本"格式,不是"日期/时间"格式. 见附件图示.

DTPicker1.Value需要符合"yyyy-m-d"这种日期写法的文本格式以匹配"返回日期"的格式,比如用:

Format(DTPicker1.Value, "yyyy-m-d")

最后,这样写法会有很大的漏洞,因为如果文本比较的话, m-d这样写法,月份顺序会是1,10,11,12,2,3,4,5,6,7,8,9,天也一样,所以如果一定用文本格式,建议改成"yyyy-mm-dd".或者最好还在ACCESS的TABLE中将"返品日期"这一列改成真正的"Date/Time"格式然后就可在SQL中用#取值.

入库信息管理系统.rar

349.52 KB, 下载次数: 10

发表于 2013-7-24 11:22 | 显示全部楼层
没安装VB,没办法测试你的代码,能否放在EXCEL查询试试
回复

使用道具 举报

发表于 2013-7-24 11:31 | 显示全部楼层
你这个SQL语句中关于日期中是没有问题的,问题肯定出在后面的连接上
     If Text1 <> "" And Text2 <> "" Then
         strSql = strSql & "and 供应商='" & Text1.Text & "' and 车种 ='" & Text2.Text & "'"
         ElseIf Text1 <> "" And Text2 = "" Then
         strSql = strSql & "and 供应商='" & Text1.Text & "'"
         ElseIf Text1 = "" And Text2 <> "" Then
         strSql = strSql & "and 车种='" & Text2.Text & "'"
         End If
你可以逐句运行一下或在最后加一个msgbox  或dubeg.print,看看strsql最后的字符串到底是什么
回复

使用道具 举报

 楼主| 发表于 2013-7-24 14:05 | 显示全部楼层
我觉得是这句短语的问题
   strSql = "select * from 入库批量返品统计 where 返品日期 between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "#"
    按照短语的思路不应该出现以上描述的问题点
回复

使用道具 举报

发表于 2013-7-24 14:07 | 显示全部楼层
where 返品日期 between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "#"
这个没任何问题
回复

使用道具 举报

 楼主| 发表于 2013-7-24 15:50 | 显示全部楼层
那是为什么呢!时间段选定后不可能查找出之外的数据呀!
回复

使用道具 举报

 楼主| 发表于 2013-7-24 15:58 | 显示全部楼层
   你看看 一下是完整的查询编辑
     Public Sub Command1_Click()
         Dim strSql As String
         Adodc1.ConnectionString = "rovider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path & "\入库信息管理系统.mdbersist Security Info=False"
         strSql = "select * from 入库批量返品统计 where 返品日期 between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "#"
         If Text1 <> "" And Text2 <> "" Then
         strSql = strSql & "and 供应商='" & Text1.Text & "' and 车种 ='" & Text2.Text & "'"
         ElseIf Text1 <> "" And Text2 = "" Then
         strSql = strSql & "and 供应商='" & Text1.Text & "'"
         ElseIf Text1 = "" And Text2 <> "" Then
         strSql = strSql & "and 车种='" & Text2.Text & "'"
         End If
         Adodc1.RecordSource = strSql
         Adodc1.Refresh
   Set DataGrid1.DataSource = Adodc1
   DataGrid1.Refresh
   DataGrid1.Columns(0).Width = 1200
   DataGrid1.Columns(1).Width = 1200
   DataGrid1.Columns(2).Width = 1200
   DataGrid1.Columns(3).Width = 1200
   DataGrid1.Columns(4).Width = 1200
   DataGrid1.Columns(5).Width = 1200
   DataGrid1.Columns(6).Width = 1200
   DataGrid1.Columns(7).Width = 1200
   DataGrid1.Columns(8).Width = 2400
   End Sub
回复

使用道具 举报

发表于 2013-7-25 05:06 | 显示全部楼层    本楼为最佳答案   
本帖最后由 adders 于 2013-7-24 19:55 编辑
qihaizhong2013 发表于 2013-7-24 02:58
你看看 一下是完整的查询编辑
     Public Sub Command1_Click()
         Dim strSql As String


  strSql = "select * from 入库批量返品统计 where 返品日期 between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "#"



  strSql = "select * from 入库批量返品统计 where 返品日期 between '" & DTPicker1.Value & "' and '" & DTPicker2.Value & "' "

在Table "入库批量返品统计"中, Field "返品日期"设置的是"文本"格式,不是"日期/时间"格式. 见附件图示.

DTPicker1.Value需要符合"yyyy-m-d"这种日期写法的文本格式以匹配"返回日期"的格式,比如用:

Format(DTPicker1.Value, "yyyy-m-d")

最后,这样写法会有很大的漏洞,因为如果文本比较的话, m-d这样写法,月份顺序会是1,10,11,12,2,3,4,5,6,7,8,9,天也一样,所以如果一定用文本格式,建议改成"yyyy-mm-dd".或者最好还在ACCESS的TABLE中将"返品日期"这一列改成真正的"Date/Time"格式然后就可在SQL中用#取值.

1.zip

100.92 KB, 下载次数: 10

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-3 06:45 , Processed in 0.409288 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表