473,396 Members | 1,758 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,396 software developers and data experts.

Overflow error in query

374 256MB
I use the following code to execute this query:

[code]Me.Text419 = RunTheSql("SELECT Sum(tblCosts.CostFig) AS CountOfID FROM tbllog INNER JOIN tblCosts ON tbllog.NCC_ID = tblCosts.NCC_ID WHERE (((tbllog.DeptRaisedBy)=" & Me.cmboDept & "));", "CountOfID")
CODE]

Note: query converted back from vba as I have been testing in normal querydef

Expand|Select|Wrap|Line Numbers
  1. Public Function RunTheSql(sqlstring As String, WhatToCount As String) As Integer
  2.  
  3. On Error GoTo jumpout
  4. Dim rs As Dao.Recordset
  5. Dim counter As Integer
  6.  
  7.  
  8. Debug.Print sqlstring
  9. Set rs = CurrentDb.OpenRecordset(sqlstring, dbOpenSnapshot)
  10. If rs.RecordCount <> 0 Then
  11. counter = rs(WhatToCount)
  12. Else
  13. counter = 0
  14. End If
  15.  
  16.  
  17. RunTheSql = counter
  18.  
  19.  
  20. completedyo:
  21. rs.Close
  22.  
  23. Set rs = Nothing
  24.  
  25. Exit Function
  26.  
  27. jumpout:
  28. MsgBox Err.Description & " - " & Err.Number
  29. Resume completedyo
  30.  
  31. End Function
  32.  
When I get to the line:
Expand|Select|Wrap|Line Numbers
  1. counter = rs(WhatToCount)
  2.  
I get the overflow error 6 message box.

I have no clue why as this works perfectly fine when I put the sql statement converted back in to a normal query.

Also the function works for all my other queries.

Any ideas?
Mar 14 '11 #1
13 6438
Mariostg
332 100+
Dim counter as Long instead of Integer. Your counter is likely larger than 32,767.
Mar 14 '11 #2
munkee
374 256MB
I thought the same but it still seems to be happening:
Expand|Select|Wrap|Line Numbers
  1. Public Function RunTheSql(sqlstring As String, WhatToCount As String) As Integer
  2.  
  3. On Error GoTo jumpout
  4. Dim rs As Dao.Recordset
  5. Dim counter As Long
  6.  
  7.  
  8. Debug.Print sqlstring
  9. Set rs = CurrentDb.OpenRecordset(sqlstring, dbOpenSnapshot)
  10. If rs.RecordCount <> 0 Then
  11. counter = rs(WhatToCount)
  12. Else
  13. counter = 0
  14. End If
  15.  
  16.  
  17. RunTheSql = counter
  18.  
  19.  
  20. completedyo:
  21. rs.Close
  22.  
  23. Set rs = Nothing
  24.  
  25. Exit Function
  26.  
  27. jumpout:
  28. MsgBox Err.Description & " - " & Err.Number
  29. Resume completedyo
  30.  
  31. End Function
  32.  
Mar 14 '11 #3
Mariostg
332 100+
Well, your function needs to return a Long as well...
did you debug.print(rs(WhatToCount)) to see what comes out?
Mar 14 '11 #4
NeoPa
32,556 Expert Mod 16PB
Maybe I'm missing something. Does this function do anything the Domain Aggregate functions can't (EG. DSum(); DCount(); etc)?
Mar 15 '11 #5
munkee
374 256MB
NeoPa: Yes the function runs about 20 times on a "dashboard" page I have showing various figures from my database. DCount / Lookup etc was hitting performance hard.

However with that being said the fix suggested using "long" has cured half the problem.

I am now getting "Invalid use of null" errors when the function should return 0.

I have stepped through the code and eventhough the sql in a normal query would return a blank I am getting the .recordcount in this function returning 1 and then ofcourse it produces the invalid use of null when I get to:

Expand|Select|Wrap|Line Numbers
  1. counter = rs(WhatToCount)
When it should really return a .recordcount = 0 and jump to counter = 0

Current code:
Expand|Select|Wrap|Line Numbers
  1. Public Function RunTheSql(sqlstring As String, WhatToCount As String) As Long
  2.  
  3. On Error GoTo jumpout
  4. Dim rs As Dao.Recordset
  5. Dim counter As Long
  6.  
  7.  
  8. Debug.Print sqlstring
  9. Set rs = CurrentDb.OpenRecordset(sqlstring, dbOpenSnapshot)
  10. If rs.RecordCount <> 0 Then
  11.  
  12. counter = rs(WhatToCount)
  13. Else
  14. counter = 0
  15. End If
  16.  
  17.  
  18. RunTheSql = counter
  19.  
  20.  
  21. completedyo:
  22. rs.Close
  23.  
  24. Set rs = Nothing
  25.  
  26. Exit Function
  27.  
  28. jumpout:
  29. MsgBox Err.Description & " - " & Err.Number
  30. Resume completedyo
  31.  
  32. End Function

Edit: Cured the issue by wrapping with nz:
Expand|Select|Wrap|Line Numbers
  1. counter = Nz(rs(WhatToCount), 0)
Weird why it was returning like this with a recordcount of 1 when there wasn't a visible record to be found though.
Mar 15 '11 #6
Mariostg
332 100+
Yep, Integer, Long cannot return a Null Value.
You could use counter = nz(rs(WhatToCount),0).

you could also check that rs.BOF and rs.EOF are not false. That ensures you don't have an empty recordset.
Mar 15 '11 #7
NeoPa
32,556 Expert Mod 16PB
Before .RecordCount is reliable in a Snapshot recordset it seems (See Help for RecordCount Property (DAO)) that the last record must first be read. In your case I expect simply testing for BOF and EOF might be a more reliable indicator.

I have a hard time believing that this function works more quickly than a properly invoked Domain Aggregate function. Not because the code isn't any good, but simply because VBA code is only semi-compiled, which in itself adds quite an overhead to executing code. This is not the case with properly compiled functions wiyhin the libraries. That said, it can be a bit tricky calling such functions for the example you give us, but like-for-like I would expect the VBA to execute more slowly.

Let us know if Mario's idea of checking for BOF & EOF instead of the RecordCount pays any dividends.
Mar 16 '11 #8
munkee
374 256MB
NeoPa,

I had around 10 of the domain functions set up initially for my simple queries but it is when they become more technical with multiple joins being required to drag in info that I have no clue where to start. That also made moving over to a function that would execute the sql uncompiled the easy option.

With that being said can you recommend any other methods for carrying out complex queries. I thought Domain functions were just a nice dressed up function native to access that would be carrying out queries with sql, making everything look a bit more userfriendly though?

I am simply calling all my functions from a submission button that grabs a date and a department name as part of the inputs to the sql to be executed.

I never really mess with using querydefs which I know are precompiled but I assume they might be an option. If there is some example code kicking around that anyone knows of regarding how to return a value from this I would appreciate it.
Mar 16 '11 #9
dsatino
393 256MB
From what I can tell, your query is only designed to return one record so you don't really need the recordcount. Just use rs.movefirst...if the recordset is empty it will error.

After that you have to test for null in 'whattocount' field.

Also i noticed that your calling it a count, but your basing it on a summed field with the word 'cost' in it. If there are decimal values in your field you'll get another error.
Mar 16 '11 #10
NeoPa
32,556 Expert Mod 16PB
Generally, the best idea is to design the .RecordSource such that as much as possible of what you want to show on the form is included. That's not always possible of course.

Using Domain Aggregate functions, of course, requires saved record sources of some type. They won't work with SQL strings.

What did you find when you tried Mario's idea (as mentioned in the previous post)?
Mar 16 '11 #11
NeoPa
32,556 Expert Mod 16PB
DSatino:
From what I can tell, your query is only designed to return one record so you don't really need the recordcount. Just use rs.movefirst...if the recordset is empty it will error.
Bear in mind that performance is an issue here. What you suggest adds an extra recordset processing step into the equation. Hence the suggestion to check .BOF & .EOF.
Mar 16 '11 #12
dsatino
393 256MB
True, but snapshot recordsets are fully loaded before you can use them so the extra step is measured in milliseconds.

Also, I'm guessing that performance issues are probably related to a lack of indexing on the JOIN or WHERE fields.
Mar 17 '11 #13
NeoPa
32,556 Expert Mod 16PB
Performance issues can be related to many and various things. It's never a good idea to forget though, that small periods add up when repeated many times. I know it's not much, but why bother if you can simply check .EOF?

Personally, and this is just my opinion so should be treated as such, I don't much like using error handling to implement logic.
Mar 17 '11 #14

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

Similar topics

5
by: Mike | last post by:
I am trying to use the bulk insert command but I am getting the following error: ---Begin Error Msg--- Server: Msg 4867, Level 16, State 1, Line 1 Bulk insert data conversion error (overflow)...
2
by: Robert McEuen | last post by:
Using Access 97, Windows XP I'm receiving a Numeric Field Overflow error during text import that I did not receive before I split my database. Another thread I found suggested that the cause of...
7
by: MLH | last post by:
?2944*24 gives me an overflow error, entered in the immediate window. Anybody else?
4
by: Chua Wen Ching | last post by:
Thanks Derek... Okay i had another question.. my program runs smoothly for the first minute, after 1 minute... suddenly it breaks and display this error: do you know what is the cause of...
1
by: zaneh | last post by:
I have found TheScripts a very useful site, but however I cannot find an answer to a problem I am having, so I finally have to ask the question! I am getting an Overflow error in a Function I have...
0
by: Bob | last post by:
I hope somebody can help. I'm linking to an excel spreadsheet with several fields that come across as double. I can query the linked table fine, but when I make a form with the query as the...
3
by: MLH | last post by:
A wide comma delimited text file import into A97 failed with an overflow error. I'm estimating it to have been no more than 1000 records. 100% of all the fields were quoted strings or ZLS's. ...
3
by: jer006 | last post by:
Hi I am writing a select statement that has an arithmetic function inside a case statement that uses logic to decide whether to divide or multiply and when I run the arithmetic statements outside...
2
by: Hulikal | last post by:
Hi, I using the following query select dateadd(s, 1185255439727, '01-01-1970 05:30:00') on MSSQL 2005. I get the error Arithmetic overflow error converting expression to data type...
3
by: sreegans | last post by:
Hi.. i appended 4 to 5 records, after that overflow error had occured while executing the sql query in vb.... Pl guide me how to solve this? Sree...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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
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,...

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.