MS Access: Using parameters with hyphens in a parameter query - ms-access

I have serial numbers in the form X-xxxx, where X is an alpha char and x is numeric, I can't seem to get the parameter passed in correctly.
One thing I've tried, which actually works properly in a different database of similar info, is:
Like [Enter Serial Number:] & "-*" Or [Enter Serial Number:]
Any suggestion would be greatly appreciated

It appears to me you want to allow the users to search 2 ways:
enter the full serial number
enter only one character (and return all serial numbers which start with that character)
If that's what you're after, try a query like this:
PARAMETERS [Enter Serial Number:] Text ( 6 );
SELECT p.*
FROM tblPingPong AS p
WHERE
(((p.serial_num)=[Enter Serial Number:]))
OR (((p.serial_num) Like [Enter Serial Number:] & "-*"));

If you are asking the user for the numeric portion:
Like "*-" & [Enter Serial Number:]
However, this will be slow unless your table is quite small. You might like to consider getting the input for the query from a form.

Related

Use IIF and LIKE statement in critera column in queries, MS Access

I have homework from Microsoft Access for school and one of the tasks was to make a search form in query, where you type for example 1. grade, 2. grade or 1st year, etc... But the field contains the name of the classes such as IT1, IT2, E1A, E3C, etc... So I could not just make a search form with [Enter Class:] or Like [Enter Class:]&"*" in the criteria column. So I was thinking, that I can use the IIF statement but it doesn't work how I imagined. I think there is a problem with the LIKE statement. I read on forums, that the IIF statement should be used in the field column but I tried the simple examples in the criteria and it worked just fine. So my question is, how I can make a search form, where I type numerous letters with one certain number in it BUT only the number will be read and returned with the same number in classes. Example: I type to search 1. grade and the value will return classes IT1, E1A, E1B, E1C, E1D. This is the line I used in the criteria column:
Like IIf([Enter the grade:]="*1*";"*1*";IIf("*2*";"*2*";IIf("*3*";"*3*";IIf("*4*";"*4*"))))
Just to qualify, I'm beginner in access and databases overall, so there is big possibility, that I'm missing something.
Thank you for help! Cheers!
Assuming input will always start with a number and you want all values containing that number, consider:
WHERE fieldname LIKE "*" & Val([Enter grade]) & "*"
Also assumes values contain only single digit.

How can I correct this code to be able to get a difference in weights for each client?

I am working in MS Access. My end goal is a database report that shows the total amount lost so far for each client, based on the texts they send in daily. I've been able to extract the number from the message, but I need to be able to have a running report each day with how much they've lost since they first texted in. I tried this solution with no success: Access SQL Query: Find the most recent date entry for each employee for each training course
After that, I'v been working with the code from here: https://www.techonthenet.com/access/queries/weight.php. I've tried to write a query that brings up the Last Visit Date, but I keep getting type mismatches with this:
DMax("LogDate","NumberQ","TargetFK=" & TargetFK)
NumberQ is a query, where columns are TargetFK (Short text), FirstName (Short text), LastName (short text), LogDate (Date), Message (short text), and Weight (calculated field).
My question is, what am I doing wrong, and is there a better way to do it?
Since TargetFK is a text field, you need quotations around it:
DMax("LogDate","NumberQ","TargetFK='" & [TargetFK] & "'")

Reusing Parameters Passed to Query to Generate Form

I have a form in MS Access that uses a query to generate a form. The query looks something like this:
SELECT *
FROM PEOPLE
WHERE LAST_NAME LIKE [Enter Last Name] & "%";
This of course creates a pop-up to enter the last name to search. I want to do an action on the parameter [Enter Last Name], but I cannot figure out how to access this parameter. In a report I just set the value of a textbox to =[Enter Last Name]. Can anyone help me figure this out?
One thing you could do would be to add the parameter as an output column, e.g.
PARAMETERS [Enter Last Name] Text ( 255 );
SELECT People.*, [Enter Last Name] AS theParameter
FROM People
WHERE (((People.LastName) Like [Enter Last Name] & "*"));
Then you could refer to the [theParameter] column in the result set.

access 2003 text display leading zero

We are currently using Access 2003. I have a vehicle maintenance log which has a text field for vehicle numbers. The field needs to be text as it will have a R in it when a vehicle is retired. They would like to have the field be a four digit number with leading zero's. So vehicle 22 would be displayed in the table and reports as 0022 and when retired it would be R0022. I have tried to change the format of the field to "0000" and "0000"# but neither of these will display the leading zero's.
Do you really want you users to manually edit that field?
I don't like solutions like this, because it's error-prone (unless you check a lot of things to make sure that no one enters invalid data) and feels unelegant to your users.
I would just save the following in the table:
the vehicle number in a numeric field (22, not 0022)
a boolean field which indicates if the vehicle is retired
This is much easier for your users to work with:
they can just enter new vehicle numbers without having to think about leading zeros
to retire a vehicle, they just need to set a checkbox, instead of putting the right letter in front of the vehicle number
Plus, showing the desired number R0022 now becomes just a matter of displaying/formatting the data from the table:
Public Function GetDisplayNo(VehicleNo As Integer, IsRetired As Boolean) As String
GetDisplayNo = IIf(IsRetired, "R", "") & Right("0000" & VehicleNo, 4)
End Function
You can use this function like this:
GetDisplayNo(22, True) returns R0022
GetDisplayNo(22, False) returns 0022
And if you need to display a list of vehicle numbers in a report or a continuous form, you can directly use this function in the underlying query:
SELECT
Vehicles.VehicleNo,
Vehicles.IsRetired,
GetDisplayNo([VehicleNo],[IsRetired]) AS DisplayNumber
FROM Vehicles;

Access / DCount gives a TExt field and not INT field?

In Access 2010, I have two tables 'Contact' and 'PhoneCalls'.
I created this Query for 'Contact' as I want to see how many times I called a contact.
Query:
SELECT Contact.*, DCount("[ID]","ColdCall"," [ColdCall]![ContactID] = " & [Contact.ID]) AS Call
FROM Contact
I have build this query using the following expression:
Call: DCount("[ID]","ColdCall"," [ColdCall]![ContactID] = " & [Contact.ID])
It works fine except that it creates a TEXT field instead of a NUMBER field. For instance, I need to sort this query out, but I can only sort it "A to Z" and not "smallest to largest" as it should be.
Do you have any idea on how I can solve that?
You could use CInt() to force the call count to be an integer:
SELECT Contact.*, CInt(DCount("[ID]","ColdCall"," [ColdCall]![ContactID] = " & [Contact.ID])) AS Call
FROM Contact;
Note also that using DCount() in this way is rather inefficient. If that approach works to your satisfaction then continue to use it for now, but don't be surprised if it starts to bog down as the tables grow.