I am getting an Overflow error in Access when this line runs
intAT = Nz(DLookup("at_ID", "qryAT", "at_sc_ID=" & Me.sc_ID & " AND at_OT=0"), 0)
If I add Debug.Print Nz(DLookup("at_ID", "qryAT", "at_sc_ID=" & Me.sc_ID & " AND at_OT=0"), 0) just before that line, it prints the id number just fine, but still errors during the assignment on the next line.
I can also get it to work by adding a criterion to the query that limits all the results to those that occur before a certain day and time. It ONLY works limiting the query to before that particular time, not after an earlier time. If any of the records after that time are included, it overflows again. I have looked at the data that was entered after that time and it all seems fine, but I am not sure what exactly I should be looking for. The at_ID associated with the first record that causes it to fail is 32838 if that helps at all.
My guess is that intAT was declared as Integer type. But 32,838 is too large for an Integer. (Integer can hold numbers from –32,768 to 32,767) So it triggers error #6, "Overflow".
Use a Long Integer instead.
Dim lngAT As Long
lngAT = Nz(DLookup("at_ID", "qryAT", "at_sc_ID=" & Me.sc_ID & " AND at_OT=0"), 0)
Related
This might be a basic formatting question, but for some reason I can't figure out a way get around it. The scenario that I have is that I want to use VBA to find the highest number from one field in a table in a given category (identified in another field in the same table). I have about eight categories, each with different numbers (e.g.)
ATD 500
ATD 250
ATD 700
MLK 500
MLK 120
PTO 900
I believe that the best way to do this would be to the DMAX Function, but for some reason I'm running into syntax errors no matter how I try to write it.
The code I'm work with so far is:
Dim HighestNumber As Variant
Dim HighCategory As Integer
HighCategory = InputBox("Please enter data", "ENTER", "ENTER")
HighestNumber = DMAX("[SaleValue]", "SalesTable", "[SalesCategory] = '" & HighCategory & "'")
(To clarify, [SaleValue] and [SalesCategory] are two columns in the access table "SalesTable", and I'm trying to set it up so that the user can enter a SalesCategory and get the highest SaleValue for that category. I'd like to be able to store that value somewhere for later use as well.)
Whenever I run this, I hit a Run-Time error '5', invalid procedure call on the last line. I'm fairly sure that it's a syntax error but I can't make heads or tales of why. I tried reading this site:
https://msdn.microsoft.com/VBA/Access-VBA/articles/application-dmax-method-access
But they focus on hardcoded criteria and I can't find a good example of how to use DMax where the criteria should be designated by the user.
Thanks for any help you can provide!
EDIT: Changing the code as follows produces the same error
Dim HighestNumber As Long
Dim HighCategory As String
HighCategory = InputBox("Please enter data", "ENTER", "ENTER")
HighestNumber = DMAX("[SaleValue]", "SalesTable", "[SalesCategory] = '" & HighCategory & "'")
You've specified that the HighCategory is an Integer, however this should either be a string or a variant, as isn't this "ATD" etc? Also, your HighValue variable is a variant, and should probably be a numeric (I'd actually consider using a LONG here rather than an INT).
I am assuming that an example of a SalesCategory would be "ATD" and therefore the highest value (and what DMAX would return) should be 700?
Here's a minor tweak to the code, this should work as you intended. I'm also checking for invalid entries here (Nz), and converting the result to a Long Integer (CLng)
Dim lngHighestNumber As Long
Dim strHighCategory As String
strHighCategory = InputBox("Please enter data", "ENTER", "ENTER")
lngHighestNumber = Nz(CLng(DMAX("[SaleValue]", "SalesTable", "[SalesCategory] = '" & strHighCategory & "'")), 0)
I am developing an Access database (using Office 2016) and have several tab controls which I want to display the number of records in the subform/subreport.
After a lot of searching etc I have it working for the subforms using a function which I call in the main forms current event (but in a seperate function so I can also call via a macro when I change the main forms record with a combo box, as it wasn't updating otherwise). The code I am using is:
Function ClientTotals()
Dim i As Integer
i = Form_sbfrm_ClientContacts.Recordset.RecordCount
Form_frm_Clients.ClientTabs.Pages("Contacts").Caption = "Contacts (" & i & ")"
End Function
This works perfectly for me and my tab name becomes "Contacts (No. of records)" but I can't get the syntax right to change this to work for a report, is it possible?
I have tried:
Function ClientTotals()
Dim i As Integer
i = Form_sbfrm_ClientContacts.Recordset.RecordCount
Form_frm_Clients.ClientTabs.Pages("Contacts").Caption = "Contacts (" & i & ")"
Dim j As Integer
j = Report_rpt_CurrentProjects.Recordset.RecordCount ' this line is highlighted with the debugger
Form_frm_Clients.ClientTabs.Pages("Current Projects").Caption = "Current Projects (" & j & ")"
End Function
As well as:
Dim j As Integer
j = rpt_CurrentProjects.Report.Recordset.RecordCount ' this line is highlighted with the debugger
Form_frm_Clients.ClientTabs.Pages("Current Projects").Caption = "Current Projects (" & j & ")"
and various others.
Another question I have is why is the syntax for the form "Form_sbfrm" etc and not using a "!". If I change to "!" it bugs out.
Thanks for your help, KAL
Thanks Delecron,
I think I will stick with the tabs for now as they are giving me exactly what I want, but remember what you have said for when I make future improvements if its a better way of doing it.
EDIT
Using what you have said I changed my VBA to a DCOUNT method:
Dim j As Integer
j = DCount("*", "qry_CurrentProjects", "FK_Project_Client_ID = Forms!Navigation!Navigationsubform.form!Client_ID")
Form_frm_Clients.ClientTabs.Pages("Current Projects").Caption = "Current Projects (" & j & ")"
This means my report tabs are now also working just how I wanted
I was getting in a muddle with the criteria/filter, hense the edit.
If Recordset is an old method I am assuming it would be best to replace my other code with the Dcount method?
Thanks again, KAL
Further EDIT
After doing this I could see that everytime the form was changed there was a slight flicker. Not bad but you could see there was a lot of calculation going on. Therefore I have changed my method to the following, and posted here for anyone looking at this in the future.
In the form footer a textbox with COUNT([Project_ID])
In my function
Dim j As Integer
j = Form_frm_Clients!rpt_CurrentProjects.Report!txt_CurrentProjectsCount.Value
Form_frm_Clients.ClientTabs.Pages("Current Projects").Caption = "Current Projects (" & j & ")"
Now I can see it is working quicker with no flicker.
Recordset if you need to return complex data, if you need one value, a total or a sum, Domain functions are the way to go. Don't overdue them though, having too many on a form or a report can start to bog down usability.
Glad I can help.
Recordset.Recordcount is a legacy feature that only worked in ADP files (Access front end's to a SQL database). Those are no longer supported.
If the report is based on 1 client only and there is no grouping then you can do this:
Click on the detail section and in Events create an event for On Paint. In there set
(Name of Page).Caption = DCount("*", "NAME OF QUERY/TABLE") or
(Name of Page).Caption = DCount("*", "NAME OF QUERY/TABLE", "Filter Expression") (Filter expression optional).
If the report is grouped where it will show a different page per client or date range or any other grouping this will not work since the Caption field is not data bound. You would have to add logic to the Dcount statement above to field by the current filter condition.
For example, say you have a database of 200 clients and you're running one big report on all of them, each page will get its own tab control per client, the syntax would be
(Name of Page).Caption = DCount("*", "ClientContacts, "ClientID = " & ClientID)
The better way to do it especially if you are grouping is get rid of the tab control and use databound controls. You could create a box around the information that would be in the tab page and put a textbox where the tab would go. Create a group header for however you are grouping the data. Create another invisible textbox in the group header and set the controlsource = Count([fieldname]) where fieldname is whatever you are grouping the data by (the inside brackets count).
Then in the textbox you created to simulate the tab, set the controlsource to the name of the invisible textbox.
Let me know if this helps.
I have a form, and am counting the number of records in a subform. Sometimes the subform is empty, which I'd like to check for, but it throws
Run-time error '2427': You entered an expression that has no value
It's obvious why this error is happening, but less obvious is how to work around it. This is the code causing the error. MainTableComboBox.Value contains RecordID.
DCount("*", "[SubFormTable]", "[SubFormTable].[RecordID] = " & MainTableTextBox.Value)
I've tried the following but it still errors out.
Using If(IsError(Dcount...)) then (do stuff)
Checking that the number of records is larger than zero
Nested Nothing, IfNull, and IfEmpty statements
Using an error handler
What other ways exist to get around this error?
I think you could set a recordset equal to the subform datasource and use IF rs.EOF = True Then to see if there are records to show before running your dcount function. See this question for more information.
Okay, I have built reports in MS Access 2007, and each report runs off of several (40+) queries. The queries are opening tables, subqueries, etc, and I don't think Access is closing them. I could be wrong, but for some reason I think this is causing the overflow.
But anyways, I am trying to figure out why it is happening all of a sudden, and what I can do to resolve it. I had the reports working fine when I just had a schema and some dummy data, but when the database was actually populated, the individuals who gave us the data created a few more look up tables, so now a typical query using 3 tables is now using 5.
Do you think this increase in look up tables (and therefore more objects being opened by Access) is the reason I am getting overflow errors, or could it be something else? Also, I don't know VBA, so are there any simple solutions (e.g. breaking up the reports, which would take a while) that would be worth pursuing?
Thanks
Make sure you really understand your "overflow" condition. This code displays "Error 6 (Overflow)", without the quotes, in the Immediate Window.
Dim i As Integer
Dim strMsg As String
On Error GoTo ErrorHandler
i = 32767
i = i + 1
ExitHere:
On Error GoTo 0
Exit Sub
ErrorHandler:
strMsg = "Error " & Err.Number & " (" & Err.description _
& ")"
Debug.Print strMsg
GoTo ExitHere
The explanation for that error is that 32,767 is the maximum value a VBA Integer can accept. So, attempting to add one would give 32,768 which is greater than an Integer can hold ... so overflow.
Other numeric data type also have limits. For example, 2147483647 is the maximum value which can be stored as a VBA Long.
I might be totally off base here, but I would check whether your complex report includes sorting and grouping options where perhaps you produce totals. And if so, whether the data you added pushes the values for any of those totals beyond the capacity of their respective data types.
If you're getting a different error message which includes the word "overflow", it might help to tell us the exact text of the error message.
I have the following SQL Query I'm executing and I'm trying to find why it returns the error 'overflow' when running the query.
Now I want to print the last record that it computes before going into overflow, is this possible using MS Access VBA?
Private Sub Command0_Click()
Dim sql As String
Dim rs As DAO.Recordset
Dim db As DAO.Database
Set db = CurrentDb()
sql = "SELECT DatumNaarWeeknummer([tbl_ArtikelVerwijderdUitZaaglijst]![RegistratieDatum]) AS WeeknummerGezaagdeOmzet, " _
& "Sum([TotaalPrijs]/([tbl_ArtikelsPerOrder]![Aantal]*[Totaal])*[tbl_ArtikelVerwijderdUitZaaglijst]![Aantal]) AS GezaagdeOmzet " _
& "FROM (((tbl_ArtikelsPerOrder LEFT JOIN qry_Actieve_Orders ON tbl_ArtikelsPerOrder.OrderID = qry_Actieve_Orders.OrderID) LEFT JOIN qry_ArtikelPerOrderID_EenheidsPrijsBijFranco ON tbl_ArtikelsPerOrder.ArtikelsPerOrderID = qry_ArtikelPerOrderID_EenheidsPrijsBijFranco.ArtikelsPerOrderID) " _
& "LEFT JOIN qry_AantalArtikelTypesPerArtikelPerOrder ON tbl_ArtikelsPerOrder.ArtikelsPerOrderID = qry_AantalArtikelTypesPerArtikelPerOrder.ArtikelsPerOrderID) " _
& "RIGHT JOIN tbl_ArtikelVerwijderdUitZaaglijst ON tbl_ArtikelsPerOrder.ArtikelsPerOrderID = tbl_ArtikelVerwijderdUitZaaglijst.ArtikelsPerOrderID " _
& "GROUP BY DatumNaarWeeknummer([tbl_ArtikelVerwijderdUitZaaglijst]![RegistratieDatum]);"
Set rs = db.OpenRecordset(sql, dbOpenDynaset)
End Sub
(Edit: rearranged to focus on likely culprit)
No, you can't get the last record easily. You can try Select Top 5000 . . . etc., raise the value if it works, and lower the value if it doesn't, and zero in on it that way. But, it is unlikely that a particular is record is causing the problem. I don't think there's bad data somewhere. It's the query.
Focus on the Sum in the select query. Take that out, and you'll likely have the query work. It could well be that the sum overwhelms the numeric type that sql is using to add your values. Actually, the more I think about it, this is probably it. Yeah. If it is, you'll need to force that to a type that can handle larger numbers, like this:
SELECT blah blah, SUM(CAST([TotaalPrijs]/([tbl_ArtikelsPerOrder]![Aantal]*[Totaal])*[tbl_ArtikelVerwijderdUitZaaglijst]![Aantal] AS DECIMAL)) AS GezaagdeOmzet
The syntax might be slightly different for MSAccess, but it will be something like that. Being Access, the default might be int, in which case you might be able to specify Long. Otherwise, specify Decimal. Try to avoid the Real numbers if you can (single, etc.) and they can mess you up if you're not careful.
Though less likely, here are some other possible culprits:
Are you sure this query is logically
correct? This query can be caused by
too large a result set being
returned. Use the Select Top 1000
etc. syntax, and analyze the results
to make sure your joins are working
as you wish, and aren't mistakenly
causing cartesian results, for
example.
If your query is returning legitimate
results, then might it be that the
legitimate result are too massive?
If you really should be getting a
billion results, and this is too
much, then you'll have to change your
whole strategy, or reduce the columns
being returned, etc.
My guess is that this expression:
Sum([TotaalPrijs]/([tbl_ArtikelsPerOrder]![Aantal]*[Totaal])*[tbl_ArtikelVerwijderdUitZaaglijst]![Aantal])
produces an numeric overflow for some records. No knowing what data types your columns use, I can only recommend trying to convert them to a "bigger" data type during the calculation.
Is it possible that the part of the query
([tbl_ArtikelsPerOrder]![Aantal]*[Totaal])*[tbl_ArtikelVerwijderdUitZaaglijst]![Aantal])
Is returning 0? If so that would cause an error, so it might not be large data that is at fault but data that is too small or non-existent