Excel精英培训网

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

[已解决]VB查询 修改

[复制链接]
发表于 2013-6-16 09:44 | 显示全部楼层 |阅读模式
        实例:
     Public Sub Command1_Click()
   Adodc1.ConnectionString = "rovider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path & "\入库信息管理系统.mdbersist Security Info=False"
   Adodc1.RecordSource = "select * from 天津北特"        '连接货品入库数据表
   Adodc1.RecordSource = "select * from 天津北特 where 纳品日期 between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "#"
   Adodc1.RecordSource = "select * from 天津北特 where 车种 =" & "'" & Text1.Text & "'"
   Adodc1.RecordSource = "select * from 天津北特 where LOT =" & "'" & Text2.Text & "'"
   Adodc1.Refresh
  含义:查询功能共计三项 时间段、text1、text2
问题点:当时间条件满足text1、text2为空时查找结果为空
修改:当时间条件满足text1、text2为空时 只查询符合时间条件的内容,text1、text2任意一个为空时不进行条件查询
最佳答案
2013-6-16 11:10
  1. Public Sub Command1_Click()

  2. Dim strSql As String
  3. Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path & "\入库信息管理系统.mdb;Persist Security Info=False"
  4. strSql = "select * from 天津北特 where 纳品日期 between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "#"

  5. If Text1 <> "" And Text2 <> "" Then
  6. strSql = strSql & "and 车种='" & Text1.Text & "' and LOT ='" & Text2.Text & "'"
  7. End If

  8. Adodc1.RecordSource = strSql
  9. Adodc1.Refresh

  10. Set DataGrid1.DataSource = Adodc1
  11. DataGrid1.Refresh
  12. DataGrid1.Columns(0).Width = 1200
  13. DataGrid1.Columns(1).Width = 1200
  14. DataGrid1.Columns(2).Width = 1200
  15. DataGrid1.Columns(3).Width = 1200
  16. DataGrid1.Columns(4).Width = 1200
  17. DataGrid1.Columns(5).Width = 1200
  18. DataGrid1.Columns(6).Width = 2400
  19. End Sub
复制代码

frm(完成).rar

246.53 KB, 下载次数: 28

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
 楼主| 发表于 2013-6-16 09:57 | 显示全部楼层
text1、text2任意一个为空时不作为条件查询
回复

使用道具 举报

发表于 2013-6-16 09:57 | 显示全部楼层
  1. Public Sub Command1_Click()
  2.     Adodc1.ConnectionString = "provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path & "\入库信息管理系统.mdb;persist Security Info=False"
  3.     Adodc1.RecordSource = "select * from 天津北特"        '连接货品入库数据表
  4.     Adodc1.RecordSource = "select * from 天津北特 where 纳品日期 between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "#"
  5.     If Len(text1.Text) And Len(text2.Text) Then
  6.         Adodc1.RecordSource = "select * from 天津北特 where 车种 =" & "'" & text1.Text & "'"
  7.         Adodc1.RecordSource = "select * from 天津北特 where LOT =" & "'" & text2.Text & "'"
  8.         Adodc1.Refresh
  9.     End If
  10. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2013-6-16 10:04 | 显示全部楼层
不行呀!这样编辑text1、2就完全没有什么意义了,text1、text2任意一个为空时不作为条件查询
回复

使用道具 举报

 楼主| 发表于 2013-6-16 10:11 | 显示全部楼层
举个简单的例子,当时间条件有了Text1 Text2为空查询的结果是满足时间段的结果,如果时间段条件有了Text1条件有了Text2为空时,查询的结果应该是满足时间段和Text1的结果,如果时间段条件有了Text2条件有了Text1为空时,查询的结果应该是满足时间段和Text2的结果,如果三个条件全有那么就完全查找结果了,
回复

使用道具 举报

发表于 2013-6-16 10:42 | 显示全部楼层
你用的是ADO不?
回复

使用道具 举报

 楼主| 发表于 2013-6-16 10:58 | 显示全部楼层
好像是!具体我不懂
回复

使用道具 举报

 楼主| 发表于 2013-6-16 10:59 | 显示全部楼层
我窗体控件adodc
回复

使用道具 举报

发表于 2013-6-16 11:10 | 显示全部楼层    本楼为最佳答案   
  1. Public Sub Command1_Click()

  2. Dim strSql As String
  3. Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path & "\入库信息管理系统.mdb;Persist Security Info=False"
  4. strSql = "select * from 天津北特 where 纳品日期 between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "#"

  5. If Text1 <> "" And Text2 <> "" Then
  6. strSql = strSql & "and 车种='" & Text1.Text & "' and LOT ='" & Text2.Text & "'"
  7. End If

  8. Adodc1.RecordSource = strSql
  9. Adodc1.Refresh

  10. Set DataGrid1.DataSource = Adodc1
  11. DataGrid1.Refresh
  12. DataGrid1.Columns(0).Width = 1200
  13. DataGrid1.Columns(1).Width = 1200
  14. DataGrid1.Columns(2).Width = 1200
  15. DataGrid1.Columns(3).Width = 1200
  16. DataGrid1.Columns(4).Width = 1200
  17. DataGrid1.Columns(5).Width = 1200
  18. DataGrid1.Columns(6).Width = 2400
  19. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2013-6-16 11:28 | 显示全部楼层
这几编辑虽然可以使用,但是Text还是不能配合时间段条件单一使用,如果说text1、text2配合时间段条件进行单一查找怎么修改呀!
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-3 16:14 , Processed in 0.157610 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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