Use If statement with multiple criteria in MS access - vba

I have one table called "EmployeeHours" where I have following columns:
Agent_Name Agent_Number Start_Date Time State Duration week Team
Under State column I have "After Call", "Training", "Lunch", "Tea Break" etc. Out of these "After Call and Training" are considered work hours and others are "Non work Hours". I want to add a column to this table which does this identification of work and non work hours.
I know How to do it in Excel: I have done it using the IF OR condition but I am facing difficulty in doing the same thing in MS access..
Anyone knows how to implement this IF OR Criteria in MS access and prepare that column?
Any help will be appreciated.

Something you can do that does not fully normalize your database but still gives the functionality you are looking for is to create a mapping table where you will have the different states ("After Call", "Training"...) and another field where you will have its correspondance to Work / Non work hours.
When this is done, you can create a query to calculate the Work / Non work correspondance by putting both tables in the query and linking on the Status field.
If you want you can also take this opportunity to create a table "State" with the different states in text format and with a uniquely identifying surrogate key (think Autoincrement) and replace the values in your current table with those. That way you will not sure as much text in your database (lighter) and you will be able to change the description at a single place instead of all the occurrences if you ever need to.

In a stored query, table calculated column, or form control you can use the IIF() function.
IIF([State]="After Call" OR [State]="Training", "Work hours", "Non work hours")

Related

Updating selected data in table/form on change of a field in form with a variable

I'm quite new to MS Access, but I'm trying to set up a database for building management and regulation in MS Access, which is going to be used for energy use modelling of the building. I therefore have multiple tables which i control with forms. There are multiple zones in a building with different occupations, e.g. one zone is offices, the other a recreational area. In those zones are multiple rooms which have the same occupation. I have a form which provides an overview of the zone, with data such as area size, floor nr etc., but also with a list of rooms that are in that zone. The zones with respective occupation and other data are in one table, the list of rooms is a different table.
What I want it to do, is that when I change the occupation of a zone in the overview form, all the rooms in that zone also change to that occupation.
Info:
Zones table contains fields 'Zone_code', 'Occupation' and non related zone specific data
Rooms table contains fields 'Room_ID', 'In_zone', 'Occupation', and room specific data
I've tried using macro's on the 'Onchange' event, where I would select the table, select the data where the zone are equal, and than set the value to the changed occupation. I've also tried it with selecting the subform of the roomlist. I've also tried using an update query, but I encountered errors updating using a variable.
I had to do the same for the floors, where there are multiple zones per floor and have a table for that overview. I'll show the macro that worked there, but when I used a similar one for the rooms, I got errors such as 'The object doesn't contain the Automation object "Roomlist"'. I even tried update queries but I feel lost.
The macro that worked for the floors where 'main' is the main building table with the floors:
https://imgur.com/a/3rED1Pw
The macro i used for the rooms:
https://imgur.com/gvMDIzq
I used the iif statement to compare zones and only update if the zones are the same but this macro crashes, other macro's i made only added 1 new room with the new occupation, but nothing else.
I expected the macro to just run down the table of the rooms and change the value of the ones where the zone code is the same. Or at least, that is what i hope the database will do eventually.
Can somebody help me figure this out so that it works? I feel I'm missing or overlooking one small thing but I don't understand access enough yet to figure out what. I don't mind if the solution uses a completely different aspect of access, as long as it works. Thanks in advance
Let me start out by saying welcome to the site and MS Access! To answer your question, I would start by looking at your data setup. As mentioned by June7, having Occupation in both tables independently is not necessary. Think of it this way: if I have a value I want to update (such as in this case with your question), it is much easier to update in one location than in multiple locations. From what you've shared, [Occupation] only applies to the [Zone_Code] table and should be present only in that table. Through the relationship of [Zone_code] to [Room_ID] through the [In_Zone] field, [Occupation] of the zone that the room is part of can now be queried.
Another thing to consider is having a table, say 'tbl_Occupation' or something of the like, where you could have the fields [OccupationID] and [Occupation]. Doing this could make it easier to maintain the occupations for which a zone can be assigned (if the reference name of an occupation changes but it is not a different occupation, etc.), and also stores occupation in one location, so that if an occupation is assigned to multiple zones, the ID is referenced instead.
I hope this helps!

Querying via a form with value from textbox as criteria

The situation: metadata about biological specimens are collected in an Access table. The specimens come from human patients and patient data are collected in a separate table. To limit the amount of private health information we have hanging around, the patient database must be updated with new patients only when we actually receive samples from them.
So that the data entry workers know when they need to update the patient table, I want a button in the specimen data entry form that will pass an entered patient id value as criteria to a query.
The query looks like this right now:
SELECT Patients.[Patient id]
FROM Patients
WHERE (((Patients.[Patient id])=[Forms]![Specimen entry]![patient id]));
but it never has results, even when I run it from records that I know correspond to patients in the patient table. How do I fix this?
Suggestions about what to call this situation so that I can make better searches about it would also be appreciated. I'm an Access novice.
The query looks correct, but make sure the WHERE clause is comparing numbers to numbers or strings to strings (not a number to a string). Also confirm that the form and textbox names are correct. A quick test using your query worked for me.
Depending on how you plan to present the information, you can also dynamically create the query in VBA and then pass the information to the form.
For searching, I'd recommend some combination of access, dynamic, query, and vba.
alternative option
If you're only looking to see if a single patient exists in the table, it may be simpler to use the dlookup function:
If IsNull(DLookup("[Patient ID]", "Patients", "[Patient ID]='" & Me.Patient_ID & "'")) Then MsgBox "does not exist"
This will check to see if the patient exists (return a number) or does not exist (returns NULL).
https://support.office.com/en-us/article/DLookup-Function-8896cb03-e31f-45d1-86db-bed10dca5937

Disable Query in Access with VBA

A coworker in accounting was complaining about how she ran a query twice and it doubled her values and she got confused. Im just a Junior IT person who has very little VBA experience. I am basically just trying to add code to make it so my queries in our databases can't be run more than once unless you restart the database. I was thinking of doing a boolean check to see if a query has been run and if it has don't allow it to be run again. Or maybe I could just do a simple if statement. Please let me know if you have any input on this issue.
I couldn't find anything on the Googs either.
I would think on a date and a session ID as default values in each table, you could code the addition of both etc.
These are populated, date =date() as default value and sessionID is the DMAX from your SessionID table, as extra column in said query.
This SessionID table is incemented by a startup popup form, running macro.
The Primary Key of each table being operated on would be the date and the sessionID not allowing dupes. You probably dont need the date, just a sessionID in the PK.
It is not always the best idea to implement ad-hoc ideas by users like this.
You should analyze what happened here, and make sure it cannot happen in the application design, not by arbitrary rules.
Example: If the update query adds fees to a bill, and this must happen only once per bill, then the update query should also set a flag "fees added" in the bill record. And it should not update bills with this flag set.

Access 2010 VBA and filtering

Good Day,
I need some assistance please. I am rebuilding a third party Access database after it's catastrophic failure and the failure of the 3rd party developer to fix his mess. I am an avid Access Developer and know my way around an Access Database well. I am not a super VBA coder, but I can do more than my bit in VBA as well as a few other languages.
Currently, I have a database test bed with a login form that stores 3 values as public variables (gstrLevel as String, gstrUser as String, gintID as Integer). I am able to set my own "permissions" with ease in that once they login I can use the values to control the switchboard etc.
My problem now is the following. We have people recording prospective client interactions and interviews. As part of the process, every time they make contact, they record this into the database and in the process a "followup date" is created. This works like a charm. What I now need to do is warm the users if these followup dates are close or have passed so that prompt action can be taken lest we lose a prospective client.
I have a query that takes the prospective table information, and the notes table information (where the followup date is stored) and then filters the dates correctly. This in turn has been used to create a continues form to display the records that need followup soon. What I cannot seem to do is to get it to only show the logged in users followup records.
I should note that the user/agent field is a lookup field in the prospective table, and thus also creates a combo box in the Followup form. I can lock the form from changes etc, but I can't seem to get it to only display the relevant user/agent details.
I have tried:
DoCmd.ApplyFilter
Me.Filter
Me.FilterOn = True
DLookup as criteria
Using my public variables directly as query criteria
And a few other weird combinations with no success.
My problem, I believe, is the fact that the user/agent is a lookup field, and I am not sure how to filter based on that fact. If I use the gstrUser variable directly I get a type mismatch and if I use gintID directly it shows nothing.
Any ideas or advice would be greatly appreciated.
You probably have a Users table, with a numeric Primary Key (PK), while your gstrUser stores the userName.
So in the source of your Followup form, just add the Users table (joined to the Prospective table on UserId) and apply the filter on the userName field in the Users table (or whatever it's called that matches the contents of gstrUser).

Database Design: Line Items & Additional Items

I am looking for a solution or to be told it simply is not possible/good practice.
I currently have a database whereby I can create new orders and select from a lookup table of products that I offer. This works great for the most part but i would also like to be able to add random miscellaneous items to the order. For instance one invoice may read "End of Tenancy Clean" and the listed product but then have also an entry for "2x Lightbulb" or something to that effect.
I have tried creating another lookup table for these items but the problem is i don't want to have to pre-define every conceivable item before I can make orders. I would much prefer to be able to simply type in the Item and price when it is needed.
Is there any database design or workaround that can achieve this? Any help is greatly appreciated. FYI I am using Lightswitch 2012 if that helps.
One option I've seen in the past is a record in your normal items table labeled something like "Additional Service", and the application code will recognize this item and also require you to enter or edit a description to print with the invoice.
In the ERP system which we have at work, there is a flag in the parts table which allows one to change the description of the part in orders; in other words, one lists the part number in the order and then changes the description. This one off description is stored in a special table (called NONSTANDARD) which basically has two fields - an id field and the description. There is a field in the 'orderlines' table which stores the id of the record in the special table. Normally the value of this field will be 0, which means that the normal description of the part be displayed, but if it's greater than 0, then the description is taken from the appropriate row in the nonstandard table.
You mean something like this?
(only key attributes included, for brevity)