MS Access 2010 - make a form populate fields in multiple tables - database

I am new to access but I have a problem. Take a look at the screenshot of my table relationships. I would like to create a form such that when someone enters the 'Student Code', the student code is populated in all three tables, rather than just one, saving time and confusion as one only needs to type it in once. Thank you everyone in advance. see http://i.stack.imgur.com/bHBqM.png

In the AfterInsert event of the Student Information form, run append VBA queries. With this, each time you add a new Student record, the corresponding student code will be inserted into the other related tables:
DoCmd.RunSQL "INSERT INTO [Parent Information] (Student Code)" _
& " VALUES (" & Forms!yourformname![Student Code] & ");"
DoCmd.RunSQL "INSERT INTO [Student Medical Conditions] (Student Code) " _
& " VALUES (" & Forms!yourformname![Student Code] & ");"
Alternatively, you can save the above queries as stored queries and run them via Macro (Open Query) or VBA (DoCmd.OpenQuery) -both still in AfterInsert form event:
DoCmd.OpenQuery "ParentInformationQ"
DoCmd.OpenQuery "StudentMedicalConditionsQ"
If your subforms are included on main form, add Requery commands to update subforms. With this, you will see blank rows appear in subforms, ready for data edit (NOT entry since you created the records in prior queries):
Forms!yourformname!yourparentinfosubformname.Form.Requery
Forms!yourformname!yourmedicalconditionssubformname.Form.Requery

Related

Where is Access form data populating data from

I am currently trying to help a friend out with their invoicing Access database. I have rarely ever used Access and I am having problems figuring out the location of where the form (frmEntry) is pulling its data from. I did not create this setup so I am unsure of how it works. I am trying to figure out where the address information is being pulled from for when a customer is selected in a drop down on a form. I checked the query and it is only pulling the CustomerID and CustomerName, no address. The table does have address fields but none of the customers in the table have any listed, yet there address is populated along with their name in the form.
I do see where there is another form (frmCustomer) that has customer and there addresses but I am not sure if the other form is pulling from here, and if so, why can I not find the addresses in any of the tables or datasheet views?
Any direction would be very much appreciated. My end goal is to obtain the customer information (address etc) so that I can insert it into a new database that I am working on
Your data contains linebreaks and a combobox only shows one line per record.
To show the data you can replace the linebreaks in rowsource.
SELECT Replace([CustomerName],vbCrLf, " ") as CName FROM table
' vbCrLf is the VBA constant for linebreaks (Cr - Carrige Return, Lf - LineFeed)
This is poor database normalization (imagine you want to search for a customer name that is equal to a city, e.g. Paris). Each line should be a separate field in table (and Postcode too). If there is a linefeed for every data (e.g. no street -> empty line), you can just split the data into the new fields.
'Put this code in a module
'Split function
Public function splitCustomerName(ByVal strCustomerName as String, ByVal index as long) as String
Dim arrCustomerName As Variant ' or declare a fixed array if you know the number of lines
arrCustomerName = Split(strCustomername,vbCrLf)
splitCustomerName = arrCustomerName(index)
End Function
The query
UPDATE table SET newCustomerName = splitCustomerName([table].[CustomerName],0)
, newCustomerStreet = splitCustomerName([table].[CustomerName],1)
, newCustomerCity = splitCustomerName([table].[CustomerName],2);
Just create the necessary columns for name, street and city, then run the query.
If you delete the CostumerName column and rename the table (e.g. newTable) you can create a query with the oldname of the table, that behaves like your old table.
SELECT *
, newCustomerName & vbCrLf & newCustomerStreet & vbCrLf & newCustomerCity as CustomerName
FROM newTable

Pulling data from an access tabel and inserting it in another tabel

Im having some trouble with the design of my database. In microsoft Access I have two tables. One named table1 this table contains three fields (Name, Surname and Birthdate). The other table, named table2 contains two fields (Name and Surname). I want the following to happen. If I make a new record in table1 using a form the Name and Surname get passed/inserted to table2 automaticly. What is the best way of doing this? I was messing around with the primary key but that doenst seem to work. And since I'm a beginner I dont know where to go from here.
Thanks in advance for your time and efford!
Just figured out the awnser to my question! I used the following code
Private Sub addNew_Click()
Dim db As Object
Dim rst As Recordset
Set db = CurrentDb
Set rst = db.OpenRecordset("select * from Person", dbOpenDynaset)
rst.AddNew
rst!PeopleSoftNr = tbPeopleSoftNr.Value
rst!Name = tbName.Value
rst!Birthday = tbBirthday.Value
rst.Update
Set rst2 = db.OpenRecordset("select * from Dental", dbOpenDynaset)
rst2.AddNew
rst2!PeopleSoftNr = tbPeopleSoftNr.Value
rst2!Dental = tbDental.Value
rst2.Update
End Sub
This allows me to write data to multiple tables.
In my opinion the best way to do, what you described, is creating a form for the personal data and include a subform to assign courses of the persons.
Detail information about the courses should be edited in an own form for the courses.

Insert new record into a table from a form, using data from multiple tables

I am fairly new to both Access and Databases and I'm struggling with how to add a new record into a table using a form, which gets its options from multiple other tables.
I have 4 tables which contain: a list of periods (tblPeriod), a list of pupils (tblNames), a list of subjects (tblAreaOfLearning) and a list of levels (tblLevels).
I want to create a form which will allow a user to select the period, then the pupil, then the subject, and then the level at which that pupil is attaining at that subject, and then add the ID's for all of these into a 5th table called tblMaster
I have no idea how to do this though? I have created a query linking everything to the master table by the ID's, but i have no idea how to create the form and the command to insert this into the new table?
All help appreciated
Here's an idea:
It looks like you can scrap your query idea, and do this instead...
Create a new form with 4 combo boxes:
cboPeriodID- user to select the period (recordsource tblPeriod query)
cboNameID- then the pupil (recordsource tblNames query)
cboAreaOfLearningID- then the subject (recordsource tblAreaOfLearning query)
cboLevelID- then the level at which that pupil is attaining at that subject (recordsource tblLevels query)
Then have a command but at the bottom of the
form with an Event Procedure to run the SQL to
insert the values into the master table.
Sub btnSubmit_Click()
dim strSQL as string
strSQL="INSERT INTO tblMaster (m_period_id,m_name_id,m_areaoflearn_id,m_level_id) VALUES (" & cboPeriodID & "," & cboNameID & "," & cboAreaOfLearningID & "," & cboLevelID & ")"
CurrentDB.Execute strSQL
End Sub

Muli-Option MS Access Reports

I am very new to MS Access and yet have been working (loosely) on a DB for a while. We have a DB that tracks membership. There is a table with all of the member info in there. When new and current customers are added, or pay for the current year, the info is applied to a 'PaidYear' column. For years now, I have been adding a query listing the current years' members and adding a report that displays the output of the query.
I would like to create a report where I could (using a drop-down maybe) select the active year and other options such as City, Company Name, Phone Number, etc. Is there any way to simply set this up? It has to be easy enough for my replacements to intuitively use. ie:
Member Report for [Choose Year] <-- Dropdown
[City] [Company] [Phone] [Select Option]<--- Extra Options for reporting
I have been playing with it for a while and while I can get the design set up, I can't set up the functionality. Thank you so much!!!
Yup, if you want to filter down your report, you can write a little bit of VBA to open your report with a filter (you don't need to use a parameter query for this. It may be more efficient to do this at the query execution level, but as far as i've noticed, the performance is the same to just run the full query and filter it at runtime of the report open (Access might actually just do this behind the scenes, again, I don't really know.
Anyways, lets get to it. Here's a code snippet that you can kind of use as a starting point and adapt.
Create a button that says 'Run Report', let's call it cmdRunReport
In the On Click event for that button, you will put some code. I'm just writing this up now, so I might have some syntax errors (don't have Access on this PC).
dim multiple as boolean
dim filtering as string
filtering = ""
if me.yearDropdown is not null then
filtering = filtering + "[myYearField] = " & me.yearDropdown
multiple = true
end if
if me.cityDroPDown is not null then
if multiple then
filtering = filtering + "AND [myCityField] = '" & me.cityDropdown & "'"
else
filtering = filtering + " [myCityField] = '" & me.cityDropdown & "'"
set multiple = true
end if
end if
if me.CompanyDropDown is not null then
if multiple then
filtering = filtering + "AND [myCompanyField] = '" & me.CompanyDropdown & "'"
else
filtering = filtering + " [myCompanyField] = '" & me.CompanyDropdown & "'"
set multiple = true
end if
end if
DoCmd.OpenReport "yourReport", acViewPreview, , filtering
This is the basis of what you can do. I may have a couple if syntax errors and concatenated the filtering string incorrectly (untested), but that's where you could start.
In english, it just looks at your form's dropdowns that you use to filter. It checks if they are not null, and then concatenates their values into the "filtering" string. This string is used as a parameter in your OpenReport method.
Hope this helps.

Issue using DLookUp function in Access

Ok so I have a text box on my main form and I want it to show the quantity of a product chosen from a drop down list.
Now there is a complication, there are 3 stock locations for each product, but this is simplified as I have 3 different levels stored on the same record for a product, with different column names.
So what I need to do is search for the name of the correct column to find the right stock location (again from a drop down).
My stock level column heading is stored in the variable "Branch" which is a string.
Here is my code.
Me.txtSourceDescQty.Value = DLookup(Branch, "[products/stock]", "[Product Code] = " & Me.cmbSource.Value)
This is the error I get. 'Stock Level' is the column header for one of the stock locations, which is stored under the variable "Branch" in the line of code.
cmbSource is the combo box where the product code is selected.
I think Me.cmbSource.Value needs to appear in quotes:
Me.txtSourceDescQty.Value = DLookup(Branch, "[products/stock]", "[Product Code] = '" & Me.cmbSource.Value & "'")

Resources