[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VB.net to Access vs straight Access querying

Posted on 2011-10-05
3
Medium Priority
?
248 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:Murray Brown
3 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 1000 total points
ID: 36919268
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
 
LVL 17

Assisted Solution

by:Carlos Villegas
Carlos Villegas earned 1000 total points
ID: 36919279
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
 

Author Closing Comment

by:Murray Brown
ID: 36919331
thanks very much for the guidance
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question