Populating a form with a dropdown - Access - ms-access

I am trying to create a form in access that has a dropdown at the top and will populate the rest of the form with the record that corresponds to what is chosen.
I have looked at 2 different guides online but both point to older versions of Access and I think I am missing something.
So. The order that I did everything:
I went to the table for the form. And I clicked Form. This made a quick form with all of the fields as text boxes.
I deleted the field that will be the dropdown and I put in a dropdown canceling the wizard
In the data section for the dropdown. I edited "Row Source" to select the field that will be in the dropdown and the unique ID, in that order.
I set the bound Column to 2 (I have tried both)
I set the name of the Combo Box to "TitleSelector"
In Form Settings. For "Record Source". I have "[forms]![Edit Piece].[TitleSelector] & "*" in Criteria for "ID". I also have the rest of the fields in the Query listed
I added the "me.requery" to on change
Not sure if it means anything. When I was typing in the Criteria for "ID" it did not show "TitleSelector" in the drop down.
I am using Access 2010
Edit:
My Problem -
The dropdown itself shows everything correctly. However nothing happens when I choose something
Edit:
SELECT Pieces.ID, Pieces.Title, Pieces.Composer, Pieces.Instrumentation, Pieces.Location, Pieces.Location_2
FROM Pieces
WHERE (((Pieces.ID)=[forms]![Edit Piece].[TitleSelector]));
Edit:
Here is a link to a copy of my database: https://www.dropbox.com/s/tpnqm686tj653fg/Trisha%20Database.accdb

Adding the me.requery to the ON CHANGE EVENT
(select CODE BUILDER - and enter the me.requery there) should work.

I have downloaded the sample file. Your problem is that the Record Source for the [Edit Piece] form is set to...
SELECT Pieces.ID, Pieces.Title, Pieces.Composer, Pieces.Instrumentation, Pieces.Location, Pieces.Location_2 FROM Pieces WHERE (((Pieces.ID)=[forms]![Edit Piece].[TitleSelector] & "*"));
...and the & "*" at the very end is causing the query to return no records. Removing that last bit so the Form's Record Source is...
SELECT Pieces.ID, Pieces.Title, Pieces.Composer, Pieces.Instrumentation, Pieces.Location, Pieces.Location_2 FROM Pieces WHERE (((Pieces.ID)=[forms]![Edit Piece].[TitleSelector]));
...gets your form working.
Edit
To have your form display information from the related tables (instead of just the foreign key values from the [Pieces] table), change the Record Source of the Form from...
SELECT Pieces.ID, Pieces.Title, Pieces.Composer, Pieces.Instrumentation, Pieces.Location, Pieces.Location_2 FROM Pieces WHERE (((Pieces.ID)=[forms]![Edit Piece].[TitleSelector]));
...to...
SELECT Pieces.ID, Pieces.Title, Composer.Composer, Instrumentation.Instrumentation, Location.Location, Location_1.Location AS Location_2 FROM (Location INNER JOIN (Instrumentation INNER JOIN (Composer INNER JOIN Pieces ON Composer.ID = Pieces.Composer) ON Instrumentation.ID = Pieces.Instrumentation) ON Location.ID = Pieces.Location) INNER JOIN Location AS Location_1 ON Pieces.Location_2 = Location_1.ID WHERE (((Pieces.ID)=[forms]![Edit Piece].[TitleSelector]));

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.

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

Editing a Form in 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.

Add FIRST Attachement (picture) to a report in access

I have a table [List_Models] which has 3 fields
PK - Model_ID (num)
Model_Name (txt)
Picture (attachment)
A certain model has plenty of colour combinations (one has 52) and so 52 pictures attached.
On my report, however, I simply want to attach first (or any) of those pictures, as long as it is a correct model.
Basically no matter colour combination, simply show a picture so it is easier to visualise the model itself.
How do I go about this in the easiest way?
I have made a query to fetch first picture from the attachment (ie. 2 columns, Model_ID, Picture.FileData). Here is the syntax:
SELECT Zoznam_Modelov.Model_ID, Zoznam_Modelov.Picture.FileData
FROM (SELECT Zoznam_Modelov.Model_ID, Max(Zoznam_Modelov.Picture) AS MaxzPicture FROM Zoznam_Modelov WHERE (((Zoznam_Modelov.model_id) Is Not Null)) GROUP BY Zoznam_Modelov.Model_ID) AS U_Pic
INNER JOIN Zoznam_Modelov ON U_Pic.Model_ID = Zoznam_Modelov.Model_ID
WHERE (((Zoznam_Modelov.Picture.FileName)=U_Pic.[MaxzPicture]));
Then I tried attaching the FileData to the report in this way:
Make Attachment field in Form
Set Control Source as:
=DLookUp("Zoznam_Modelov.Picture.FileData";"qryUnique_Pictures_Data";"Model_ID = " & [Model_ID])
However, it remains blank.
How do I go about this in the easiest way?
Just open the report in Design View, then drag and drop the Attachment field onto the report. That will add an Attachment control that is bound to the Attachment field. The default settings will display the first attachment (picture) on the report like this:

Form Opens blank in form view

I have a database system that logs calls for a department. All the calls for today are displayed on a main form in a list box. When a record is double clicked the call can be opened to view the information. This information is stored across two tables by a intermediate relationship.
When the form is opened in design view all the controls are visible. As soon as it goes to form view or a record is attempted to be opened, the form opens blank.
This has happened since i added the information from the second table. What has gone wrong?
Here is the query.
SELECT
TBL_CallsDB.ID, TBL_CallsDB.Agent_No, TBL_CallsDB.Agent_Name,
TBL_CallsDB.Call_Date, TBL_CallsDB.Call_Time, TBL_CallsDB.Caller_Type,
TBL_CallsDB.Call_Type, TBL_CallsDB.[Company Name], TBL_CallsDB.[Company Contact],
TBL_CallsDB.Caller_Name, TBL_CallsDB.Phone_No, TBL_CallsDB.Cell_No,
TBL_CallsDB.Fax_No, TBL_CallsDB.Email_Address, TBL_CallsDB.Address,
TBL_CallsDB.City, TBL_CallsDB.State, TBL_CallsDB.[Zip Code],
TBL_CallsDB.Details, TBL_CallsDB.Action_Resolve, TBL_CallsDB.Resolve_Date,
TBL_CallsDB.Resolved, TBL_CallsDB.Attachments, TBL_CallsDB.Status,
TBL_CallsDB.Memo, TBL_Units.Technology, TBL_Units.[Serial_No#],
TBL_Units.Model, TBL_Units.[Model_No#], TBL_Units.Unit_Type,
TBL_Units.Install_Date
FROM TBL_CallsDB
INNER JOIN TBL_Units ON TBL_CallsDB.Agent_No=TBL_Units.[Agent No];
Examine the values stored in TBL_CallsDB.Agent_No and TBL_Units.[Agent No]. The query's join condition causes it to retrieve only those rows with matching values in Agent_No and [Agent No]. But none of those existing values match, so the query returns no rows.