I am trying to count the number of records in a table which satisfy criteria for two different fields. Both fields are string values.
The first field is what type of Test appears e.g. 'Manometry'. I can get this field to work on it's own.
I experience a problem when trying to add the second criteria.
The second field is the TestID, which is in the format A_155_19, where 155 is the investigation number and 19 identifies the year it took place.
I would like to count all the manometry tests which occur in the current year.
DCount("[Test]", "Visits", "[Test] = 'Manometry'" & "[TestID] = *Right((Year(Date)), 2)'")
I am currently getting the error message 3075, which is missing syntax.
Any help would be greatly appreciated.
What about:
DCount("[Test]", "Visits", "[Test] = 'Manometry' And [TestID] Like " & SomeExpression)
You can use Format:
DCount("*", "Visits", "[Test] = 'Manometry' And [TestID] Like '*_' & Format(Date(), "yy") & "'")
Related
I'm trying to write an If statement that determines if my column [Pack_Number] has only one Unique number or if there is more than one.
I'm not exactly sure of the syntax to make this check for an Access Table since it uses records instead of rows like Excel.
This isn't right but it shows what I'm trying to do:
If [Forms]![ReviewButton]![RetailEntry].[Form]![Pack_Number].Value = Distinct Then
MsgBox "It's Unique!"
Else
MsgBox "It's NOT Unique!"
End If
Can anyone point me in the right direction for this check or have an example they can show me?
Any help would be greatly appreciated!
Thanks!
Consider domain aggregate, DCount, to search on specific value. Below assumes form value is a string to encloses single quotes.
If DCount("Pack_Number", "myTable", _ "Pack_Number = '" & _
[Forms]![ReviewButton]![RetailEntry].[Form]![Pack_Number].Value & "'") = 1 Then
...
Per #Erik's comment, no need to concatenate or punctuate:
If DCount("Pack_Number", "myTable", _
"Pack_Number = [Forms]![ReviewButton]![RetailEntry].[Form]![Pack_Number].Value") = 1 Then
...
I have a db in Access and I'm trying to get a textbox to run my query and pass an other bounded textbox's value in as the criteria in DLookUp. I have the query running in design view and when I enter the criteria directly it returns the correct results. When I open the report it gives me the sum of all the possible rows. In other words it doesn't filter the rows.
I haven't used Access in about twelve years, thankfully, and everything I've done up to this point has been tutorial/example patchwork, but here it is...
SQL Query:
SELECT Sum(IIf(Attended=-1,1,0)) AS attendance
FROM Students_Classes_Attendance
WHERE (((CStr([Students_Classes_Attendance].[Class_Id]))=[classId]));
DLookUp as Control Source:
=DLookUp("[Total Attendance by Class]![attendance]",
"[Total Attendance by Class]",
"[Class_Id] =" & [Class_Id])
I'm lost at the moment. I'm guessing that the value isn't there before the query fires and since the criteria is an optional parameter that it's being passed null, but I would hope you'd get an error from that. Not that #Error is very meaningful anyway.
Does anyone know for certain the problem and the best way to correct it? Thanks.
Edit:
I did the changes recommended in the answer so now my DLookUp looks like...
=DLookUp("[attendance]",
"[Total Attendance by Class]",
"[Class_Id] =" & [Class_Id])
...still returns the total for all rows. Removing the criteria completely makes no difference either, which returns me to thinking it has something to do with the bound textbox not having a value.
DLookup uses the following syntax:
Syntax for numerical values:
DLookup("FieldName" , "TableName" , "Criteria = n")
Syntax for strings: (note the single apostrophe before and after the string value)
DLookup("FieldName" , "TableName" , "Criteria= 'string'")
Syntax for dates: (note the # before and after the date value)
DLookup("FieldName" , "TableName" , "Criteria= #date#")
I believe you just need to remove the table name from the first parameter. Try this:
=DLookUp("[attendance]", "[Total Attendance by Class]", "[Class_Id] = " & [Class_Id])
Keep in mind that if Class_Id is a Text Field, you need to surround it by single quotes:
=DLookUp("[attendance]", "[Total Attendance by Class]", "[Class_Id] = '" & [Class_Id] & "'")
In a davg formula, in the condition section, how to match a field in a table with a formula in a query.
In the below example, I want to avg all the numbers of [dec10], given its category is matching with the [class]
My current formula which is not working is:
Davg(“[dec10]”,”prct change”,”[Category] = [Class]“)
The error I am receiving is “Microsoft Access cannot find the name [Class] you entered into the expression”
I can achieve my desired results through:
IIf([Class]=”Retail”,Davg(“[dec10]”,”prct change”,”[category] = ‘retail’ “),
IIf([Class]=”Banks”,Davg(“[dec10]”,”prct change”,”[category] = ‘banks’ “), etc…))
However I have more than 100 classes and the size will not fit in the formula.
Any idea anyone pls?
My guess is that you actually want
Davg("[dec10]","prct change","[Category] = '" & [Class] & "'")
Sorry if the title is confusing. But I have a table with a few different columns. One column is the KitNumber and the other is the ReturnDate. I am trying to select the value of the ReturnDate to see what the length of the entry is (also, does VBA let you get the length of a date?). What I need to do though, is the user will enter a number in an unbound, and then that value will look in the table to see if it matches another value in there, and if it does, it will select the return date. Here is the code I have now:
strSQL = "SELECT ReturnDate FROM Crew WHERE KitNumber = " & Me.AssignKit
Debug.Print strSQL
DateLen = Len(strSQL)
So say I enter '111111' in the unbound. I want it to look in the table then to see if there is a matching number. Then if there is it should return the ReturnDate value and get the length of it. Cause right now the Debug just returns the KitNumber instead of the date. Anyone be able to help me out? Thank you
If it's a one off, then a DLookup in the OnExit or OnChange events should give you the info you need to work with
using your example,
Debug.Print DLookup("ReturnDate","Crew","KitNumber = " & Me.AssignKit)
if KitNumber is stored as a string in the database, then you would need to put quotes around the selection
Debug.Print DLookup("ReturnDate","Crew","KitNumber = '" & Me.AssignKit & "'")
Note that DLookup returns the first one it finds, so if you need multiple values, you will have to look into recordset functions .Find and .FindNext
I have a problem very similar to this one, but I just can't seem to solve it!
In MS Access (2003), I want to search a table based on entries in a number of fields, some of which may be empty.
I have:
text fields
date fields
integer fields, and
a memo field (but we can probably not bother searching this one if it is difficult).
They map onto a table exactly.
I am trying to create a query that will return matching rows when data is entered into one or more of these fields, but some fields can be left blank. How the heck do I do this?
A query like the one on the linked question works for text fields, but what do I do about the number fields, date fields (and possibly even the memo field)?
To give a clear example, the following code block works for TextField1, but not NumberField1:
PARAMETERS [Forms]![SearchForm]![FilterTextField1] Text ( 255 ), [Forms]![SearchForm]![FilterNumberField1] Text ( 255 );
SELECT Table1.[TextField1], Table1.[NumberField1], Table1.[TextField2], Table1.[TextField3], Table1.[DateField1], Table1.[DateField2], Table1.[DateField3]
FROM Table1
WHERE (Len([Forms]![SearchForm]![FilterTextField1] & '')=0 OR Table1.[TextField1] Like '*' & [Forms]![SearchForm]![FilterTextField1] & '*') AND (Len([Forms]![SearchForm]![FilterNumberField1] & '')=0 OR Table1.[NumberField1] Like '*' & [Forms]![SearchForm]![FilterNumberField1] & '*');
I do hope you can help. I'm sure I'm missing something really obvious, but for some reason my brain feels like it is leaking out of my ears at the moment.
Thank you!
If you need it, this is the basic design of the relevant entities:
Table1
SomePrimaryKeyWeDontCareAboutRightNow
TextField1
TextField2
TextField3
NumberField1
DateField1
DateField2
DateField3
MemoField1
SearchForm
FilterTextField1
FilterTextField2
FilterTextField3
FilterNumberField1
FilterDateField1
FilterDateField2
FilterDateField3
FilterMemoField1
You can check fo null values or cast to string
You could certainly spend a great deal of time crafting a huge and very hard to debug SQL query for this, or just jump into VBA and write some code to construct just the SQL you need.
VBA is there just for these kinds of scenario, where something is either impossible or becoming too complex to do otherwise.
With VBA, you can use an initial SELECT query that collect all the data, and then construct a WHERE clause based on the content of your search form to filter it.
For instance, I have a form like this, that allows the user to enter any criteria to filter a list of prices:
Some code to implement this could look like:
' Call this whenever the use click the Apply button '
Private Sub btApply_Click()
' Construct the filter '
Dim filter As String
If Not IsBlank(cbSupplierID) Then
If Not IsBlank(filter) Then filter = filter & " AND "
filter = filter & "(SupplierID=" & cbSupplierID & ")"
End If
If Not IsBlank(txtPartNumber) Then
If Not IsBlank(filter) Then filter = filter & " AND "
filter = filter & "(PartNumber LIKE '*" & txtPartNumber & "*')"
End If
If Not ckShowLocked Then
If Not IsBlank(filter) Then filter = filter & " AND "
filter = filter & "(NOT PriceLocked)"
End If
' ... code snipped, you get the jest ... '
' Now re-construct the SQL query '
Dim sql As String
sql = "SELECT * FROM Price"
If Not IsBlank(filter) Then
sql = sql & " WHERE " & filter
End If
SubForm.Form.RecordSource = sql
End Sub
It may seem like a lot of code, but each block only does one thing, and it's a lot easier to debug and maintain than cramming everything into a query.