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?
 
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
0
 
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.
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
thanks very much for the guidance
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.