+ Reply to Thread
Results 1 to 9 of 9

SQL Query in VBA throwing mismatch error

Hybrid View

  1. #1
    Registered User
    Join Date
    08-16-2010
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2007
    Posts
    27

    SQL Query in VBA throwing mismatch error

    I am getting data type mismatch in criteria error in this code..

    please help

        Application.ScreenUpdating = False
        Application.ScreenUpdating = False
        graph3.Unprotect
        Dim winchoice, tierchoice, originalchoice, reg_terr_choice, decilechoice As String
        Graph_data.Range("graph_r1").Clear
        If terr_master_list.Range("pivotfield_calledon").Value = "All" Then
            winchoice = "1, 2"
        Else
            winchoice = terr_master_list.Range("pivotfield_calledon").Value
        End If
        If terr_master_list.Range("pivotfield_decile").Value = "All" Then
            decilechoice = "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10"
        Else
            decilechoice = terr_master_list.Range("pivotfield_decile").Value
        End If
        If terr_master_list.Range("pivotfield_original").Value = "All" Then
            originalchoice = "'Y', 'N'"
        Else
            originalchoice = " '" & terr_master_list.Range("pivotfield_original").Value & "' "
        End If
        If terr_master_list.Range("tierchoice").Value = "All" Then
            tierchoice = "1, 2"
        Else
            tierchoice = terr_master_list.Range("tierchoice").Value
        End If
        If terr_master_list.Range("pivotfield_Region").Value = "All" Then
            reg_terr_choice = "5410, 5420, 5430"
        ElseIf terr_master_list.Range("graph3_1_5choice").Value <= 4 Then
            reg_terr_choice = terr_master_list.Range("pivotfield_Region").Value
        Else
            reg_terr_choice = " '" & terr_master_list.Range("pivotfield_Region").Value & "' "
        End If
            Data_Range = GetDataRange_Query([Graph_data], Range(Range(Range("graph_dataStart"), Range("graph_dataStart").Offset(5000, 0).End(xlUp)), Range(Range("graph_dataStart"), Range("graph_dataStart").End(xlToRight))))
        If terr_master_list.Range("pivotfield_Region").Value = "All" Or terr_master_list.Range("pivotfield_Region").Value = 5410 Or terr_master_list.Range("pivotfield_Region").Value = 5420 Or terr_master_list.Range("pivotfield_Region").Value = 5430 Then
            strsql = "Select * From " & Data_Range & " Where [Region_Code] in (" & reg_terr_choice & ") AND  [Decile] in (" & decilechoice & ") AND  [Win_Called_on] in (" & winchoice & ") AND [Account_Tier] in (" & tierchoice & ") AND [Original_Account] in (" & originalchoice & ");"
        Else
            strsql = "Select * From " & Data_Range & " Where [Territory_Code] in (" & reg_terr_choice & ") AND  [Decile] in (" & decilechoice & ") AND  [Win_Called_on] in (" & winchoice & ") AND [Account_Tier] in (" & tierchoice & ") AND [Original_Account] in (" & originalchoice & ");"
        End If
        PasteSQl strsql, [graph_strt]
        graph3.Protect AllowSorting:=True, AllowFiltering:=True
    Last edited by pike; 04-06-2011 at 06:07 AM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,324

    Re: VBA Query help

    It may help others to help you if you uploaded a sample workbook with some typical data and the code in context.

    You don't say where the error occurs and it cannot be tested without the data and structure of the worksbook/sheets.

    That said,

    Dim winchoice, tierchoice, originalchoice, reg_terr_choice, decilechoice As String

    only Dim's decilechoice As String; the other variables are type variant. You need to specify each variable individually (although it does not need individual Dim statements)


    Regards
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    08-16-2010
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re:

    the error is obtained while performing this step

    
    PasteSQl strsql, [graph_strt]
    check the paste sql subroutine below

    Sub GetConnection()
    
        Dim strPath As String
        
        Set Cn = New ADODB.Connection
       
        strPath = ThisWorkbook.Path & "\" & ThisWorkbook.Name
    
        With Cn
            .Provider = "MSDASQL"
            .ConnectionString = "Driver={Microsoft Excel Driver (*.xls)};" & "DBQ=" & strPath & "; ReadOnly=false; MaxScanRows= 0;"
            .Open
        End With
        
    End Sub
    
    Public Sub PasteSQl(strsql As String, strrange As Range)
        strrange.Worksheet.Unprotect
        GetConnection
            
        Dim rsADO As New ADODB.Recordset
    
        rsADO.Open strsql, Cn
    
        If rsADO.RecordCount = 0 Then MsgBox "No Data Found", vbInformation
       
        If Not rsADO.EOF Then
        strrange.CopyFromRecordset rsADO
        End If
        
        Set rsADO = Nothing
        Set Cn = Nothing
    
    End Sub
    Last edited by kishen1912; 04-06-2011 at 06:09 AM.

  4. #4
    Registered User
    Join Date
    08-16-2010
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: VBA Query help

    i am getting the error at this section

    rsADO.Open strsql, Cn

  5. #5
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: VBA Query help

    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution. Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.
    To change a Title on your post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.

    PLEASE PM WHEN YOU HAVE DONE THIS AND I WILL DELETE THIS POST
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,324

    Re: VBA Query help

    @kishen1912: please follow Pike's instructions or you will not get any assistance.

    A suitable titke might be "Data mismatch in ADODB Connection"

    Also note that, although you have provided more code, there is nothing to test it with. Please post a workbook.

    I, for one, will not try to generate test data in order to find a fault in your code.

    Regards

  7. #7
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: SQL Query in VBA throwing mismatch error

    Hi kishen1912
    Or even Query data type mismatch in criteria error would help other search for a similar problem
    I change it

  8. #8
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: SQL Query in VBA throwing mismatch error

    Thanks for taking the time to comply with the rule .

  9. #9
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: SQL Query in VBA throwing mismatch error

    hi, kishen1912, your SQL string is wrong, when you use IN you can follow it either by sub select statement or data array separated by coma

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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