VB.net to Access vs straight Access querying

Hi

I have a VB.net project where I use the following VB.net code to query an Access database.
As the tables in question get longer, the queries get a lot slower. Should I rather be doing all my queries in Access and then query the result? I tried doing make table queries in Access and then using a select statement on the VB side and this took pretty long too.
Sub GetUnPaidInvoiceAmounts()

        Try

            Dim oSupplier As String
            oSupplier = Me.ComboBox_Supplier.Text
            Dim sSQL_Supplier As String = ""

            If oSupplier = "[ALL]" Or oSupplier = "" Then
                'do nothing
            Else
                oSupplier = Me.ComboBox_Supplier.Text
                sSQL_Supplier = " And Suppliers.SupplierName = '" & SQLConvert(oSupplier) & "'"
            End If


            Dim oFrom As String = Me.DateTimePicker_From.Value.ToString
            Dim oTo As String = Me.DateTimePicker_To.Value.ToString
            Dim sDateCriteria As String = " And (Transactions.[Transaction Date] >= #" & oFrom & "# And Transactions.[Transaction Date] <= #" & oTo & "#)"


            Dim sSQL As String
            sSQL = "SELECT Transactions.ID, Suppliers.SupplierName, Transactions.Reference, Transactions.[Transaction Date], Transactions.Amount, IIf(DCount('Amount','Transactions','[Reference] = ''' & [Reference] & '''' & ' And ' & '[Transaction Type] = " & TransT_Payment & "' & ' And ' & '[DR_CR] = ''Dr''')=0,[Transactions].[Amount],Transactions.Amount-DSum('Amount','Transactions','[Reference] = ''' & [Reference] & '''' & ' And ' & '[Transaction Type] = " & TransT_Payment & "' & ' And ' & '[DR_CR] = ''Dr''')) As Due "
            sSQL = sSQL & "FROM Suppliers INNER JOIN Transactions ON Suppliers.SupplierID = Transactions.SupplierID "
            sSQL = sSQL & "WHERE ("
            sSQL = sSQL & "DSum('Amount','Transactions','[Reference] = ''' & [Reference] & '''' & ' And ' & '[Transaction Type] = " & TransT_Payment & "' & ' And ' & '[DR_CR] = ''Dr''') < Amount"
            sSQL = sSQL & " And [Transactions].[Transaction Type]=" & TransT_Invoice & ""
            sSQL = sSQL & " And [Transactions].[DR_CR]='Cr'"
            sSQL = sSQL & " And Void = False"
            sSQL = sSQL & sSQL_Supplier
            sSQL = sSQL & sDateCriteria
            sSQL = sSQL & ")"  'worked
            sSQL = sSQL & "Or ("
            sSQL = sSQL & "IsNull(DSum('Amount','Transactions','[Reference] = ''' & [Reference] & '''' & ' And ' & '[Transaction Type] = " & TransT_Payment & "' & ' And ' & '[DR_CR] = ''Dr'''))"
            sSQL = sSQL & " And [Transactions].[Transaction Type]=" & TransT_Invoice & ""
            sSQL = sSQL & " And [Transactions].[DR_CR]='Cr'"
            sSQL = sSQL & " And Void = False"
            sSQL = sSQL & sSQL_Supplier
            sSQL = sSQL & sDateCriteria
            sSQL = sSQL & ")"
      

            Dim connection As New OleDbConnection(ConnectionString)
            Dim dataadapter As New OleDbDataAdapter(sSQL, connection)
            Dim ds As New DataSet()
            connection.Open()
            dataadapter.Fill(ds, "Suppliers_table")
            connection.Close()
            DataGridView1.DataSource = ds
            DataGridView1.DataMember = "Suppliers_table"

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

Open in new window

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CodeCruiserCommented:
That is a complex query and I dont think it would make much difference if the query is run within Access. Have you added an Index?

http://www.techonthenet.com/access/tables/indexes.php

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Carlos VillegasFull Stack .NET DeveloperCommented:
Hello, that's what I think:
1. Always is better encapsulate your queries inside the DB (but I don't think that is the problem here).
2. You Access tables may need the correct indexes, if it done right that can greatly improve your query performance.
3. Also, the DSum function can be your bottle neck in this case, I'm not sure if it take advantage of the table indices.
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
thanks very much for the guidance
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.