Access error 2427: You entered an expression that has no value - ms-access

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.

Related

Access - DLookup error for empty fields

I have a DLookup function for displaying records from another table. Function works o.k., but problem occurs when there is empty field in the parent record.
Here is my DLookup function :
=DLookUp("Surname";"Employees";"ID_Employees_Table2=" & [Forms]![Company]![Company_Subform].[Form]![ID_Employees_Table1])
So, this works when ID_Employees_Table1 field is not empty, but If is "empty" It returns me #Error.
I tried with Nz function like this :
=Nz(DLookUp("Surname";"Employees";"ID_Employees_Table2=" & [Forms]![Company]![Company_Subform].[Form]![ID_Employees_Table1]);0)
But Access still returns me #Error in Textbox - where I'm using this function. I've tried also in combination with IsNull, IIf, but both can't stop this #Error from appearing.
How on earth can I stop this error from appearing in my Textbox, please any advices !
Answer is -
=DLookUp("Surname";"Employees";"ID_Employees_Table2=" & Nz([Forms]![Company]![Company_Subform].[Form]![ID_Employees_Table1]);0))
I just had to put Nz function in correct place, nightmare !!

Error 3061 Query "loses" form text value

I looked at a number of 3061 posts but they all have the query in VB. I am trying to run an already saved query in Access, that has a filter using a text field on a form. So all I am trying to do is just get a recordset from an existing query.
Not sure quite how to explain what's going on. But I have a Master form which holds the current selected date in a text object. I have a query that filters results based on the text object value:
SELECT DISTINCT EmployeeName
FROM dbo_Audits
WHERE dbo_Audits.AuditDate = [Forms]![MasterForm]![ReportDate]
Running the query is fine and it pulls for the selected date except in a specific circumstance.
If I open a subform, and keep the master form still open but not in focus, it still works i.e. I can run the query and it pulls the list of employees that had an audit that day.
But if I click a button on the subform to perform an action and put a breakpoint on the OnClick event, then try to run the query, it doesn't return any results. Its because it doesn't "recognize" or it's lost the value of "[Forms]![MasterForm]![ReportDate]" and therefore no results are returned.
Odd thing is, at the breakpoint, I query the text box value in the intermediate window and it still returns the date.
That is one way I have tested it. But what I am really trying to do is get the recordset from this query, in the back end coding, but when it encounters this coding:
strSQL = "SELECT * FROM " & strQueryName & " "
Set rstNames = CurrentDb.OpenRecordset(strSQL)
The OpenRecordSet returns the error message:
3061 - Too Few Parameters. Expected 1.
I put a breakpoint on the OpenRecordSet and do a DCount on the strQueryName and get a result of the number of records. So the query is kind of working. But not when I run the query through access (while on the breakpoint) and not when it tries to open the recordset.
Any ideas what's going on and how to fix this?
Since OpenRecordset does not dereference [Forms]![MasterForm]![ReportDate], and thinks it's a parameter, open the the saved query as a QueryDef object and give it the parameter value Access wants. Then you can use OpenRecordset from the QueryDef.
Dim qdf As DAO.QueryDef
Set qdf = CurrentDb.QueryDefs(strQueryName)
qdf.Parameters(0).Value = Eval(qdf.Parameters(0).Name)
Set rstNames = qdf.OpenRecordset()

DoCmd acCmdSelectRecord gives me run-time error 3125

I'm building a relatively simple datasheet-style form for selecting a record (which will be used to populate another form)
The form pulls from a query with multiple columns. Since it's counter-intuitive to have a highlighted field and data entry cursor when all the user wants to do is select a record, I added the following code to each field on the form:
Private Sub Model_Enter()
RunCommand acCmdSelectRecord
End Sub
This works fine for the "Model" field, but for every other field it gives me the following error:
Run-time error 3125
" is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long.
There's literally nothing else going on in this form, so I have no idea what the issue is, or why the code works for Model and breaks for everything else. Anyone else run into this issue?
I suggest you use the On Current event, rather than write code for each control. It is also possible to use conditional formatting and a little code to highlight a record.

access to oldValue for control in continous form generates error 3251 in beforeUpdate when a checkbox on form is updated

This is one of the stranger issues I have seen in MS Access. I have the following code in a continuous form:
Private Sub thisForm_BeforeUpdate(Cancel As Integer)
If Not Cancel Then
Debug.Print "pre-logging data changes..."
' here we need to doublecheck to see if any values changed.
' we simply iterate through the whole list, re-setting oldValue
' and newValue.
For Each control In thisForm.Section(acDetail).controls
If control.ControlType = acTextBox Or _
control.ControlType = acComboBox Or _
control.ControlType = acListBox Or _
control.ControlType = acOptionGroup Or _
control.ControlType = acCheckBox Then
Debug.Print control.Name
oldValues(control.Name) = control.oldValue
newValues(control.Name) = control.value
End If
Next
End If
End Sub
oldValues and newValues are Dictionary objects (although likely not related to the issue).
My form has 3 textbox controls, and a checkbox control. One of the text box controls is disabled, and is populated via the results of a simple inner join (to get the human readable name associated with a foreign key). The data source comes from the form's recordsource (no DLookup or anything is used).
If I edit one of the other two textbox controls, this code runs absolutely fine. HOWEVER, if I toggle the checkbox on the form, i get a runtime error 3251. In the watches window, I get the error again when i try to view the properties of "control". It shows the value of oldValue for the disabled control to be "Reserved Error".
If it did this consistently, I would think it was due to the control being disabled; but since it works without a problem when the other textboxes receive edits, and only breaks when the checkbox is toggled; I am stumped. I'm almost inclined to believe I found a bug in access, but I could use some extra input.
Anyone else every encounter an issue like this?
EDIT: Upon digging further, I found that in actuality only one of the 3 editable fields will not trigger this error. It holds string data. The other two controls hold a date value, and a yes/no value. Now I am even more confused.
i've got two ideas to that issue.
First one: If the RecordSource of your Form is an ODBC-Table thats linked to a SQL-Server then you should set a standard value for the CheckBox-Column. Otherwise it will try to set NULL to False and throw an error saying that somebody else edited the current record.
Second idea: Sometimes Access just has a little "hiccup" when it compiles the code. You could make a backup of your database and then try to decompile it using "C:\Program Files\Microsoft Office 2007\Office12\MSACCESS.EXE" "C:\yourFolder\yourDatabase.accdb" /decompile in the Run... Window (of course you have to insert the Path as it is on your machine). That often helps solving strange Problems.

MS Access overflow when I change queries

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.