473,399 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Functions and in line sql to fetch counts.. best way?

374 256MB
Hi all,

This is more of a check against what I am doing to see if this is the best / most optimised way of carrying out the procedure.

I run a number of queries against multiple tables to produce monthly and fiscal year figures.

These are:

Number of actions to be closed
Number of actions completed on time
Number of actions completed

Monthly/Fiscally.

In order to do this I use 3 combo boxes and a submit button on my main form with the code behind calling multiple functions to populate text boxes on the form:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command81_Click()
  2. On Error GoTo pleasedont
  3.  
  4.     'Counting actions required to close in current month and current fiscal year
  5.     Me.txtActionsReqClose = CountingActionsPerMonth(Me.cmboMonth.Column(0), Me.cmboYear.Column(0), Me.cmboDept.Column(1))
  6.     Me.txtActionsRequiredClosedFiscal = CountingActionsPerFiscalYear(Me.cmboMonth.Column(0), Me.cmboYear.Column(0), Me.cmboDept.Column(1))
  7.  
  8.     'Counting actions completed on time in current month and current fiscal year
  9.     Me.txtMonthActionsCompletedOnTime = CountingActionsClosedOnTimePerMonth(Me.cmboMonth.Column(0), Me.cmboYear.Column(0), Me.cmboDept.Column(1))
  10.     Me.txtActionsCompletedOnTimeFiscal = CountingActionsClosedOnTimePerFiscalYear(Me.cmboMonth.Column(0), Me.cmboYear.Column(0), Me.cmboDept.Column(1))
  11.  
  12.     'Counting actions completed in current month and current fiscal year
  13.     Me.txtActionsCompletedThisMonth = CountingActionsClosedPerMonth(Me.cmboMonth.Column(0), Me.cmboYear.Column(0), Me.cmboDept.Column(1))
  14.     Me.txtActionsCompletedFiscal = CountingActionsClosedPerFiscalYear(Me.cmboMonth.Column(0), Me.cmboYear.Column(0), Me.cmboDept.Column(1))
  15.  
  16. Exitingyo:
  17.     Exit Sub
  18.  
  19. pleasedont:
  20.     MsgBox Err.Description & " - " & Err.Number
  21.     Resume Exitingyo
  22. End Sub
My functions use inline sql and DAO to return a value. I will post up one of my Fiscal functions just to see as an example as the query also contains a function within it as our fiscal year runs from October - September.

Expand|Select|Wrap|Line Numbers
  1. 'Function used to count the number of actions that need to be closed for the fiscal year selected
  2. Public Function CountingActionsPerFiscalYear(cmboMonth As String, cmboYear As Integer, cmboDept As String)
  3.  
  4. On Error GoTo jumpoutst
  5.  
  6. Dim dbst As DAO.Database
  7. Dim rsst As DAO.Recordset
  8. Dim strsqlst As String
  9. Dim counterst As Integer
  10.  
  11. Set dbst = CurrentDb
  12.  
  13.  
  14. strsqlst = ""
  15.  
  16. strsqlst = "SELECT Count(*) AS [Fiscal Actions To Complete]"
  17. strsqlst = strsqlst & " FROM tblCorrective INNER JOIN (tbldept INNER JOIN tblSecurity ON tbldept.DeptID = tblSecurity.Udept) ON tblCorrective.CorrectivePerson = tblSecurity.SecurityID"
  18. strsqlst = strsqlst & " WHERE (((tbldept.Department)='" & cmboDept & "') AND ((tblCorrective.CorrectiveCompletedDate) Between fiscalStartDate('" & cmboMonth & "'," & cmboYear & ") And fiscalEndDate('" & cmboMonth & "'," & cmboYear & ")));"
  19.  
  20. 'Debug.Print strsqlst
  21. Set rsst = dbst.OpenRecordset(strsqlst, dbOpenSnapshot)
  22. If rsst.RecordCount <> 0 Then
  23. counterst = rsst("Fiscal Actions To Complete")
  24. Else
  25. counterst = 0
  26. End If
  27.  
  28. 'Debug.Print counter
  29. CountingActionsPerFiscalYear = counterst
  30.  
  31.  
  32. completedyost:
  33. rsst.Close
  34. dbst.Close
  35. Set rsst = Nothing
  36. Set dbst = Nothing
  37. Exit Function
  38.  
  39. jumpoutst:
  40. MsgBox Err.Description & " - " & Err.Number
  41. Resume completedyost
  42.  
  43. End Function
  44.  

Is this a sound way to produce values? It runs pretty smoothly on my machine but I will be splitting the database soon and I would rather find out if the method is alright now before moving on and realising I am using a very slow method for retrieving values.
Jan 20 '11 #1
2 1571
hype261
207 100+
One of the way you could potentially speed this up is instead of using inline sql I would just create a parameterized query inside by database and then call that query from the code. Here is a quick example with a query called myQuery that is expecting a parameter @ID

Expand|Select|Wrap|Line Numbers
  1. Public Sub FancyFunction
  2. On Error goto Err_FancyFunction
  3.  
  4. Dim qry as DAO.QueryDef
  5. Dim rcd as DAO.Recordset
  6.  
  7. Set qry = CurrentDb.QueryDef("myQuery")
  8.  
  9. qry.parameters("@ID") = 1
  10.  
  11. set rcd = qry.openRecordSet
  12.  
  13. 'Do something fancy with the recordset
  14.  
  15. Exit_FancyFunction:
  16. rcd.close
  17. set rcd = nothing
  18. qry.close
  19. set qry = nothing
  20.  
  21. Err_FancyFunction:
  22. MsgBox err.description
  23. resume Exit_FancyFunction
  24.  
End Sub

The benefits of this is that the sql has already been parsed and optimized by the engine and could result in some performance gains. Also if you ever need to use the sql again some place you can easy do it without any copy and paste.
Jan 20 '11 #2
patjones
931 Expert 512MB
Hi,

If you did want to keep it inline, I'm not sure why you couldn't just write one general counting function to use anytime you needed to count something rather than having four separate functions which appear to do essentially the same thing.

Pat
Jan 20 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Roberto Dias | last post by:
What about to interchange variables between functions? Which are the best way to do this? I know that this is a fundamental question, but I C++ fundamental student yet. I just started few day ago...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
6
by: magix | last post by:
Hi, when I read entries in file i.e text file, how can I determine the first line and the last line ? I know the first line of entry can be filtered using counter, but how about the last line...
2
by: twq | last post by:
hello Does anyone no if finanical functions of excel like Coupondays , Duration, Yield to Maturity, are included into vb 05, if not has anyone an idea where i can get a library with the...
60
by: jacob navia | last post by:
Gnu C features some interesting extensions, among others compound statements that return a value. For instance: ({ int y = foo(); int z; if (y>0) z = y; else z=-y; z; }) A block enclosed by...
6
by: Peter Duniho | last post by:
So, I'm trying to learn how the Regex class works, and I've been trying to use it to do what I think ought to be simple things. Except I can't figure out how to do everything I want. :( If I...
3
by: maya | last post by:
hi, what is best way of doing this.. I have a bunch of headlines, one after the other, thus: <div class="headlines"> Article Headline goes here lorem ipsum viderer<br> Article Headline goes...
6
by: Bryan Parkoff | last post by:
I want to know the best practice to write on both C / C++ source codes. The best practice is to ensure readable. Should left brace be after function or if on the same line or next line. For...
2
by: Andrea Taverna | last post by:
Hello everyone, I wrote a bunch of recursive functions to operate on multi-dimensional matrices. The matrices are allocated dynamically in a non-contiguous way, i.e. as an array of pointers...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.