I appreciate some help
I am trying to use Dcount function with 4 criteria
I have a text box in a form to count records on table_orders
The DCount function has 4 criteria:
The ID field in table_orders has to match the Form ID field
The field nature on table_orders has to be null
The field team on table_ordres has to be different than “Canceled”
The field ram (yes/no field) in table_ordes has to be “No”
I´ve been trying in many different ways with no success
This is my last try:
=DCount("*","Table_orders","ID=" & [ID]& " AND IsNull(nature)=true" & " AND [team] <> "Canceled" & AND [ram] ="No")
Try this:
=DCount("*","Table_orders","ID = " & [ID] & " AND [nature] Is Null AND [team] <> 'Canceled' AND [ram] = 'No'")
Related
I am not sure if there is another post like this but i think my question is a little different.
I am currently designing a database to track employee training given by employer. My current error I find is adding an employee by using a bound form (frmAddEmployee) to my employees table (tblEmployees).
What I have at the moment that works is VBA code that shows a notification when you enter a value into a textbox for the employee number and it finds a duplicate record in the table. The VBA will also show the record in the same form that corresponds with the value you have entered when you clear the notification.
Here's the code I use for the txtEmpNumber after update:
Private Sub txtEmpNumber_AfterUpdate()
Dim EmpNum As String
Dim stLinkCriteria As String
Dim EmpNr As Integer
'Assign the entered employee number to a variable
EmpNum = Me.txtEmpNumber.Value
stLinkCriteria = "[EmpNumber] = " & "'" & EmpNum & "'"
If Me.txtEmpNumber = DLookup("[EmpNumber]", "tblEmployees", stLinkCriteria) Then
MsgBox "This employee number, " & EmpNum & ", has already been entered in database." _
& vbCr & vbCr & "Please check the number.", vbExclamation, "Duplicate information"
Me.Undo
'show the record of matched employee number from the employees table
EmpNr = DLookup("[EmpID]", "tblEmployees", stLinkCriteria)
Me.DataEntry = False
DoCmd.FindRecord EmpNum, , , , , acCurrent
Me.cmdSave.Enabled = False
Me.cmdNew.Enabled = True
Else
Me.txtIDNumber.Enabled = True
End If
End Sub
The notification shows there is a duplicate value, for example 1234 and shows the record in the table for 1234 but when I try a different value like 5678 then it shows the value again for 1234 and not 5678.
Any ideas how to fix this problem?
I think the problem you are having is that this command:
DoCmd.FindRecord EmpNum, , , , , acCurrent
...searches in the current field. But if there isn't a current field (because the form doesn't have the focus), or if the current field is some other field (such as ID), the "find" won't find a match, and the record you're looking at won't change.
The simplest fix would be to change your code to this:
DoCmd.FindRecord EmpNum, , , , , acAll
That will search for the value of EmpNum in ALL the fields.
But, if there's a chance that the value for EmpNum might also appear in other fields than txtEmpNumber (for example, if someone has EmpNum = 12 and someone else is ID = 12), this will eventually start "finding" the wrong record.
So I think the best thing to do would be to make sure your current field is txtEmpNumber before you execute the FindRecord.
Me.txtEmpNumber.SetFocus
DoCmd.FindRecord EmpNum, , , , , acCurrent
That will ensure it only looks in txtEmpNumber when it's trying to find the record.
I have a table in access (simplified) with fields of sex, first name, last name, and phone number.
Sex First Last Phone
F Alice Smith
M Bob 111-111-1111
F Smithe 111-111-1112
M Charlie Smith
F Eve Drop 111-111-1113
I have created a form along with a query to search for the records, using the criteria of
Is Null Or Like "*" & [Forms]![Search]![Criteria] & "*"
where Search is the name of my form, and Criteria is the name of the individual entries on my form.
Right now, if I search for Sex F and the last name Smith, and leave the first and last name fields blank, I want to be able to see the records for "Alice Smith" and "Smithe". But instead, I only get the record of "Alice Smith". Furthermore, if I search for Sex F and phone number 111, I get results for Alice, Smithe, and Eve. Similarly, if I just search for phone number alone, I will get all five records.
I'm assuming that I'm doing something wrong here, but I can't tell what it is. Based on the conditional logic of or, the results should be either Null or 111 (and similar for the other fields), so I guess it is behaving as expected. I've tried xor however, and I get the same result (and even if it does work the way I'm thinking it would, it would seem bad since a blank entry would be interpreted as null instead of "search every entry". I've tried using iif, which should be the correct way to do this, but it appears to not work when placed in the criteria field. Am I missing something here?
Null is the special value (with the meaning not-known) and it is NOT the same as a blank text field, which has the value "" (empty string).
So adjust your criteria to accept the value "".
For multi-field search use criteria for each field like this:
WHERE
([Sex] Like "*" & [Forms]![Search]![CriteriaSex] & "*"
OR Nz([Forms]![Search]![CriteriaSex],"")="")
AND ([First] Like "*" & [Forms]![Search]![CriteriaFirst] & "*"
OR Nz([Forms]![Search]![CriteriaLast],"")="")
AND ([Last] Like "*" & [Forms]![Search]![CriteriaLast] & "*"
OR Nz([Forms]![Search]![CriteriaLast],"")="")
AND ([Phone] Like "*" & [Forms]![Search]![CriteriaPhone] & "*"
OR Nz([Forms]![Search]![CriteriaPhone],"")="")
In this case every condition will be TRUE if corresponding criteria field is empty.
Another way to build this query would be to build a string based upon which fields are populated.
q = "SELECT * FROM table "
w = ""
IF(nz([Forms]![Search]![Criteria1],"")<> "") THEN
w = "WHERE [table]![field] like '*" & [Forms]![Search]![Criteria1] & "*'"
END IF
IF(nz([Forms]![Search]![Criteria2],"")<> "") THEN
IF (w="") THEN
w=" WHERE "
ELSE
w=w & " AND "
END
w = w & " [table]![field] like '*" & [Forms]![Search]![Criteria2] & "*'"
END IF
IF(nz([Forms]![Search]![Criteria3],"")<> "") THEN
IF (w="") THEN
w=" WHERE "
ELSE
w=w & " AND "
END
w = w & " [table]![field] like '*" & [Forms]![Search]![Criteria3] & "*'"
END IF
q = q & w
There is a table in MS Access having 3 columns:
ID (Primary key)
Date (Date/Time)
Train no (short text)
I have designed a form where date can be selected from a combo box and according to the date selected, respective trains should be displayed.
Problem is, the query works fine on dates above 10 (like 11/1/2015) but below 10 (like 9/1/2015) it gives error : "No current record" . The record is there in the table but it doesn't display.
The query is : SELECT DISTINCT [Train No] FROM Issue WHERE [Date] = #" & dt & "#"
dt is the date selected from the combo box.
Try this:
Dim DateSelected As Date
Dim DateString As String
DateSelected = DateValue(Me!YourComboBox.Value)
DateString = Format(DateSelected, "yyyy\/mm\/dd")
SELECT DISTINCT [Train No] FROM Issue WHERE [Date] = #" & DateString & "#"
I spent some time looking around at other questions on the board, but didn't find anything similar so here it goes:
I have a report that runs up against the limits of CRM 2011 online in regards to the max number of characters in a Fetch Query. I would like to have a drop box that has an option for "All Contacts" or "Select up to Five Contacts" to avoid that issue.
What I would like to do is remove the condition from the Fetch statement if the "All Contacts" option is selected, and include the condition only if the select up to five is chosen.
How would I go about doing this?
Let me know if you have any further questions.
Nick
You can build your SQL using custom code because the SQL itself can be a string expression that you build manually. Let's say your SQL looks like this at the moment:
SELECT ThisField, ThatField
FROM Contacts
WHERE ContactId IN #Contacts
Let's change that to a string expression that builds the SQL:
="SELECT ThisField, ThatField "
& "FROM Contacts "
& "WHERE ContactId IN #Contacts "
Now we need to pull the #Contacts parameter apart and only select by ContactId when there are five or less contacts selected:
Function ContactsSQL(ByVal parameter As Parameter) AS String
Dim Result As String
Result = ""
If parameter.IsMultiValue Then
If parameter.Count <= 5 Then
Result = "WHERE ContactId IN ("
For i As integer = 0 To parameter.Count-1
Result = Result + CStr(parameter.Value(i)) + ", "
Next
Result = Left(Result, Result.Length - 2) + ") "
End If
End If
Return Result
End Function
This function builds the SQL WHERE clause to select the contacts only if five or less have been selected, otherwise it passes back an empty string. Now we use this function in our SQL expression:
="SELECT ThisField, ThatField "
& "FROM Contacts "
& Code.ContactsSQL(Parameters!Contacts)
Note that you are passing the Contacts parameter object here, not the Value property. We access the Value property in the custom code function.
I have a form record source set to a elaborate SQL select statement. That is working fine. If it helps to know, the form layout is Tabular. Here is an example of the data:
order carrier billto employee
1 smgd horm chrnic
2 axxm sele chrnic
3 smgd horm redned
4 mcta cron greand
5 mcta cron greand
Its basically unbilled order entries. I want a combo box to show distinct employee names (chrnic, redned, greand) based on the current records showing. I will be coding it to filter the form. Seems simple, but I am having trouble
Things I have tried:
Tried setting rowsource to me.recordsource, but get an
It appears that I would need to parse & edit that string
I copied the complex query & put as combo box record source & that worked to filter the form, so I know that filter logic is correct. I just want it to be dynamic so we only have to change SQL statement in one place if needed
"I have a form record source set to a elaborate SQL select statement."
Save that query as a named QueryDef. I will pretend you chose qryRecordSource as the name.
"I want a combo box to show distinct employee names ... based on the current records"
For the combo box row source use ...
SELECT DISTINCT employee
FROM qryRecordSource
ORDER BY 1;
And then to filter the form based on the combo selection, add a command button, cmdApplyFilter, and use this in its click event procedure ...
Me.Filter = "[employee] = '" & Me.YourComboName.Value & "'"
Me.FilterOn = True
If the employee names can include an apostrophe, use this for the Filter expression ...
Me.Filter = "[employee] = '" & _
Replace(Me.YourComboName.Value, "'", "''") & "'"
If you want to include a combo row to clear the filter, use a UNION query as the combo row source ...
SELECT "*** ALL ***" AS employee
FROM Dual
UNION
SELECT employee
FROM qryRecordSource
ORDER BY 1;
... where Dual is any table or query which returns just one row. Then in the command button click event you can do ...
If Me.YourComboName.Value = "*** ALL ***" Or _
IsNull(Me.YourComboName.Value) Then
Me.Filter = vbNullString
Me.FilterOn = False
Else
Me.Filter = "[employee] = '" & Me.YourComboName.Value & "'"
Me.FilterOn = True
End If
And actually you wouldn't even need the command button. You could set the Filter from the combo's AfterUpdate event.