Results 1 to 2 of 2

Excel/SQL Hybrid - Run-time Error: Data type mismatch in criteria expression

Threaded View

Sleepyshy Excel/SQL Hybrid - Run-time... 03-11-2016, 12:38 PM
Sleepyshy Re: Excel/SQL Hybrid -... 03-11-2016, 05:43 PM
  1. #1
    Registered User
    Join Date
    11-30-2012
    Location
    alberta, canada
    MS-Off Ver
    Excel 2010
    Posts
    39

    Excel/SQL Hybrid - Run-time Error: Data type mismatch in criteria expression

    Hello!

    I am working on a database for entering and pulling old invoices. I am essentially using the excel document as a sql database and limiting user imput on the cells themself to avoid error as many of the people that will use it like to delete things.

    How the document works:
    The users can select fields from the Combo Boxes that will help them limit their searches. One of these limiting factors is a date range from range 1 to range 2. This is the only thing that I seem to have issues with:

    Private Sub cmdShowData_Click()
    Dim intSQL As Integer
    Dim rng As Range
    Dim strHyper As String
    
        'populate data
        strSQL = "SELECT * FROM [data$] WHERE "
        If cmbProducts.Text <> "" Then
            strSQL = strSQL & " [Company]='" & cmbProducts.Text & "'"
        End If
        
        If cmbID.Text <> "" Then
            If strSQL <> "SELECT * FROM [data$] WHERE " Then
                strSQL = strSQL & " AND [Invoice#]='" & cmbID.Text & "'"
            Else
                strSQL = strSQL & " [Invoice#]='" & cmbID.Text & "'"
            End If
        End If
        
        If cbAccount.Text <> "" Then
            If strSQL <> "SELECT * FROM [data$] WHERE " Then
                strSQL = strSQL & " AND [Account#]='" & cbAccount.Text & "'"
            Else
                strSQL = strSQL & " [Account#]='" & cbAccount.Text & "'"
            End If
        End If
        
        ' THIS IS WHAT IS CAUSING MY GRIEF
        If tbDate1.Text <> "" Then
            If strSQL <> "SELECT * FROM [data$] WHERE " Then
                strSQL = strSQL & " AND [Date]>='" & WorksheetFunction.Text(tbDate1.Value, "yyyy-mm-dd hh:MM:ss") & "' AND [Date]<='" & WorksheetFunction.Text(tbDate2.Value, "yyyy-mm-dd hh:MM:ss") & "'"
            Else
                strSQL = strSQL & " [Date]>='" & WorksheetFunction.Text(tbDate1.Value, "yyyy-mm-dd hh:MM:ss") & "' AND [Date]<='" & WorksheetFunction.Text(tbDate2.Value, "yyyy-mm-dd hh:MM:ss") & "'"
            End If
        End If
        
        If cmbProducts.Text <> "" Or cmbID.Text <> "" Or cbAccount.Text <> "" Or tbDate1.Text <> "" Then
            'now extract data
            closeRS
            
            OpenDB
            
            MsgBox strSQL
            
            ' THIS IS WHERE THE ERROR OCCURS
            rs.Open strSQL, cnn, adOpenKeyset, adLockOptimistic
            If rs.RecordCount > 0 Then
                Sheets("Search").Visible = True
                Sheets("Search").Select
                Range("dataSet").Select
                Range(Selection, Selection.End(xlDown)).ClearContents
                
                'Now putting the data on the sheet
                ActiveCell.CopyFromRecordset rs
            Else
                MsgBox "I was not able to find any matching records.", vbExclamation + vbOKOnly
                Exit Sub
            End If
        End If
    
    Set rng = Sheet1.Range("A7")
    
    Do Until rng.Value = ""
        If rng.Offset(0, 6).Value <> "" Then
            strHyper = rng.Offset(0, 6).Value
            rng.Offset(0, 6).Value = "=HYPERLINK(" & Chr(34) & strHyper & Chr(34) & "," & Chr(34) & "Invoice" & Chr(34) & ")"
        End If
        Set rng = rng.Offset(1, 0)
    Loop
    
    'Set rng = Sheet1.Range(Sheet1.Range("A7"), rng.Offset(0, 6))
    
    'Sheet1.Sort.SortFields.Clear
    'Sheet1.Sort.SortFields.Add Key:=Range("A7"), SortOn:=xlSortOnValues, Order:=xlDescending, _
    '    DataOption:=xlSortNormal
    'With Sheet1.Sort
    '    .SetRange rng
    '    .Header = xlGuess
    '    .MatchCase = False
    '    .Orientation = xlTopToBottom
    '    .SortMethod = xlPinYin
    '    .Apply
    'End With
    
    End
    End Sub
    To my understanding it is having trouble taking the dates entered in the cells and using it in SQL. It works just fine if you leave the date range blank, it is just when you use it as a search criteria.

    The range however is extremely important so I cannot do without.

    Let me know if you have any questions and I will do my best to answer them!
    Attached Files Attached Files
    Last edited by Sleepyshy; 03-11-2016 at 05:44 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Run-Time Type 13 Mismatch Error help - Excel VBA
    By ShakJames in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-08-2015, 06:19 AM
  2. error 3464 data type mismatch in criteria expression in CurrentDb.Execute update
    By SKooLZ in forum Access Programming / VBA / Macros
    Replies: 1
    Last Post: 07-12-2015, 09:05 AM
  3. Excel VBA Run-time error '13' Type mismatch
    By svibuk in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-01-2014, 04:23 AM
  4. Excel VBA Run-time error '13' Type mismatch
    By pankajpm in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 12-27-2013, 05:34 AM
  5. [SOLVED] Excel VBA Run-time error '13' Type mismatch
    By mackypogi in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-17-2013, 02:15 AM
  6. Excel VBA Run-time error '13' Type mismatch
    By edgaryp in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-01-2013, 06:48 PM
  7. [SOLVED] Data Type Mismatch in Criteria Expression
    By williams485 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 10-15-2012, 10:26 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1