Sql Excel VBA从Access数据库中选择记录不正确
我有一个宏,它从Access数据库中提取数据,并根据输入到用户表单中的日期将记录集写入电子表格。但是,如果我输入“3/2/2105”和“3/5/2015”,它将返回3/2-3/5和3/20-3/31的所有记录。我想不出它为什么会这样做。如果有人能给我指出正确的方向/提出建议,我将不胜感激Sql Excel VBA从Access数据库中选择记录不正确,sql,vba,Sql,Vba,我有一个宏,它从Access数据库中提取数据,并根据输入到用户表单中的日期将记录集写入电子表格。但是,如果我输入“3/2/2105”和“3/5/2015”,它将返回3/2-3/5和3/20-3/31的所有记录。我想不出它为什么会这样做。如果有人能给我指出正确的方向/提出建议,我将不胜感激 Sub pullfrommsaccess() queryform.Show Dim conn As Object Dim rs As Object Dim AccessFil
Sub pullfrommsaccess()
queryform.Show
Dim conn As Object
Dim rs As Object
Dim AccessFile As String
Dim SQL As String
Dim startdate As String
Dim enddate As String
Dim i As Integer
Sheet2.Cells.Delete
Application.ScreenUpdating = False
AccessFile = ThisWorkbook.Path & "\" & "mdidatabase.accdb"
On Error Resume Next
Set conn = CreateObject("ADODB.connection")
If Err.Number <> 0 Then
MsgBox "Connection was not created!", vbCritical, "Connection Error"
Exit Sub
End If
On Error GoTo 0
conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & AccessFile
If tblname = "Attainments" Then
If shift1 = "1" Then
SQL = "SELECT [Line],[Area],[Shift],[Attainment Percentage],[Date] FROM " & tblname & " WHERE Shift='1' and Date Between " & "'" & pastdate & "' " & "and" & " '" & currentdate & "'"
End If
If shift2 = "2" Then
SQL = "SELECT [Line],[Area],[Shift],[Attainment Percentage],[Date] FROM " & tblname & " WHERE Shift='2' and Date Between " & "'" & pastdate & "' " & "and" & " '" & currentdate & "'"
End If
If shift1 = "1" And shift2 = "2" Then
SQL = "SELECT [Line],[Area],[Shift],[Attainment Percentage],[Date] FROM " & tblname & " WHERE Date Between " & "'" & pastdate & "' " & "and" & " '" & currentdate & "'"
End If
End If
If tblname = "MDItable" Then
If shift1misses = "1" Then
SQL = "SELECT [Date],[Area],[Shift],[Line],[Quantity],[Issue] FROM " & tblname & " WHERE Shift='1' and Date Between " & "'" & pastdatemisses & "' " & "and" & " '" & currentdatemisses & "'"
End If
If shift2misses = "2" Then
SQL = "SELECT [Date],[Area],[Shift],[Line],[Quantity],[Issue] FROM " & tblname & " WHERE Shift='2' and Date Between " & "'" & pastdatemisses & "' " & "and" & " '" & currentdatemisses & "'"
End If
If shift1misses = "1" And shift2misses = "2" Then
SQL = "SELECT [Date],[Area],[Shift],[Line],[Quantity],[Issue] FROM " & tblname & " WHERE Date Between " & "'" & pastdatemisses & "' " & "and" & " '" & currentdatemisses & "'"
End If
End If
On Error Resume Next
Set rs = CreateObject("ADODB.Recordset")
If Err.Number <> 0 Then
Set rs = Nothing
Set conn = Nothing
MsgBox "Recordset was not created!", vbCritical, "Recordset Error"
Exit Sub
End If
On Error GoTo 0
rs.CursorLocation = 3
rs.CursorType = 1
rs.Open SQL, conn
If rs.EOF And rs.BOF Then
rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
Application.ScreenUpdating = True
MsgBox "There are no records in the recordset!", vbCritical, "No Records"
Exit Sub
End If
For i = 0 To rs.Fields.Count - 1
Sheet2.Cells(1, i + 1) = rs.Fields(i).Name
Next i
'Copy From RecordSet to Excel and Reset
Sheet2.Range("A2").CopyFromRecordset rs
rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
MsgBox "The records from " & pastdate & " and " & currentdate & " were successfully retrieved from the '" & tblname & "' table!", vbInformation, "Done"
End If
Call TrimALL
End Sub
Sub-pullfrommsaccess()
查询表单显示
作为对象的Dim conn
将遥感器作为对象
将文件设置为字符串
将SQL设置为字符串
暗起始日期为字符串
Dim enddate作为字符串
作为整数的Dim i
Sheet2.Cells.Delete
Application.ScreenUpdating=False
AccessFile=ThisWorkbook.Path&“\”和“mdidatabase.accdb”
出错时继续下一步
Set conn=CreateObject(“ADODB.connection”)
如果错误号为0,则
MsgBox“未创建连接!”,vbCritical,“连接错误”
出口接头
如果结束
错误转到0
conn.Open“Provider=Microsoft.ACE.OLEDB.12.0;数据源=”&AccessFile
如果tblname=“成就”,则
如果shift1=“1”,则
SQL=“从“&tblname&”中选择[Line]、[Area]、[Shift]、[Acquisition Percentage]、[Date],其中Shift='1'和日期介于“&”&pastdate&“&”和“&”¤tdate&”之间”
如果结束
如果shift2=“2”,则
SQL=“从“&tblname&”中选择[Line]、[Area]、[Shift]、[Acquisition Percentage]、[Date],其中Shift='2'和日期介于“&”&pastdate&“&”和“&”¤tdate&”之间”
如果结束
如果shift1=“1”和shift2=“2”,则
SQL=“从“&tblname&”中选择[Line]、[Area]、[Shift]、[Acquisition Percentage]、[Date],其中日期介于“&”&pastdate&“&”和“&”¤tdate&”之间”
如果结束
如果结束
如果tblname=“MDItable”,则
如果shift1misses=“1”,则
SQL=“从“&tblname&”中选择[Date]、[Area]、[Shift]、[Line]、[Quantity]、[Issue],其中Shift='1'和日期介于“&”&pastdatemisses&“&”和“&”¤tdatemisses&”之间”
如果结束
如果shift2misses=“2”,则
SQL=“从“&tblname&”中选择[Date]、[Area]、[Shift]、[Line]、[Quantity]、[Issue],其中Shift='2'和日期介于“&”&pastdatemisses&“&”和“&”¤tdatemisses&”之间”
如果结束
如果shift1misses=“1”和shift2misses=“2”,则
SQL=“从“&tblname&”中选择[Date]、[Area]、[Shift]、[Line]、[Quantity]、[Issue],其中日期介于“&”&pastdatemisses&“&”和“&”¤tdatemisses&”之间”
如果结束
如果结束
出错时继续下一步
Set rs=CreateObject(“ADODB.Recordset”)
如果错误号为0,则
设置rs=无
设置连接=无
MsgBox“未创建记录集!”,vbCritical,“记录集错误”
出口接头
如果结束
错误转到0
rs.CursorLocation=3
rs.CursorType=1
康涅狄格州开放式SQL
如果是rs.EOF和rs.BOF,则
rs.Close
关闭连接
设置rs=无
设置连接=无
Application.ScreenUpdating=True
MsgBox“记录集中没有记录!”,vbCritical,“没有记录”
出口接头
如果结束
对于i=0到rs.Fields.Count-1
表2.单元格(1,i+1)=rs.字段(i).名称
接下来我
'从记录集复制到Excel并重置
表2.范围(“A2”).来自记录集rs的副本
rs.Close
关闭连接
设置rs=无
设置连接=无
MsgBox“已从”&tblname&“表!”中成功检索到来自“&pastdate&”和“¤tdate&”的记录,vbInformation,“完成”
如果结束
打电话给特里莫尔
端接头
如果有一个名为Date的字段,请尝试重命名该字段并重新编写代码,因为在第一个实例中,这是一个保留字,对初学者来说是个坏主意
在处理日期时,请参见艾伦·布朗对该问题的评论,以获得一致性;
您将日期声明为字符串,但在SQL查询中,您将日期用“not a#”括起来。它应该是:
Date Between " & "#" & pastdate & "# " & "and" & " #" & currentdate & "#"
以上所有这些都会让你分清头绪,如果没有评论的话,我会更仔细地看你 不相关,但我建议您放弃
alllowercase
,对本地人采用camelCase
命名约定,对过程名称采用PascalCase
。可读性++谢谢你的提示。我知道我很不擅长让我的代码变得漂亮,我是唯一一个在工作中看到它的人通常hahaI实现了代码,但它仍然不工作:/我认为名为Date的字段很好,因为它周围有括号,在过去的项目中工作得很好。谢谢你的帮助,我真的很感激!