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

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!

Related

How do I add new rows to SQL automatically by time?

I'm a pretty new programmer and I'm working on a project that I'm not sure how to make work. I'm hoping for some advice please.
Part of the project I'm working on will be used by a company to allow employees to sign up for lunch from their computers. I'm doing the project in MVC ASP.NET
The interface will look something like this:
----------------------
|1200 | Employee Dropdown Name 1
| Employee Dropdown Name 2
|---------------------
|1230 | Employee Dropdown Name 1
| Employee Dropdown Name 2
|---------------------
and on and on and on.
With this company, everything has to be recorded and stored. So, I already have a table with employee information. That will populate the drop down areas. Lunch times need to be stored in the database so it can be searched years down the line. So it has to be in a table.
The table get more tricky because not every time of the day is available for lunch (i.e. - no lunches after 0430 and before 0800).
My question is about how to create the future time slots in the database.
I could obviously make the table with all of these rows already in places for several years down the line. That's time-consuming, though, and I'll have to go back in in several years and fix it. Horrible idea.
What I'd LOVE to do is make it so every 24 hours, the database just automatically adds new rows with the next days times available - so just increment (at midnight, the program will just add the next day's times associated with that date (so at midnight on February 6, 2020, it will create February 7, 2020 0000, February 7, 2020 0030, etc. I've studied a lot but I'm still beside myself on how to make this work.
Thanks in advance everyone!!!
As I understand, you want to drive your interface from the database table so that the user can select Name 1 and Name 2 and a time slot and submit.
It sounds like you also want the available timeslots to be driven by the database also (ie, timeslot in table without names with it is availlable). This is not a good idea. As you mentioned, you would be inserting data that is not actually a record but a placeholder. That will be very confusing down the track when you come to query the data.
My approach would be to do the following:
* add NOT NULL constraints to all columns in your database (if your database supports this feature) or have your app complain very much about NULLS in any of the columns. There is no need for NULLS in your use case by the look of it.
the database should have a CHECK constraint that the time is within the allowable time range, and (assuming employees can not double book time slots) a CHECK constraint that there is no overlapping time slots, and also a UNIQUE constraint that ensures no duplicate times.... adjust to suit your needs.
your app populates times between 0800 and 1630 (8AM and 4:30PM) and also query the database for all records matching the current day so those booked slots can be removed from the list of available time slots... adjust to suit.
your app sends the user request of name and time slot to the DB. All the critical requirements are accepted or rejected by the DB schema and if there is something wrong, display an appropriate error in the app.
This way, your database is literally storing records of booked lunches.
I would NOT go down the path of pre inserting as then it becomes more complex as some records are "real" and some are artificially generated records to drive a GUI...
If you can't do the time slot calculations in your app rather than in the DB, then at least use a separate table that is maintained by a worker thread in your app OR if your DB supports it, a Stored Procedure which returns a table of available time slots.
I would use the stored procedure if I was avoiding doing complex time calculations in my app (also avoids need to worry about time zones - if you make sure to only store and display UTC times in your DB).
Having in mind structure like this:
LunchTimeSlots (id, time_slot)
Employee (id, name, preferred_time_slot_id, etc)
Lunches(employee_id, time_slot_id, date)
You need a scheduled job to add records to the "Lunches" table every midnight. How to define the job depends on your database vendor. But most of the popular rdbms have this feature. (f.e. mssql)
Despite it's possible to do what you want with db schedulers or any other scheduler, i would recommend to avoid such db design. It's always better to write real facts to the database like a list of employees or fact that lunch was served
to employee at 1pm today.
Unlike real facts, virtual data can be always generated "on-the-fly" by sql queries. F.e. by joining employees to list of dates from today till year 2100, we can get planned lunches for all employees for next 80 years.

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).

Use domain of one table for criteria in another in ms Access query?

I am trying to create a report that displays 3 different numbers for each of my projects.
Contract Hours - Stored in projects table, 1 to 1 relationship
Worked Hours - Stored in linked table that will be updated using an external website reporting feature that will contain only data for the dates that are to be displayed in the report, one to many relationship needs to be a sum
Allocated Hours - Stored in a table in my database called allocations and contains data for all dates, one to many relationship needs to be summed.
Right now i have it set up in a way that the user has to type the data range for the report every time it is run, however the date range only actually applies to the Allocation data because the worked hours data comes filtered and the contract data is one to one.
What I would like to do is set up a query that can see the domain of the worked hours and apply it as a date criteria for the allocated hours.
I have attempted to use max and min values of the Worked hours and tried to get creative but I'm actually not even sure if this is possible because I cannot see any simple solution (although I know it should be possible and fairly simple)
Any help, suggestions, or recommendations are appreciated.

Use If statement with multiple criteria in MS access

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")

MS Access- Table normalization and query design problems

I have a tricky thing I'm trying to get working
I have a table that contains events, and 10 fields populated with ID Numbers of employees who attended, and a comment box for each one. I tried to create a query that uses a combo-box with the ID Number to Search for the events they attended, and display them in a form cleanly (IE without displaying other peoples, or having a large number of text boxes everywhere). I got it partially working but I could not figure out how to go any farther. I can't figure out how to separate out the fields by the people. I was toying with the idea of having the event listed say 10 times with one person per record but that would cause alota bloat.
Any ideas how to do this? Different formats/other approaches would be great as well.
Thanks guys.
I would split the tables... have one that contains the event, with an eventID field as an AutoNumber. Then have another table called Attendance with three fields: eventID containing the ID of the event, employeeID, and Comment for the comment. This would then even allow you to create another table containing more info about the employee like first name and last name for use in reports.