ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 624|回复: 0

[求助] VBA查询Access数据库导出的数据不完整问题求指点

[复制链接]

TA的精华主题

TA的得分主题

发表于 2018-10-18 17:09 | 显示全部楼层 |阅读模式
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
我从ACCESS数据库文件中使用SQL查询导出的表格信息不完整,主要有两个问题:
1、比如数据库中有1000行数据,但是查询导出的数据只有800多行,有差异
2、SQL语句中我限定了从数据库中查询的End_Date列的日期只能大于等于查询当天的时间,但是查询出来的结果还是会有很多是小于这个日期的。
以上两个问题需要请论坛中的高手指点,不知道问题出在哪里,非常感谢!


代码如下:(在宏文件中是鼠标邮件运行这个代码)
Public Sub Export1()
Dim DBS As Database
Dim rsttemp1 As Recordset
Dim rsttemp As Recordset
Dim rsttemp2 As Recordset
Dim RSTTEMP9 As Recordset
Dim rst As Recordset
Dim qdf As QueryDef
Dim StrSQL, STRSQL1 As String
Dim Date1 As Date
Dim Date2 As Date
Dim datestr As Variant
Date1 = DTPicker1
Date2 = DTPicker2
Dim st As Variant
st = UserForm13.ComboBox1.Value
Dim re As Variant
Dim Pr As Variant
Dim Ci As Variant
Dim di As Variant
Dim dep As Variant
Dim id As Variant
Dim Sn As Variant
Dim pt As Variant
Dim tb As Variant
Dim k1 As Variant
Dim tp As Variant
Dim i As Variant
Dim List1
Dim filepath As String
Dim datafilename As String
'USERID = Get_User_Name
'filepath = "C:\Users\" & USERID & "\Desktop\"
filepath = "C:\YPTemplate\"
  
'datafilename = "信息清单" & Format(Date, "yyyy-m-d ") & Format(Time, "hh.mm.ss")
datafilename = "当前正在生效清单"

datestr = VBA.Date
re = UserForm13.Label11.Caption
Pr = UserForm13.Label12.Caption
Ci = UserForm13.Label13.Caption
Sn = UserForm13.Label21.Caption
st = UserForm13.ComboBox1.Value

Connect_Original_Database
Set DBS = OpenDatabase(myfile)
If UserForm13.ComboBox1.Value = "" Then
   MsgBox "请输店号!", , "系统提示"
Exit Sub
End If
If UserForm13.ComboBox1.Value <> "" Then
   StrSQL = "SELECT ID as 序号,Source_ID,Tab as 目录,PD_Name as 品名,Sheet as 类型,Type as 类别,规格_L1 as 规格, Div as 区划, Dept as 部门, YP_Promotion_Type as 形式,PR_Description as 简述,Start_Date as 开始,End_Date as 结束,(除XX) as 除外 FROM PRD_Data where  (Send like '*全国*' OR Send like '*" & re & "*' OR Send like '*" & Pr & "*' OR Send like '*" & Ci & "*' OR Send like '*" & st & "*') and (isnull(Omit) or not (Omit like '*" & re & "*' or Omit like '*" & Pr & "*' or Omit like '*" & Ci & "*' or Omit like '*" & st & "*')) and (End_Date>= " & datestr & ")"
End If
Set qdf = DBS.CreateQueryDef("", StrSQL)
Set rsttemp1 = qdf.OpenRecordset
If rsttemp1.RecordCount = 0 Then
    MsgBox "无此记录!", , "系统提示"
Else
On Error Resume Next
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Workbooks.Add
  Sheets(1).Select

ActiveSheet.UsedRange.Offset(1).Delete
   
For i = 1 To rsttemp1.Fields.Count

Cells(1, i) = rsttemp1.Fields(i - 1).Name
Next
[a2].CopyFromRecordset rsttemp1
End If
    Cells.Select
    Cells.EntireColumn.AutoFit
   
    Range("A1:N1").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark2
        .TintAndShade = -0.249977111117893
        .PatternTintAndShade = 0
    End With
   
    Cells.Select
    With Selection.Font
        .Size = 9
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .TintAndShade = 0
    End With
   
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
        
    Cells.Select
    Selection.ColumnWidth = 8.88
        
        
  ActiveWorkbook.SaveAs (filepath & datafilename & ".xlsx")
  Workbooks(datafilename & ".xlsx").Close
   
  MsgBox "正在生效记录已下载至文件夹!", , "系统提示"
  
DBS.Close
Set rsttemp1 = Nothing
Set DBS = Nothing
On Error GoTo 0

End Sub
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-24 08:17 , Processed in 0.033024 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

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