MS-Access Looking up and updating records based on values in form - ms-access

I've learned a lot in Access over the last month or so but am now have trouble resolving an issue that I don't know how to approach. I'm sure there's a simple solution but I need help being pointed in the right direction. Here's the background:
This problem deals with three forms:
frmProject - Lists project details it has a
subfrmMilestones - Milestone details
frmProjMsgBox - Collects info to pass to Project table
I have it set that when I click OK on ProjectMsgBox it passes the information to the Project form and starts the creation of a new record.
tblProject stores all details related to projects
tblMilestones stores all details on milestones, contains key for tblProject
tblMilestones_Inf stores a list of common milestones based on project type, has key for tblProjectType, which is on tblProject
My Problem -
When I click okay on the ProjectMsgBox form, I would like it to also look up milestones from tblMilestones_Inf and insert into tblMilestones. I also need to assign the Project_ID that on the form to the creates. What is the best way to go about this?
I've tried a few things in VBA but haven't had success. I can create a query that pulls the milestones in but don't know how to update the milestone table with the relationship to the project table.
I can provide more details but wasn't sure what is helpful and what is not. I'm more or less looking for a resource (or keywords to search) so I can figure this out on my own
Thank you!
UPDATE:
tblProject has 1 to many relationship with tblMilestones
tblProjectType has 1 to many relationship with tblMilestones_Inf
Once I hit okay on the frmProjMsgBox it fills out text boxes that have controls on frmProject to create the parent record (i think that's the term) in tblProject. This works well. I would like access to go out to the tblMilestones_Inf and match the project type (child record?). It would then add 4-5 records based on matching ProjectType on the Milestone_Inf table. I don't want code, but maybe actions/keywords to lookup so I can figure this out on my own. I'll post some of my code later that tries to achieve this.

You might want to look into Query Defs, you can use those to change the SQL statement of your queries from VBA. So if you have something like:
MyQryDef.SQL = "SELECT * FROM MyTable WHERE MyIndex = " & MyControl.Value & ";"
and your control value is set to 5, you then use the execute statement and your Query SQL will then read:
SELECT * FROM MyTable WHERE MyIndex = 5;
There's a few more lines you will have to look up to make this work, but this is a very basic example.

Related

Saving a separate record when saving the current record

In my MS Access application the user opens the database to enter research notes (RNs). Each RN record needs to be attached to a project. All research notes entered during the current session relate to a specific project, which the user nominates on the first form. However, the RN - Project relationship is many-to-many. Although a single Project is attached when the RN record is created, other projects can be related later.
So... when I create and save a new RN record, I want to create a join table record with the new RN ID and the ID of the selected Project. I cannot find a way of creating this second record when creating the RN record. Does anyone have a way? Thanks
As June7th implied, It doesn't sound like you have ruled out the normal access forms/subforms approach. create a new database then insert the tables behind your many to many relationship. Then go to the database tools tab and tell access about the many to many relationship. For instance:
next select either of your main forms and then on the create tab select form and access will use the information about the relationships to auto-generate a rather ugly but working form and subform:
Form Prettification is Recommended before giving to actual users. Play around with the circled record selectors and try entering some data. Then check the tables and you will see that they have been properly updated. This approach may not work for your particular case, but try it because it is the easiest and fastest approach. Next approach is to add a button which associates a note and a project then runs some vba to add them to the junction table between notes and projects.

Multiple table update design in Access

I have learned more, and was compelled to find the solutions, mind changed.
PS dear experts, your help is appreciated in the time-saving nature of having forums and discussion in the first place, and also is mined for usefulness, not street cred. get helping or get off. The time I wasted reading posts where the answer was "I don't understand what you are trying to do..." and then questioning the OP with animus or incredulity, or suggesting some unrelated answer further confusing issues, is seemingly the problem with the world these days, if you want to help, help
Original Post:
Ok so as per the comments, thanks to ANYONE who volunteers any help with this problem.
I have a table and relationship design problem.
I have a table with a pk auto and I want to have a related table with a related column incrementally numbered that updates every time new data is entered in the form that is bound to it. It needs to update the related rows in the autonumbered table's key.
---the answer was to join the table on the autonumbered field.
The autonumber of the first table (main recordsource) is just an ID. I think I need two Primary Keys as I need to update the related record with that number in the entry form and move to NextRec but update the pk in the main table and move to NewRec , how do I join (see jpg image)?
I want enter results and update that existing record but update the adjoining pk so that a new record is created in the main table.
Tourney
more in depth:
From yours
My desired form
note that the calculation table feeds the upcoming games table, where a query shows the players recent results. I would like to split the upcoming results to show the players' recent game history, the second tab I would like to enter either one result or many results at a time. I hope I am clearer. You can see why I have had a challenge. See my form though. The recordsource is the upcominggames table and the data entry form is for input (ENTER GAME DATA)
-----the answer to this was an update query (a separate form) and then requery the statistics form to show the new matchups that were entered.
As from the comments, this is what I'm thinking of when I read your description:
Since the UpcomingGames will be entered first and exactly one GameResult can be entered per UpcomingGame, this will be a 1:1 relationship. As the name Upcoming says: The Upcoming data has to be entered before the Result can make sense. Unless an UpcomingGame can be cancelled, there will indeed be a Result for the Game, so there is no need to separate the information into 2 tables. I'd say, a user interface could look like this:
As you can see, the T_NUM column is an autovalue. Before entering any data, I initialized that column using a query like this (and deleted that record afterwards):
INSERT INTO Games ( T_NUM )
VALUES (1004);
This way, the numbering started with number 1005.
You won't be able to to avoid gaps in the numbering, as long as the users can remove existing records or cancel the insertion of a new record. If you want at least to avoid the latter, you will need some VBA code in the form.

How to eliminate database table row duplication

I have a question on databases and how information is displayed in regards to Primary and Foreign keys.
For example, there are three tables; Employees, Employee_tickets and Employee_comments.
Each employeecan have multiple tickets and also multiple comments. A foreign key is placed in the Employee tickets and Employee Comments table. My application is built in vb.net with Visual Studio and it is a desktop application. How can I query say.. Employee Name ('Jon Doe') and display all of his tickets in a grid as well as all of the comments people have made on him over time? I have created a View on the sql database which returns all of the information I require but for each ticket listed under ('Jon Doe') the View displays and Employee Name for every single ticket. Is there a way to display the employee name only once and then every ticket listed under that particular individual without displaying the Employee Name again or do I have to make Separate windows to segregate all of this?
This seems like a really dumb question and I cannot for the life of me figure out how to correctly display what is required in this situation.
Here is an example of what I am trying to explain:
So for troy there is one employee name entered in the Employee Names table, There is one CWB ticket entered in the CWB table but there are TWO PQ Cards entered in the PQR Ticket table. How Can I Display only one row for Troy and one Row for his CWB because there are only one of each entered in the tables then the two rows for the PQR Cards under his name?
I have created a view which gathers this information all into the one single view itself then bound the datagridview's to this View.
Your problem has nothing to do with databases. Rather, the issue is that you have an entity (the employee) that has two separate collections associated with it (tickets and comments) and you want to show the contents of both collections.
Doing this in a datagrid is difficult because in its simplest incarnation it's intended to show one collection of like items.
I can think of a number of possibilities:
In your code, convert each collection to a single string value and display that single string value on the row with the employee's name. This conversion could be to comma-separate a stringified version of each item in the collection (as suggested by BS123 in the comments) or could simply be a summary (eg "5 Tickets").
Put the basic employee information in one data grid and then have two additional data grids below it, one bound to the Tickets collection and one to the Comments collection.
Embed data grids directly in the main data grid, one in the Tickets column and one in the Comments column, and bind each one to the appropriate collection in the employee.
Your database structure is correct so don't change that, you simply need to solve the issue of presentation.
What you're missing here is a controller between your view and your model. Your view is presenting exactly what it was given to present - it's up to you to format it.
There are several possible solutions to this, and the correct one partially depends on needs and infrastructure.
If you infrastructure is solid and your needs are near real time, consider dropping separately querying to fill your second and third tables based on what is picked in the first. This will increase the load on the database, but your data will almost always be correct, and the data will come from the database the way you want to see it.
If the database-centered solution is not good for you, LINQ provides some good ways to filter your data into typed collections that would present exactly what you want the user to see.
To get the users:
Dim users = From l In data.lines
Group By FirstName = l.firstName, LastName = l.lastName
Into Tickets = Group, Count()
You can then present this object to your grid. While dynamic typing works here, I think it would be easier to manage view interactions with defined classes. I'll leave that part up to you. Do some searching on LINQ to fill in the rest of the blanks. It's pretty neat stuff.

How inefficient would this query be and also table structure?

I have two tables. One table stores course_updates, which basically has a new row pushed to it everytime someone adds or drops a course. I also have another table follower_updates that has a record pushed to it whenever someone follows someone. I want to be able to get the information for the logged in user, but I'm unsure how I should detect which table the information is coming from being that I want to display the information based upon which table it is from. Should I make a new column that update_type, or should I have a different method?
I'm also going to show what I'm thinking relatively in terms of sql. It won't be perfect because I haven't tested it yet. This is just a sample. I didn't want to bring in my current query because just the course_updates already has three inner joins and an outerjoin, so I tried to streamline the content for this question. thanks!
SELECT * FROM course_updates WHERE (establishes connection
enter code herebetween user and courses and followers)
UNION SELECT * FROM follower_updates WHERE followee.id = currentUser.id etc.
Don't use a UNION. Use two separate queries instead.

MS Access - how to create a label on a form that populates data from another table

I have a fairly simple database that I inherited. For the purposes of this question, there are two tables:
Mastertable and Providertable.
Mastertable references Providertable thruogh provid, which is a FK to Providertable PK (provid).
So it looks like this:
Mastertable:
acct (PK)
(other fields)
provid (FK)
Providertable
provid (PK)
provname
provspecialty
Simple right? However, the Mastertable!provid field is actually a lookup table which displays Providertable!provname but stores provid. There is a form the users use to populate the Mastertable, and it has this lookup field shown.
The users now want to show the provider specialty based on what they select as the provid. I can't figure this out to save my life. I'm pretty well versed in SQL, having written many stored procedures and created a few db apps using .NET, but this is quite challenging. I tried creating a lookup field called provspeciality, but that's not what they want. I tried changing the "OnUpdate" event for the lookup field to point the Provider Specialty label to the right thing.
Right now, I can't even get a simple select going that joins the two tables since they are using this lookup field as the FK and Access I guess can't understand it. Any help appreciated.
Since the Mastertable provid field is a lookup type, the displayed value is the lookup value rather than the value which is actually stored in the field. This query will show you the stored provid values.
SELECT acct, provid
FROM Mastertable;
And I think you should be able to retrieve the matching provider specialties with a query similar to this:
SELECT m.provid, p.provname, p.provspecialty
FROM
Mastertable AS m
INNER JOIN Providertable AS p
ON p.provid = m.provid;
You may even be able to use that query as the Row Source for a combo or list box on your form. Make provid the bound column. You may wish to set the provid column width to 0" so it is not actually displayed in the control, but still stored in Mastertable.
I think you should modify the table to make provid a normal (numeric?) field instead of a lookup. Fortunately you indicated this is "fairly simple database", so that will hopefully limit the amount of additional changes you need to be compatible with the redesigned table. Good luck.
I created an "On Change" event for the form field "provid" which is a lookup field that displays the provider name while putting the provider id into the master table as a FK. However apparently Access is not able to do lookups based on this field (or I am doing something wrong) using queries - as shown above in comments. What I did is use this as the event code. Important, you must enable macros for this to work!
Private Sub provid_Change()
Me.txtProviderSpecialty = DLookup("Provspecialty", "Providertable", "provid = " & Me.provid.Value)
End Sub