+ Reply to Thread
Results 1 to 10 of 10

Query result: Data type mismatch

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-26-2013
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    367

    Query result: Data type mismatch

    Hello,

    I'm getting Data type mismatch for the below query, seeking for your advises please.

    
    SqlQuery = "SELECT COUNT(*) AS Report_ID,q1.Report_Name,q1.Report_Owner FROM (SELECT Report_ID,Report_Name,Report_Owner FROM tbl_Monday UNION ALL SELECT Report_ID,Report_Name,Report_Owner FROM tbl_Tuesday UNION ALL SELECT Report_ID,Report_Name,Report_Owner FROM tbl_Wednesday) q1 INNER JOIN(SELECT Report_ID,Report_Name,Report_Owner FROM tbl_Monday UNION ALL SELECT Report_ID,Report_Name,Report_Owner FROM tbl_Tuesday UNION ALL SELECT Report_ID,Report_Name,Report_Owner FROM tbl_Wednesday) q2 on CLng(Val(q1.Report_ID)) + q1.Report_Name + q1.Report_Owner >= CLng(Val(q2.Report_ID)) + q2.Report_Name + q2.Report_Owner GROUP BY q1.Report_ID,q1.Report_Name,q1.Report_Owner"
    
    rs.Open SqlQuery, conn
    
            With rs
            
                Do While Not .EOF
                    Set litem = frm_ALLREPORTS.lv_ALLREPORTS.ListItems.Add(, , .Fields("Report_ID").Value)
                    litem.SubItems(1) = .Fields("Report_Name").Value
                                    litem.SubItems(2) = .Fields("Report_Owner").Value
    
    
    .MoveNext
                Loop
                
            End With
    
    conn.Close
    Thanks in advance Gurus!

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Query result: Data type mismatch

    Where do you get the error?
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    02-26-2013
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    367

    Re: Query result: Data type mismatch

    I think on this part Norie

    Actually, the original code is in SQL script, I've tried substituing CLng instead of Cast due to access cannot read Cast function.

    CLng(Val(q1.Report_ID)) + q1.Report_Name + q1.Report_Owner >= CLng(Val(q2.Report_ID))
    btw, Report_ID is my Primary key.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Query result: Data type mismatch

    Does that query run when you try and execute it manually in the database?

    Does the database you are querying support the functions CLng and Val?

  5. #5
    Forum Contributor
    Join Date
    02-26-2013
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    367

    Re: Query result: Data type mismatch

    It doesn't run coz' the query seems incorrect. Yes, access database support CLng and Val functions.

    The original code was: (It is in MySQL script)
    
    SqlQuery = "SELECT COUNT(*) AS Report_ID,q1.Report_Name,q1.Report_Owner FROM (SELECT Report_ID,Report_Name,Report_Owner FROM tbl_Monday UNION ALL SELECT Report_ID,Report_Name,Report_Owner FROM tbl_Tuesday UNION ALL SELECT Report_ID,Report_Name,Report_Owner FROM tbl_Wednesday) q1 INNER JOIN(SELECT Report_ID,Report_Name,Report_Owner FROM tbl_Monday UNION ALL SELECT Report_ID,Report_Name,Report_Owner FROM tbl_Tuesday UNION ALL SELECT Report_ID,Report_Name,Report_Owner FROM tbl_Wednesday) q2 on Cast(q1.Report_ID as Integer) + q1.Report_Name + q1.Report_Owner >= Cast(q2.Report_ID as Integer) + q2.Report_Name + q2.Report_Owner GROUP BY q1.Report_ID,q1.Report_Name,q1.Report_Owner"
    My problem in that code is that Cast function is not supported in ms access and I've search in the internet that CLng, CInt and Val functions can substitute for Cast, but unfortunately, it still gives me an error

    for the below code
    Cast(q1.Report_ID as Integer) + q1.Report_Name + q1.Report_Owner >= Cast(q2.Report_ID as Integer) + q2.Report_Name + q2.Report_Owner
    i've converted it to CLng or CInt, but still it doesn't work.
    
    CInt(Val(q1.Report_ID)) + q1.Report_Name + q1.Report_Owner >= CInt(Val(q2.Report_ID)) + q2.Report_Name + q2.Report_Owner
    CLng(Val(q1.Report_ID)) + q1.Report_Name + q1.Report_Owner >= CLng(Val(q2.Report_ID))
    Last edited by puuts; 02-09-2015 at 01:23 PM.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Query result: Data type mismatch

    Why are you using Val and CInt?

    Have you tried just using one of them?

    Also, what happens if you don't use either of them?

  7. #7
    Forum Contributor
    Join Date
    02-26-2013
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    367

    Re: Query result: Data type mismatch

    still get the same error. Either I've use only Val,CLng or CInt.

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Query result: Data type mismatch

    Where exactly do you get the error?

  9. #9
    Forum Contributor
    Join Date
    02-26-2013
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    367

    Re: Query result: Data type mismatch

    It jumps on my Errohandler when entering on this line:

    rs.Open SqlQuery, conn
    My whole code is:
    
    Sub ALL_DATA_REPORTS()
    
    
    On Error GoTo ErrorHandler
    
    OPEN_DATABASE
    
    SqlQuery = "SELECT COUNT(*) AS Report_ID,q1.Report_Name,q1.Report_Owner FROM (SELECT Report_ID,Report_Name,Report_Owner FROM tbl_Monday UNION ALL SELECT Report_ID,Report_Name,Report_Owner FROM tbl_Tuesday UNION ALL SELECT Report_ID,Report_Name,Report_Owner FROM tbl_Wednesday) q1 INNER JOIN(SELECT Report_ID,Report_Name,Report_Owner FROM tbl_Monday UNION ALL SELECT Report_ID,Report_Name,Report_Owner FROM tbl_Tuesday UNION ALL SELECT Report_ID,Report_Name,Report_Owner FROM tbl_Wednesday) q2 on CLng(q1.Report_ID) + q1.Report_Name + q1.Report_Owner >= CLng(q2.Report_ID) + q2.Report_Name + q2.Report_Owner GROUP BY q1.Report_ID,q1.Report_Name,q1.Report_Owner"
    
    
    rs.Open SqlQuery, conn
    
            With rs
            
                Do While Not .EOF
                    Set litem = frm_ALLREPORTS.lv_ALLREPORTS.ListItems.Add(, , .Fields("Report_ID").Value)
                    litem.SubItems(1) = .Fields("Report_Name").Value
                                    litem.SubItems(2) = .Fields("Report_Owner").Value
                                    'litem.SubItems(3) = .Fields("Function").Value
                                    'litem.SubItems(4) = .Fields("Region").Value
                                    'litem.SubItems(5) = .Fields("Comment").Value
                                    'litem.SubItems(6) = .Fields("Cut_Off").Value
                                    'litem.SubItems(7) = .Fields("Reminder_Time").Value
                                    'litem.SubItems(8) = .Fields("Report_Day").Value
                .MoveNext
                Loop
                
            End With
    
    conn.Close
    
    Exit Sub
    
    ErrorHandler:
    msg = MsgBox(" Error: " & Err.Description, vbCritical + vbOKOnly, "ERROR FOUND!")
    
    
    End Sub

  10. #10
    Forum Contributor
    Join Date
    02-26-2013
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    367

    Re: Query result: Data type mismatch

    I've fix it now. I've used CStr() function instead of CLng,Val or CInt since varchar is used in the original script.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Type Mismatch when exporting query to Excel
    By Frankie_The_Flyer in forum Access Programming / VBA / Macros
    Replies: 1
    Last Post: 09-23-2013, 07:53 PM
  2. Type mismatch in macro query
    By ill_comms in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-22-2011, 02:35 AM
  3. Type Mismatch when running query on SQL Database
    By Neily in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-08-2005, 11:05 AM
  4. [SOLVED] Database Query -- Data Type Mismatch
    By StephenP in forum Excel General
    Replies: 0
    Last Post: 04-14-2005, 03:06 PM
  5. [SOLVED] MS Query of Excel Datalist -- Data type mismatch
    By StephenP in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-14-2005, 02:06 PM

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