473,466 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Handling Ties in VBA

26 New Member
Hi Folks,

I have an Access 2003 application handling dinghy racing results. I'm looking for a way to handle ties. On the current first recordset pass, the results are fine if there are no ties (orig points). If there are ties, then they need to be handled by a process that recognises that ties exist (elapsed time) and adds together the original values (Orig Points) and divides by the number of ties to create new values (Points Reqd). As in the table below.

Trust this makes sense and that you folks can suggest some suitable code ... with best regards, Brian


Record Elapsed time Orig Points Points Reqd

1 5.5 1 1
2 5.6 2 3
3 5.6 3 3
4 5.6 4 3
5 5.7 5 5
6 5.7 6 5
7 5.8 7 7
8 5.9 8 8
May 30 '13 #1

✓ answered by ADezii

If you wish to keep this approach:
  1. Change the Data Type of the [Orig Points] Field in your Table to SINGLE.
  2. Change the SQL Statement used to Update your Table to:
    Expand|Select|Wrap|Line Numbers
    1. CurrentDb.Execute "UPDATE tblTest SET [Points Reqd] = " & _
    2.                              Round((intTotalOrigPoints / intNumOfTies), 2) & _
    3.                             " WHERE [Elapsed Time] = " & ![Elapsed Time], dbFailOnError
  3. This will Round the Results to 2 Decimal Places.
  4. Make sure that you change the Integer Division Operator (\) to Division (/).
  5. As stated earlier, a SQL approach would be more efficient.

15 1610
Oralloy
988 Recognized Expert Contributor
helm,

Are you trying to use just SQL, or are you using VBA for this?

Most importantly, what are your requirements? What sorts of information are you trying to extract from this database? It is pretty obvious that you are currently in the design phase, so let's discuss.

Finally, when you put down a data table, you should probably enclose it in CODE tags, or some such, so that we can read it easily. Like this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. This is your data table
  3.  
  4. Record  Elapsed time  Orig Points  Points Reqd
  5.  
  6.    1        5.5           1            1
  7.    2        5.6           2            3
  8.    3        5.6           3            3
  9.  
  10. etc... 
  11.  
Cheers,
Oralloy
May 30 '13 #2
ADezii
8,834 Recognized Expert Expert
Here is my Code Based approach that has been tested and appears to work quite well. The Logic is:
  1. Create a Recordset consisting of Unique Elapsed Times.
  2. Loop through this Recordset testing each Elapsed Time to see if there are Ties.
  3. If there are Tie(s):
    1. Calculate the Total Original Points for these Ties.
    2. Calculate the actual Number of Ties.
    3. Calculate the new Value for Original Points using the Formula:
      Expand|Select|Wrap|Line Numbers
      1. 'Notice use of Integer Division
      2. (Total Original Points \ Number of Ties)
    4. Update your Table (tblTest) with the Recalculated Values for Original Points for each Tie.
  4. The actual Code, which has been tested is:
    Expand|Select|Wrap|Line Numbers
    1. Dim MyDB As DAO.Database
    2. Dim rsUniqueET As DAO.Recordset
    3. Dim intNumOfTies As Integer
    4. Dim intTotalOrigPoints As Integer
    5.  
    6. Set MyDB = CurrentDb
    7.  
    8. 'Create a Recordset consisting of 'Unique' Elapsed Times
    9. Set rsUniqueET = MyDB.OpenRecordset("SELECT DISTINCT [Elapsed Time] FROM tblTest", dbOpenForwardOnly)
    10.  
    11. With rsUniqueET
    12.   Do While Not .EOF
    13.     'For each Unique Elapsed Time, are there other Ties?
    14.     intNumOfTies = DCount("*", "tblTest", "[Elapsed Time] = " & ![Elapsed Time])
    15.       If intNumOfTies > 1 Then   'There are Ties
    16.         'Calculate Total Original Points for the Ties
    17.         intTotalOrigPoints = DSum("[Orig Points]", "tblTest", "[Elapsed Time] = " & ![Elapsed Time])
    18.           'Update Points Required for all the Ties via provided Formula
    19.           CurrentDb.Execute "UPDATE tblTest SET [Points Reqd] = " & (intTotalOrigPoints \ intNumOfTies) & _
    20.                             " WHERE [Elapsed Time] = " & ![Elapsed Time], dbFailOnError
    21.       End If
    22.        .MoveNext
    23.   Loop
    24. End With
    25.  
    26. rsUniqueET.Close
    27. Set rsUniqueET = Nothing
    28.  
  5. Any questions, please feel free to ask.
P.S. - There may be a SQL approach that is more efficient and practical, but SQL is not my area of expertise.
May 30 '13 #3
helm
26 New Member
Hi Adezii,

That's brilliant - finds ties and updates records, Can we take this a step further please? In your solution, all the ties are given the same value - great - but there are some values that would not be integers - if for example there were ties at [orig points] 5 and 6, the values to be assigned [points reqd]would be 5+6/2 = 5.5

Thought I could change through from integer values to single but results still come out as integers ...

Regards, Helm
May 30 '13 #4
zmbd
5,501 Recognized Expert Moderator Expert
You should be able to do this with a ranking query:
How to Rank Records Within a Query

Then there is good-ole Allen Brown:
Ranking or numbering records
May 31 '13 #5
ADezii
8,834 Recognized Expert Expert
If you wish to keep this approach:
  1. Change the Data Type of the [Orig Points] Field in your Table to SINGLE.
  2. Change the SQL Statement used to Update your Table to:
    Expand|Select|Wrap|Line Numbers
    1. CurrentDb.Execute "UPDATE tblTest SET [Points Reqd] = " & _
    2.                              Round((intTotalOrigPoints / intNumOfTies), 2) & _
    3.                             " WHERE [Elapsed Time] = " & ![Elapsed Time], dbFailOnError
  3. This will Round the Results to 2 Decimal Places.
  4. Make sure that you change the Integer Division Operator (\) to Division (/).
  5. As stated earlier, a SQL approach would be more efficient.
May 31 '13 #6
Oralloy
988 Recognized Expert Contributor
helm,

You've obviously found the help you need.

It sounds like you are having difficulty storing the computed values. Remember that even though you calculate using floating-point, if the column you store the data in is an integer type, the digits after the decimal point will be discarded (lost).

Ok, that's my two-cents worth.

Good luck with your project.

Oralloy
May 31 '13 #7
helm
26 New Member
A big thank you to ADezzi - I didn't know that there weretwo division operators! Thanks too for those SQL references - I'm going to look at those in more detail. As you say, an SQL approach is great if you can work out (or find out) how to do it.

Best Regards, helm
May 31 '13 #8
Rabbit
12,516 Recognized Expert Moderator MVP
If I understand correctly, you can join this query to the original table on the [Elapsed time] field.
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.    [Elapsed time],
  3.    SUM([Orig Points]) / COUNT(*) AS [Points Reqd]
  4. FROM tblTest
  5. GROUP BY [Elapsed time]
May 31 '13 #9
ADezii
8,834 Recognized Expert Expert
@helm:
I must say that Rabbitt's solution is much more elegant, efficient, and professional. I would strongly suggest that you adapt his approach. The only change that you may wish to make is to control the number of Decimal Places displayed, as in:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2. [Elapsed time], 
  3. Round(SUM([Orig Points]) / COUNT(*),2) AS [Points Reqd] 
  4. FROM tblTest 
  5. GROUP BY [Elapsed time]
Jun 1 '13 #10
helm
26 New Member
Thank you Adezii and Rabbit,

The recordset I'm using has been through a range of activities before it gets to this (ties) point in the overall processing - I have quite a few 'with recordset' sections to test and answer various things ... I could now drop the recordset into a table and use SQL to work out the ties ... I wonder though whether it's possible to run SQL against a recordset rather than a table - sorry if sounds a bit of a naïve question (I'm an amateur dabbler as opposed to a professional programmer)-
just so glad to have an answer, at last to the 'ties' problem!

Regards, helm
Jun 2 '13 #11
Rabbit
12,516 Recognized Expert Moderator MVP
You can incorporate the SQL into the SQL you use to create the recordset.
Jun 2 '13 #12
helm
26 New Member
Hi Rabbit and Adezzi

Back from my hols ...

From some tests looks like I can't Dcount on a recordset - which is a pity since I have it right there ready to go. I could close the recordset and then run Adezzi's solution against the original data table ... but did you both believe that I could achieve the same thing (identify ties and update the table with 'just' an SQL update query)?

Thanks for your continuing interest and regards

Helm
Jun 11 '13 #13
Rabbit
12,516 Recognized Expert Moderator MVP
If you wanted to update the table you could. But there's no need to do so when you can caluclate it dynamically. And in fact, it's recommended that you not store what can be calculated unless there's very good reason to.

I'm not sure what you mean by the DCount, there's no need to use a DCount with my solution. And I'm not sure what you mean by recordset because the SQL for any recordset can be put in a query and you can use a DCount on a query. But then again, I don't see any need to use a DCount in the first place.
Jun 11 '13 #14
helm
26 New Member
I probably have not been very clear. Following data entry into a form, I open a dynaset and undertake quite a lot of data processing (2 A4 pages in fact) which includes the calculation of 'points'. The value of the points is fine just as long as there are no ties (where [elapsed time] is the same for two or more records). ADezii showed me a way using VBA to calculate my tie values using data from a test table. And sure thing, it worked.

In was working on the ADezzi approach when Rabbit suggested a wholly SQL approach.

If using a VBA approach, I've found that I can't just replace a source table with a recordset using the DCount/Dsum methods so guess that I need to separately open my source table, do some tie calcs and close the table.

If the issue can be solved using wholly SQL then happy to adopt that way forward ... assume I would need something like UPDATE table, SET, WHERE ... and so on) ...

Regards, Helm
Jun 11 '13 #15
Rabbit
12,516 Recognized Expert Moderator MVP
My point is that there's no need to replace/update anything. Just run the query as needed and everything will be up to date without having to save any additional data.

When you save calculated data, you run the risk of becoming out of sync with the rest of the data. If you instead run a query whenever the data is needed, it will always be correct and up to date.
Jun 11 '13 #16

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

Similar topics

2
by: WSeeger | last post by:
When creating a new class, is it encouraged to always include error handling routines within your LET and GET procedures? It's seems that most text books never seem to include much about error...
9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
3
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
5
by: ED | last post by:
I currently have vba code that ranks employees based on their average job time ordered by their region, zone, and job code. I currently have vba code that will cycle through a query and ranks each...
21
by: Anthony England | last post by:
Everyone knows that global variables get re-set in an mdb when an un-handled error is encountered, but it seems that this also happens when the variable is defined as private at form-level. So...
3
by: Stefan Johansson | last post by:
Hi all I'am moving from Visual Foxpro and have a question regarding "best practice" error handling in vb .net. In VFP I have always used a "central" error handling object in order to have a...
4
by: Al Williams | last post by:
Hi, I have error handling in place throughout my application. I also start the application wrapped in error handling code to catch any unexpected exceptions (i.e. exceptions that occur where I...
9
by: Gustaf | last post by:
I'm confused about structured error handling. The following piece of code is a simplification of a class library I'm working on. It works, and it does what I want, but I'm still not convinced that...
9
by: pargat.singh | last post by:
Hi Everyone: I have a C# program which loop through No. of files. In UAT i had medium size files and every thing goes ok but in Production files are big and i notice that my program is very...
0
by: Lysander | last post by:
Thought I would give something back with a few articles. This article is a bit of code to add error handling. When I have time, I want to write articles on multilingual databases, and Access...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.