# Visual Basic > Visual Basic .NET > VS 2019 [RESOLVED] Throws an error after adding one more extra field in the code!

## VS2013

Hi,

The below code is working fine with 2 fields:



```
Dim StrSql As String = "Select ST.CustNo As Cust_No, Sum(ST.Total) As Total_Amount,
                               IIf(IsNull(TotSol), 0, TotSol) As Amount_Paid, 
                               Sum(ST.Total) - IIf(IsNull(TotSol), 0, TotSol) As Balance_Amount
                        From SalesTable ST 
                        Left Join(select RT.CustNo, sum(RT.ReceivedAmt) As TotSol 
                                  from ReceiptsTable RT 
                                  Group by RT.CustNo
                        ) Q on ST.CustNo = Q.CustNo
                        GROUP BY ST.CustNo, 
                                 IIf(IsNull(TotSol), 0, TotSol)"

        Dim dt As New DataTable
        If Not DAL.ExecuteSql(dt, StrSql, Msg) Then MsgBox(Msg, MsgBoxStyle.Exclamation, "Error") : Exit Sub
        Me.dgvTable.DataSource = dt
        If dt Is Nothing Or dt.Rows.Count <= 0 Then
            Exit Sub
        End If
```

But when I added one more field CustName (*which is available in both tables SalesTable & ReceiptsTable*) to it, it throws an error. Please support. The following is the code:



```
Dim StrSql As String = "Select ST.CustNo As Cust_No, ST.CustName As Cust_Name, Sum(ST.Total) As Total_Amount,
                               IIf(IsNull(TotSol), 0, TotSol) As Amount_Paid, 
                               Sum(ST.Total) - IIf(IsNull(TotSol), 0, TotSol) As Balance_Amount
                        From SalesTable ST 
                        Left Join(select RT.CustNo,RT.CustName,sum(RT.ReceivedAmt) As TotSol 
                                  from ReceiptsTable RT 
                                  Group by RT.CustNo, RT.CustName
                        ) Q on ST.CustNo = Q.CustNo, ST.CustName = Q.CustName
                        GROUP BY ST.CustNo, St.CustName 
                                 IIf(IsNull(TotSol), 0, TotSol)"

        Dim dt As New DataTable
        If Not DAL.ExecuteSql(dt, StrSql, Msg) Then MsgBox(Msg, MsgBoxStyle.Exclamation, "Error") : Exit Sub
        Me.dgvTable.DataSource = dt
        If dt Is Nothing Or dt.Rows.Count <= 0 Then
            Exit Sub
        End If
```

*Note:* *The error is: JOIN expression not supported.*

----------


## dunfiddlin

> Hi,
> 
> The below code is working fine with 2 fields:
> 
> 
> 
> *Note:* *The error is: JOIN expression not supported.*


Self explanatory, really. Join combines two sources and *only* two sources.

----------


## techgnome

It's this:


```
Q on ST.CustNo = Q.CustNo, ST.CustName = Q.CustName
```

That's an improper join ...



```
Q on ST.CustNo = Q.CustNo AND ST.CustName = Q.CustName
```

Join clauses use the same structure as a WHERE clause. The use of "," is imperfect and ambiguous. Does it mean "AND" or "OR"? In your case, I think you're looking for an "AND"... so use that.


-tg

----------


## VS2013

Thanks for your kind reply. Now, it shows the below error:

Syntax error in query expression 'St.CustName IIf(IsNull(TotSol), 0, TotSol)'.

Looking forward to your kind reply. Thanks once again.

----------


## OptionBase1

Please post the updated query string.

----------


## VS2013

```
       Dim StrSql As String = "Select ST.CustNo As Cust_No, ST.CustName As Cust_Name, Sum(ST.Total) As Total_Amount,
                               IIf(IsNull(TotSol), 0, TotSol) As Amount_Paid, 
                               Sum(ST.Total) - IIf(IsNull(TotSol), 0, TotSol) As Balance_Amount
                        From SalesTable ST 
                        Left Join(select RT.CustNo,RT.CustName,sum(RT.ReceivedAmt) As TotSol 
                                  from ReceiptsTable RT 
                                  Group by RT.CustNo, RT.CustName
                        ) Q on ST.CustNo = Q.CustNo AND ST.CustName = Q.CustName
                        GROUP BY ST.CustNo, St.CustName 
                                 IIf(IsNull(TotSol), 0, TotSol)"

        Dim dt As New DataTable
        If Not DAL.ExecuteSql(dt, StrSql, Msg) Then MsgBox(Msg, MsgBoxStyle.Exclamation, "Error") : Exit Sub
        Me.dgvTable.DataSource = dt
        If dt Is Nothing Or dt.Rows.Count <= 0 Then
            Exit Sub
        End If
```

*Syntax error in query expression 'St.CustName IIf(IsNull(TotSol), 0, TotSol)'.*

----------


## jmcilhinney

You seem to be missing a comma in your GROUP BY clause.

----------


## Shaggy Hiker

If that's the actual code, and it looks like it is, then it certainly appears that there is a missing comma after CustName in the Group By clause.

----------


## VS2013

Thank you so much to both of you for the support. It is working now.

----------

