Adding IF Conditional statement inside the query - ms-access

Hi,
I would like to ask help regarding my simple inventory using MS ACCESS 2013. I would like to create an automatic status update in one of my query table, as you can see on the image above there is a code written inside the status column. That code is working, if the EmployeeID field is empty it show's "available" on the status. Now, how do I display Not Available if the EmployeeID is populated?

Try with - as EmployeeID hardly can be an empty string):
Status: IIf([EmployeeID] Is Null, "Available", "Not available"))

Related

SQL/mysql - how to display two columns with different value from 1 table

I am trying to make a query for approval of documents, where the result display the name and signature with date. How can I get the date for two people approving the document?
Select Uname
case when stepcode=1 then 'approver1' end as 'name of person'
case when stepcode=1 then 'approver1' end as ' date of signed noted'
case when stepcode=2 then 'approver2' end as 'date of signed approved'
from table
I tried this, but only one result showed up. Only the name, signature and date of the first approval displayed.
We can only answer this by making some assumptions:
the field stepcode denotes what stage of the sign off process the record is at
value of 1 means noted and value 2 means approved. A value of 0 means nothing has happened yet
approver1 and approver 2 are NULL if the action has not yet taken place
If all of the above is true, then there should be no requirement to have a CASE statement for the fields... just including the fields within the SELECT statement will bring the values through if they have been completed.
Some validation of data might be required here though if you are not getting the results you are expecting. Running some rough counts for each of the steps and for where they have values in the approver fields would help to make sure your code is working. The following should give you something to work with:
SELECT
stepcode
COUNT(TableID) AS NumberAtStep
FROM table
GROUP BY stepcode
Using these counts, you can then run your statement without the CASE statements and run a manual count to ensure you are seeing the right number of records with the relevant populated columns for each step.
Further information will be required to delve into your problem further however

Access 2007 - Hide Text based on check box

I have an Access 2007 database that has a table (Fixture Schedule), a form (Input Fixtures), and two reports (Exterior Fixture Schedule, Interior Fixture Schedule).
For this database, the users will be using the "Input Fixtures" form to add fixtures to the "Fixture Schedule" database. When they've finished adding data, the "Exterior/Interior Fixture Schedules" take the data in the table and output it in a report that can be printed or exported.
Because of the nature of the table, there will be times where the data in the table for a particular record needs to stay in place, but that record needs to be flagged as "Not Used" for the report.
I've set up the table with a column labeled "Not Used", and the form successfully uses a check box to update a fixture's current "Used/Not Used" status (i.e. when the form says "Not Used", the table says "Not Used" and vice-versa).
However, I would like to have the reports reflect the "Not Used" status in a meaningful manner.
Currently, the reports will show data from the table something like this:
F1
Information about fixture
------------------------------------------
Location: Something
Min/Max: Something
Dimensions: Something
(and so on, one text box per column in the table).
What I would like to do is have the "Not Used" status trigger the Visible status of each text box, and change the "Information about fixture" line's text. It would then look like this:
F1
NOT USED
------------------------------------------
(everything below this is hidden)
I've been trying a variety of VBA scripts. Some have had me try to assign a value to the "Tag" control and use If-Then statements, but it's not working (user error, most likely).
It seems like it should be fairly easy to have the report check the status of the "Not Used" column in the "Fixture Schedule" table and adjust its output based on that, but I'm not having any luck.
Any help would be greatly appreciated.
Thanks.
In the report:
Use query to display to display [Info] or "Not Used", eg:
Display: IIF(NotUsed, "Not Used", [my info field])
Use the Detail Format event to hide fields you don't want displayed, eg:
Location.visible=not NotUsed (etc)
Set Detail section to Can Shrink=True.

MS Access Query using IFF to compare values

I am trying to build a query which will look at the data in two fields in two different tables and check to see if the data is the same, if it is I want it to return the number of times it is matched, if it isn't I simply want it to return the text saying "No viewings".
I have constructed this query in my access database which has the field from the first table "Property" and the second field I want it to compare the data with, "Viewings". I have build the following expression using the build tool, however I am stuck to make it work since every time I get this error message when trying to run the query: "Your query does not include the specified expression 'Property Viewed' as part of an aggregate function."
totalViewings: IIf([Viewings]![Property Viewed]=[Property]![ID],Count([Viewings]![Property Viewed]=[Property]![ID]),"No Viewings")
Any help how to overcome this error would be very appreciated.
Thanks
I would suggest doing something like this:
1) Assuming this is something you are developing yourself, make sure your data structure is all in order first. Since I dislike relatively code-hostile identifiers, I'd have the tables as so -
Properties - PropertyID (AutoNumber, primary key), HouseNumberOrName, Street, etc.
Viewings - ViewingID (AutoNumber, primary key), PropertyID (Number/Long Integer), ViewingDate, etc.
In the Relationships view, Properties.PropertyID would then be set up to point to Viewings.PropertyID in a one-to-many relation.
2) Your actual query I would then break into two, the first to compile the data and the second to format it for display. The first would go like this, saved as ViewingCounts...
SELECT Properties.PropertyID, Count(Viewings.PropertyID) As ViewingCount
FROM Properties LEFT JOIN Viewings ON Properties.PropertyID = Viewings.PropertyID
GROUP BY Properties.PropertyID;
... and the second like this, saved as ViewingCountsForDisplay:
SELECT Properties.*, IIf(ViewingCount = 0, 'No viewings', ViewingCount) AS Viewings
FROM Properties INNER JOIN ViewingCounts ON Properties.PropertyID = ViewingCounts.PropertyID
ORDER BY Properties.PropertyID;

powerbuilder datawindow validate records

close to 3 months into powerbuilder classic 12.5 and sql server 2008 and am working out well. I am creating a car hire system using car registration_number as the primary key.
i need to capture the car's details and make sure that the registration_number is not available. The code here is not working but that of the capacity works out...
What is wrong with the code? please give another way that works.
if i dont click on the column 'capacity' and click on save, the program goes on and saves the record even when it is an empty field. how to i avoid that.
String car_registration_number
car_registration_number = dw_newvehicle.GetItemString( Row, "registration_number" )
long error_code = 0
string column
column = dwo.name
choose case column
case "registration_number"
if data = car_registration_number THEN
messagebox("validation error", "You cannot available regno")
error_code = 1
end if
case "capacity"
if integer(data) >10 then
messagebox("validation error", "a car's capacity cannot be more than 10...")
error_code = 1
end if
end choose
return error_code
Instead of GetItemString at the top, you would need to do one or two finds in the DataWindow for the registration number in data. If the first find returns 0 you are OK. If it returns the current row, you need to search from row to the last row unless you are on the last row, which you may well be if you are inserting. However, I wouldn't do it that way because it won't work in a multi-user environment. Instead, you should go ahead and insert the row and then if you get a duplicate key error, tell the user the registration number is already in the system. If the user is going to enter a lot of data you could try to SELECT the registration number from the database when the user enters it, but you still have to be able to handle the duplicate key error.
I don't understand what kind of check you are doing here for registration_number : you are getting the current value of the column in the DW into car_registration_number then you are comparing to the value that was modified (the code comes from the itemchanged event ?). Do you expect to enter a value that is different from the current one ?
Also beware of the GetItemString if the type of the column is not a text (as it is called ..._number) as PB may crash, return some null value or silently fail (depending on the PB mood of the moment ;). If you get a null value, your if check will always fail.
If you want to get numerical values, use GetItemNumber instead.
you can set the capacity column to be not null in you database. When saving the DW, you will get an error telling that there is some required value that was not set.
A better idea would be to iterate in the UpdateStart event on the columns that are member of the table primary key and to check if they are set or not.
To write some dynamic code, you can get at runtime the columns from the DW by describing the datawindow.column.count then checking the pk members with #1.key (replace #1 by #2, #3, ...) if the check must be done on that column.

How can I display a *foreign* field value in a text box?

How do I bind a text box with a field, which doesn't belong to form's "Record Source" table, through the Design View?
Example: I have "Order.cust_id" (Record Source=Order) and I want to display "Customers.name". I believe it is trivial but I have no experience with MS Access. I tried to use the text box "Control Source" property but no luck.
One method would be to convert the text box to a combo box. Then set the row source to include both the cust_Id and the Customer.Name from the customer table. SQL statement example
Select Cust_ID, Name From Customer
Order By Name;
By setting the number of columns to 2 and the column widths; the first column as zero (i.e. "0;6") then the foreign key would be hidden from the user and the customer name would be displayed.
Note this method does force you to have limit to list set to true.
Also you do end up with a drop down list which may not be what you want.
You can use DlookUp as the control source of a textbox:
=DlookUp("[Name]", "Customer", "ID=" & Cust_ID)
Syntax: What to look up, table name, where statement
The Where statement should follow the rules for Jet SQL, which means that you must use delimiters if the field is text or date format.
Note that Name is a very bad name indeed for anything. I suggest you rename the field immediately before things get worse.
It can be useful to know the error(s).
You could create a new View (e.g. OrdersAndCustomerNames), select all the columns you want to use in the form, then instead of using the Order table as Record Source, you would just switch to OrdersAndCustomerNames. You say you have no experience with MS Access, so I am guessing you are not building anything huge and overly complicated, so I would do it this way. I am quite sure it can be done more elegantly but this will do for now.