SMORF
SMORF

Reputation: 499

Run-time error '13': Type mismatch in SQL code

I have a 'Run-time error '13': Type mismatch' for part of my SQL code in VBA.

The code the error refers to is:

SQL = SQL & "   (select unique sv.gps_planshpdate from oes_delsegview sv, oes_opos op, oes_oposdelseg ds, oes_nrbom nb, ncf_comppart cp where cp.item_part_no = pd.part_no and nb.c_catnr = cp.catnr and nb.c_prodtyp = cp.prodtyp and nb.c_packtyp = cp.packtyp and nb.c_vernr = cp.vernr " & _
SQL = SQL & "       and sv.ordnr = ds.ordnr and sv.posnr = ds.posnr and sv.catnr = nb.p_catnr and sv.prodtyp = nb.p_prodtyp and sv.packtyp = nb.p_packtyp and op.ordnr = ds.ordnr and op.posnr = ds.posnr and op.catnr = nb.p_catnr and op.prodtyp = nb.p_prodtyp and op.packtyp = nb.p_packtyp " & _
SQL = SQL & "       and op.vernr = nb.p_vernr and nb.active = 'Y' and op.ol_typ in ('XX','CO') and sv.gps_planshpdate = " & _
SQL = SQL & "           (select max(sv1.gps_planshpdate) from oes_delsegview sv1, oes_opos op1, oes_oposdelseg ds1, oes_nrbom nb1, ncf_comppart cp1 where cp1.item_part_no = cp.item_part_no and nb1.c_catnr = cp1.catnr " & _
SQL = SQL & "               and nb1.c_prodtyp = cp1.prodtyp and nb1.c_packtyp = cp1.packtyp and nb1.c_vernr = cp1.vernr and sv1.ordnr = ds1.ordnr and sv1.posnr = ds1.posnr and sv1.catnr = nb1.p_catnr and sv1.prodtyp = nb1.p_prodtyp and sv1.packtyp = nb1.p_packtyp " & _
SQL = SQL & "               and op1.ordnr = ds1.ordnr and op1.posnr = ds1.posnr and op1.catnr = nb1.p_catnr and op1.prodtyp = nb1.p_prodtyp and op1.packtyp = nb1.p_packtyp and op1.vernr = nb1.p_vernr and nb1.active = 'Y' and sv1.ord_o_status = '9' and op1.ol_typ in ('XX','CO'))), " 'last_ship_date_manufacturing

Any idea's how to run this code without the 'Type mismatch' error ?

Thanks

Upvotes: 1

Views: 2453

Answers (1)

David Zemens
David Zemens

Reputation: 53623

When I evaluate your expression, it returns FALSE which is certainly what is causing the mismatch error, as SQL expects a string query.

The problem is in your original method of concatenation, which is combining two things incorrectly.

If you want to do:

Sql = Sql & " some expression"
Sql = Sql & " some other expression"
'Etc.

Note that in the above, I did NOT continue the line, like you did:

Sql = Sql & " some expression" & _
Sql = Sql & " some other expression" 

That will raise an error. Visualize it thusly:

Sql = Sql & " A " & Sql = Sql & " B "

Or:

Sql = ((Sql & " A " & Sql) = (Sql & " B "))

The error raises because this expression logically evaluates to False: the right-side of this assignment statement is an equivalence statement, and since the two expressions in the equivalent statement are NOT equal, it can only return FALSE.

I have not made any changes to your query expression other than correcting the misuse of line-continuation characters:

Dim SQL As String

SQL = SQL & "   (select unique sv.gps_planshpdate from oes_delsegview sv, " & _
            "oes_opos op, oes_oposdelseg ds, oes_nrbom nb, ncf_comppart cp " & _
            "where cp.item_part_no = pd.part_no and nb.c_catnr = cp.catnr and " & _
            "nb.c_prodtyp = cp.prodtyp and nb.c_packtyp = cp.packtyp and nb.c_vernr = cp.vernr " & _
            "       and sv.ordnr = ds.ordnr and sv.posnr = ds.posnr and sv.catnr = nb.p_catnr " & _
            "and sv.prodtyp = nb.p_prodtyp and sv.packtyp = nb.p_packtyp and op.ordnr = ds.ordnr " & _
            "and op.posnr = ds.posnr and op.catnr = nb.p_catnr and op.prodtyp = nb.p_prodtyp and " & _
            "op.packtyp = nb.p_packtyp " & _
            "       and op.vernr = nb.p_vernr and nb.active = 'Y' and op.ol_typ in ('XX','CO') and " & _
            "sv.gps_planshpdate = " & _
            "           (select max(sv1.gps_planshpdate) from oes_delsegview sv1, oes_opos op1, " & _
            "oes_oposdelseg ds1, oes_nrbom nb1, ncf_comppart cp1 where cp1.item_part_no = " & _
            "cp.item_part_no and nb1.c_catnr = cp1.catnr " & _
            "               and nb1.c_prodtyp = cp1.prodtyp and nb1.c_packtyp = cp1.packtyp and " & _
            "nb1.c_vernr = cp1.vernr and sv1.ordnr = ds1.ordnr and sv1.posnr = ds1.posnr and " & _
            "sv1.catnr = nb1.p_catnr and sv1.prodtyp = nb1.p_prodtyp and sv1.packtyp = nb1.p_packtyp " & _
            "               and op1.ordnr = ds1.ordnr and op1.posnr = ds1.posnr and op1.catnr = " & _
            "nb1.p_catnr and op1.prodtyp = nb1.p_prodtyp and op1.packtyp = nb1.p_packtyp and op1.vernr " & _
            "= nb1.p_vernr and nb1.active = 'Y' and sv1.ord_o_status = '9' and op1.ol_typ in ('XX','CO'))), " 'last_ship_date_manufacturing


Debug.Print SQL

Upvotes: 1

Related Questions