Editing a Form in MS-Access - ms-access

I am working with MS-Access and I have created a Form(1) that consists of data from a Query(1) and a subform with data from a Table(2). Along with that, I have the Form(1) in a Split Form View displaying the datasheet at the bottom. The problem I am having is that I want to be able to completely edit everything as well as Add New Records(main problem) in the Form but, Access is not allowing me too. To understand my problem better, I have added some descriptions of the tables and attached some images below:
Table 1: Sales -
Fields: SalesID(AutoNumber), Customer(Short Text), and Date(Date/Time)
Table 2: Transaction -
Fields: TransactionID(AutoNumber), SalesID(Number), Sales(Currency), Cost(Currency), and Profit(Calculated, Currency -> [Sales]-[Cost])
Relationship: Table 1: SalesID(one) -> Table 2: SalesID(many)
Query 1:
Form 1:

I created a form/subform arrangement like you describe. No problem adding new record. However, getting the aggregate query to update requires code to requery the form's data. So in the subform's AfterUpdate event:
Sub Form_AfterUpdate()
Me.Parent.Requery
End Sub
One drawback to this may be that focus will be returned to first record of the main form.
UPDATE: Learned something new - Me.Parent.Recordset.Requery will not return focus to first record.

Related

How can I input data for a many-to-many table in Access?

I'm sure that this is a basic question but I'm struggling to get to grips with Access.
In my case I have two tables: Student and Subject, linked by a junction table, StudentSubject. Student and Subject have a many to many relationship, as one student can study multiple subjects, and one subject can be studied by many students.
What I don't understand specifically is how to input data so that each student can study more than one subject. I've created a Form with a Subform that looks like this:
When I try to input anything into the "SubjectName" field, it says "Field cannot be updated". If I press "Okay" the message goes away and I can type something in the field, but as soon as I try to put something else in the record below for "Subject", the same error comes up. Then when I press the arrow to go to the Next Record on the Form, and save the form, nothing has changed in any of the tables.
Here are the three tables and their relationships:
I'm not too sure what I'm doing wrong here, but the end goal is that I can input a student and also input all of the subjects that they are doing, so that each student's individual list of subjects is stored alongside their name. I don't know where the "Field cannot be updated" error is coming from.
Could anyone advise?
You need 3 forms:
One form for students,
One form for subjects
One form for the Student-Subject table
The Student-Subject form will be a sub-form in both of the other forms. This form can have combo-boxes for the student and subject. For example, here's what editing a student might look like:
This way, you can easily add new subjects a student is taking (and similarly, add new students that are taking a subject).
Note that both the Subject and Student columns are being shown. What I've done in the past is have a bit of VBA to hide the appropriate column based on what the parent form is:
Option Explicit
' note that this code goes in the Student-Subject form.
Private Sub Form_Load()
If Me.HasParent Then
Me.cboStudentID.ColumnHidden = (Me.Parent.Name = "frmStudent")
Me.cboSubjectID.ColumnHidden = (Me.Parent.Name = "frmSubject")
End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''
Public Function HasParent() As Boolean
On Error GoTo err_handle
HasParent = Not Me.Parent Is Nothing
Exit Function
err_handle:
HasParent = False
Exit Function
End Function
This way, the appropriate column is hidden based on whether you're editing a student (and the Student column will be hidden) or editing a subject (and the Subject column will be hidden).
Typically you have two different views on such a multi to multi relation. One is you are in the student's view and the other is if you are in the subject's view.
In the case of the student's view you must create a form to edit the student's table and you have to add a subform to edit the detail data table, i.e. StudentSubject. This detail table has to be synchronized with your StudentID from the main form. Usually this is already done by the wizard. But please be aware that you have only the ids in the detail table. To display a readable information you need to change the text boxes to combo boxes and to bind the combo boxes to the underlying tables.
Because you have only numbers in the detail table StudentSubject you cannot input text directly into this box. If the SubjectName has not been registered in the table Subject you have to add the new entry before you can select this. To dynamically add a new text entry you may use the "Not in List" event to handle this.

MS Access popup form conditional on combobox value

I am working on a database to store a heap of animal behavioural observation data and am working on creating a form to input data (one of many forms). The main form feeds into a table called "FocalSample" and the subform feeds into a "FocalData" table.
This is the form at present
There will be multiple records in the subform to a single overarching sample, a sample is a 10 minute period and a single animals behaviour is documented over that 10 minutes, noting time in minutes and seconds to calculate duration of time spent in each behaviour.
I need popup forms to come up depending on what is selected in the "Behaviour Type" field of the subform, as the kind of data recorded for a "feed" observation compared to a "social" observation is very different. I have created separate tables for the different behaviour types of data and forms that are set as "popup" forms, but need help with the code to get it to do what I need! :P
I have been trying a variation of the following code that I found here (http://www.justskins.com/forums/pop-up-form-based-238440.html)
Private Sub Campaign_AfterUpdate()
Dim strFormName As String
Select Me.Campaign.Value
Case "In Honor", "In Memory"
strFormName = "HonorMemory"
Case "In-Kind"
strFormName = "InKind"
End Select
DoCmd.OpenForm strFormName, , , , , acDialog
End Sub
But I keep getting an error message after the first row.
Here is the error and code screen
Similar to the other forum link I attached, I also need the popup form to draw a lot of info from the main form, but I'd just be happy to be able to get the popup forms working at the moment.
Sorry for so much detail - I am in a remote area of Africa and only have limited access to internet so am trying to give as much info up front!
Example of popup form - you might be able to determine fields that will feed off the focalsample form
Another example of a popup form - different behaviour type to the previous
EDIT
I have jigged the code now based off previous response but am not getting a different error.This is the code with the error line highlighted - I am not sure what table name to add if any
Error code
The line Select Me.Campaign.Value should read Select Case Me.Campaign.Value.

How to populate data in a one to many relationship between two forms when the target form's table has no matching entry?

This is probably a dumb question, but I'm a noob, so forgive me. I have two tables with a one to many relationship based on a OrderNumber. Essentially, I have some order details in one table, including OrderNumber (the one side of the relationship) and I am trying to get a list of LotNumbers in the other table using the OrderNumberLot (the many side) to link them together, as there can be many lot numbers on one order. The primary key in tblOrderDetails is OrderNumber and the primary key in tblLotNumber is LotID (auto number), but the linked field is OrderNumberLot. I made a open form button located on frmOrderDetail to open frmLotNumbers (based on the tblLotNumber). The deal is when frmLotNumber opens the OrderNumberLot field is blank. I understand there is no associated record in tblLotNumber, because I am trying to enter it, but how do I get the OrderNumber from the previous form, frmOrderDetail, with the button to automatically populate in the OrderNumberLot field in frmLotNumbers? I certainly don't want people to have to type it, because they will screw it up as badly as I have this explanation of my question! Thanks in advance for the help.
I agree with June7 on using a subform-approach for your purpose.
If you already have created the form, you could simply drag it from the sidebar onto your mainform and set the "Link Master Fields" and "Link Child Fields" to "OrderNumber" and "OrderNumberLot" - or you just use the subform-wizard and follow its instructions.
Though, some people dislike subforms in general and try to avoid them whenever possible - which might make sense at further stages of development. Instead of subforms, you could then use vba to transfer information from one form to another.
Lets say your button is named bt_openform
Private Sub bt_openform_Click()
'Open the desired form. The option "acFormAdd" forces the form
'to add a new entry whenever loading and requerying.
'As we want to add a new record, we will not use the filter settings
DoCmd.OpenForm "frmLotNumber", , , ,acFormAdd
'Next, store information from the initial form into the newly opened form
'To refer to data from the current form (meaning the one, in which the Sub is triggerd),
'you can use "Me.fieldname" and to refer to another form, use "Forms!formname!fieldname"
Forms!frmLotNumber!OrderNumberLot = Me.OrderNumber
End Sub
You could also do it the other way around and retrieve the information when loading the form:
Private Sub Form_Load()
Me.OrderNumberLot = Forms!frmOrderDetail!OrderNumber
End Sub
To be on the safe site, one might want to nest this function in an if statement, to check whether frmOrderDetail is open or not. This way, you can open your frmLotNumber from different forms without causing troubles
If CurrentProject.AllForms("frmOrderDetail").IsLoaded Then Me.OrderNumberLot = Forms!frmOrderDetail

How to autofill tableA-fieldA based on tableA-fieldB by getting the data from tableB?

First, some context: I am used to working with Excel, and I have been using it to create production calculators for my worldbuilding hobby. Due to some recent problems with excessive amounts of data needing calculation, I have finally given in and tried switching it all into Access.
I have been doing some readings on how to use Access, and based on that I decided on the following:
1) I have a temperature table for regions (boreal, temperate...) with specific production levels (1,2...)
2) I have a precipitation table for regions (wet, arid...) with specific production levels (1, 2...)
3) I then have a biome table where I mix the above regions to create my biomes with the following fields:
- Biome.
- Precipitation (dropdown menu from table 2).
- Temperature (dropdown menu from table 1).
- Productivity level (which should be Precipitation Production Level from Table 2 times the Temperature Production Level from Table 1).
QUESTION: How can I have the Productivity Level in table 3 be automatic?
NOTE 1: I don't know VBA and this is my first time working with Access.
NOTE 2: I habe been told to just create table 3 as a form, but I do not think that works with what I want to do. Just in case it may be relevant (and I am not seeing the obvious), I'll next describe my first goal at building this database.
DATA ENTRY FORM: all the tables referenced below are connected by the concatenation of latitude-longitude. I have only built it partially (main form and city subform) successfully.
- main form based on table with 3 fields: latitude, longitude, terrain.
- subform based on table with 3 fields: city name, foundation date, collapse date.
- 3 subforms based on 3 tables, each representing a time period, with 2 fields: biome (biomes change in a given area depending on time periods), and its productivity levels.
After building the world, coordinate by coordinate, I will then go to the next phase - creating tables where I identify plants and animals, plus products derived from them and their levels of productivity. This will then be used to create my world's economy system and a list of characters in different levels of wealth.
I can make this work somewhat easily in Excel (without using VBA), but the amount of data will kill the file before I can use it. I only hope I'll manage to pull it off in Access - but I'll deal with this monster one step at a time. Right now, I am just focusing on the question I posted above. Thank you for any assistance you may be able to give me.
You want a database trigger meaning that entering data into biome table in the specified columns "triggers" the calculation for your third table. Access does not support triggers but there is a workaround. You use a form and create events to the textboxes of the triggering attributes.
Create a new form based on your biome table. I suggest you get the triggers to work first. Later you can add it to your form with all the subforms. Since you are new to Access you do not want to deal with subforms so soon because they make everything more complicated than it needs to be.
You rename the textboxes Precipitation, Temperature, ProductivityLevel to something like txtPrecipitation txtTemperature txtProdLevel
You open the VBA-Editor and try the following code in your new form:
.
'Trigger Events
private sub txtTemperature_Change()
call CalculateProductivityLevel()
end sub
private sub txtPrecipitation_Change()
call CalculateProductivityLevel()
end sub
private sub txtTemperature_AfterUpdate()
call CalculateProductivityLevel()
end sub
private sub txtPrecipitation_AfterUpdate()
call CalculateProductivityLevel()
end sub
'Calculation Procedure
private sub CalculateProductivityLevel()
'Check if both attributes have values. If not do not calculate anything
if (Len(me.txtTemperature & "") = 0) OR (Len(me.txtPreciperation & "") = 0) Then
exit sub
else
me.txtProdLevel = me.txtTemperature * me.txtTemperature
end sub
Note
The trigger events will call the calulcation procedure when there are new entries in the two attributes or new entries are saved.
Make sure to put the code in the new form tab of your VBA-editor
untested code so there might be some errors

Method 'Parent' of object failed

I have MS Access 2013 and I'm trying to make a search form that populates other details when a row inside a subform is selected. I figured out how to get which row is selected, and which column, but now I need to pass that information to the parent form so I can populate the other things on the form.
So on my form's subform, I made an On Click event:
Option Compare Database
Private Sub Form_Click()
MsgBox(Me.Name) ' returns P_pat subform
MsgBox(Me.Parent.Name) ' says 'Parent' failed
But it can never find its parent. I also tried on a few other events but the results were the same. The Access form looks like this:
The highlighted subform is the one I'm trying to work with, and I want it to call the parent so that the parent can populate its other child subform (the one below the highlighted form).
I feel like I slammed into a brick wall that shouldn't be there and my pride hurts.
How do I get the parent?
I know that I can simply set the record ID I selected with a global variable, but I have no way of triggering an update event for the other subform.
Any help or advice?
There doesn't look like there is anything at all wrong with your code. So from research there seem to be three possible solutions that I have found so far:
Make sure there are no special characters in your forms' names
Compact and repair
Create a new blank project and import all of your database into the blank project