Excel VBA从Access数据库中选择记录未正确拉取

时间:2015-04-09 16:52:31

标签: 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 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

1 个答案:

答案 0 :(得分:1)

你有一个名为Date的字段,尝试重命名并重新编写代码,就像在第一个实例中那样是一个保留字,对初学者来说是一个坏主意!

使用日期时,请参阅Allen Browne对此事项的评论以保持一致性; http://allenbrowne.com/ser-36.html

你的日期被声明为字符串,但是在你的SQL查询中,你用'不是#来包围它们。应阅读;

Date Between " & "#" & pastdate & "# " & "and" & " #" & currentdate & "#"

以上所有内容都应该排除你的意见,如果没有评论,我会仔细看看你!