0

I am trying to import specific records from Access Tables to Excel spreadsheets based on a particular Date criteria. The code fails when it tries to execute the sql statement and the error message says

"DataType mismatch"

I have explored as many ways as I could think of or what I could find online to set the data type for the date but nothing seems to work. After modifying the code as it is now, I was able to get rid off the error message but code does not recognize the data in the access table. Any help will be greatly appreciated. Apologies in advance if my question does not make any sense. Trying to get my head around this being a newbie developer... Thanks for your patience.

Public Sub ImportData()

Application.ScreenUpdating = False

'
'       Initialize shtArray (Public Array)
'
        With ThisWorkbook
            shtArray = Array(.Sheets("shtDom"))
        End With

'
'       Initialize tblArray (Public Array)
'
        tblArray = Array("tbl_DOM")


        Dim con As ADODB.Connection
        Dim rs As ADODB.Recordset
        Dim dbPath As String
        Dim SQL As String
        Dim i As Integer
        Dim sht As Worksheet, lastRow As Long
        Dim dtDom As String, dtObk As String

        If Weekday(frmInterface.dtPickDomestic, vbMonday) = 1 Then
            dtDom = Format(frmInterface.dtPickDomestic - 3, "dd/mm/yyyy")
        Else
            dtDom = Format(frmInterface.dtPickDomestic - 1, "dd/mm/yyyy")
        End If

        If Weekday(frmInterface.dtPickOtherBanks, vbMonday) = 1 Then
            dtObk = Format(frmInterface.dtPickOtherBanks - 3, "dd/mm/yyyy")
        Else
            dtObk = Format(frmInterface.dtPickOtherBanks - 1, "dd/mm/yyyy")
        End If


        dbPath = ThisWorkbook.Path & "\DOMESTIC SETTLEMENTS.mdb"
        Set con = New ADODB.Connection
        con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath

        For i = LBound(tblArray) To UBound(tblArray)

            Select Case i
                Case 0, 8, 9
                    SQL = "SELECT * FROM " & tblArray(i) & " WHERE [BAL_DATE] = #" & dtDom & "#"
                Case 10, 11, 12, 13
                    'SQL = "SELECT * FROM " & tblArray(i) & " WHERE [BAL_DATE] = #" & dtObk & "#"
                Case Else
                    GoTo continue
            End Select                    
                Set sht = shtArray(i)
                lastRow = sht.Cells(Rows.count, 1).End(xlUp).Offset(1, 0).Row
                Set rs = New ADODB.Recordset
                rs.Open SQL, con                    
                    If rs.EOF And rs.BOF Then
                        rs.Close
                        con.Close
                        Set rs = Nothing
                        Set con = Nothing

                        MsgBox "No records!!!", vbCritical
                        Exit Sub
                    End If
                shtArray(i).Range("A" & lastRow).CopyFromRecordset rs                    
                rs.Close
                Set rs = Nothing
continue:
        Next i        
            con.Close
            Set con = Nothing
            Set sht = Nothing

        On Error GoTo 0
        Exit Sub
errHandler:
        Set rs = Nothing
        Set con = Nothing
End Sub
10
  • Show your code. Commented Apr 11, 2017 at 7:57
  • Sorry.. missed putting the code in there... just added Commented Apr 11, 2017 at 7:59
  • And your problem now is DataType mismatch on rs.Open SQL, con or what? Commented Apr 11, 2017 at 8:03
  • No, the code actually runs through and executes the if statement right after rs.Open SQL, con, which means it does not recognize the data that is there in tbl_DOM with requested date being there under BAL_DATE column Commented Apr 11, 2017 at 8:12
  • Do NOT create SQL commands concatenating string, use parameters. Commented Apr 11, 2017 at 8:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.