2014-09-30 123 views
1

我有'運行時錯誤'13':類型不匹配'部分我的SQL代碼在VBA中。運行時錯誤'13':類型不匹配在SQL代碼

的代碼中的錯誤指的是:

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 

任何想法是如何沒有「類型不匹配」錯誤運行該代碼?

感謝

+0

當我評價表情,'SQL'返回一個布爾值,'FALSE'。 FIx字符串連接,以便它計算查詢語句,而不是布爾值。 – 2014-09-30 13:58:16

回答

1

當我計算表達式,它返回FALSE這肯定是什麼原因造成的失配誤差,如SQL預計串查詢。

問題在於您的原始串聯方法,它錯誤地組合了兩件事。

如果你想做的事:

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

注意,在上面的,我所做的繼續行了,像你這樣:

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

這將引發一個錯誤。正是如此它想象:

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

或者:

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

錯誤引發,因爲這種表達邏輯計算結果爲False:這個賦值語句的右側是一個等價語句,因爲在兩個表達式相當的說法是不是相等,它只能返回FALSE

我沒有比修正的續行字符的濫用之外的任何更改您的查詢表達式:

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 
+0

當然......現在非常有意義......謝謝! – SMORF 2014-09-30 14:07:57

相關問題