Dlookup in a form with 2 subforms (The requirement for the dlookup is in the second subform) - ms-access-2007

Access 2007
Form Name is: MineLocationQuery1
1st Subform name is:MineExtractionSubform
2nd Subform name is: ExtractionLineSubform2
I am trying to use dlookup to get check a value in a table and then multiply it by another value. It works with: NO Subform and with 1 Subform. But when I add 2 subforms I cannot get it to work.
Here is what I have tried so far
Simple form lookup that works:
=DLookUp("[Price]","[Resource]","[AtomicRef]='" & [Forms]![ExtractionLine]![AtomicRef] & "'")*[Tonnage]
Here is the working example from a form created with 1 subform:
=DLookUp("[Price]","[Resource]","[AtomicRef]='" & [Forms]![MineExtraction1]![ExtractionLineSubform]![AtomicRef] & "'")*[Tonnage]
And here is 1 of the many attempts I have made with 2 subforms and it is not working.
=DLookUp("[Price]","[Resource]","[AtomicRef]='" & Forms![MineLocationQuery1]!MineExtractionSubform.Form!ExtractionLineSubform2.Form.AtomicRef & "'")*[Tonnage]
What am I doing wrong other than getting tired and frustrated at copying and pasting many attempts into the worths textbox control source and repeatedly getting #Name errors

If I wish to refer to a nested subform in a textbox on the main form, I might say:
=[SubformControl1].[Form].[SubformControl2].[Form].[AControlName]
Note that this uses the name of the subform control and Form as a reference to the object contained by the control.
So this (split for ease of reading) looks right:
=DLookUp("[Price]","[Resource]","[AtomicRef]='" & Forms![MineLocationQuery1]!
MineExtractionSubform.Form!ExtractionLineSubform2.
Form.AtomicRef & "'")*[Tonnage]
However, it is very easy to get names wrong. For example, you may be referring to the form contained, rather than the subform control name. You can use the Expression Builder to troubleshoot, or you can refer to the form bit by bit in the immediate window (Ctrl+G) to ensure you have the names right. For example:
?Forms![MineLocationQuery1].Name
?Forms![MineLocationQuery1]!MineExtractionSubform.Name
More information: http://access.mvps.org/access/forms/frm0031.htm
EDIT re comments
If you work in subform 2, as I believe you should in this case, you can simply refer to [atomicref]:
=DLookUp("[Price]","[Resource]","[AtomicRef]='" & [atomicref] & "'")

Related

Open a form to a record selected in a subform

I want to open a form to the record selected in the subform of a different form. I've tried following what I’ve seen in other posts about this, but I still can't get it to work. I think I'm just missing some very basic detail but this is my first Access database and I can't figure out what it is. Thanks in advance for your assistance.
Details:
F_Detail - (This is a Single Form containing details on a project.)
F_List - (This is a Single Form containing a subform and a button.)
subF_List - (This is the subform contained in F_List. This subform is in Datasheet view)
Project_ID - (This is the primary key contained in subF_List and in F_Detail. It is the common criteria between the two. It is Short Text type.)
subF_List displays row after row of projects. F_Detail displays details about a single project at a time. From F_List I want to select a row in subF_List and then click the button to open F_Detail, and F_Detail will be displaying details of the project whose row was selected in subF_List when I pressed the button.
What I Have for the Button:
On Click > Event Procedure
Private Sub ProjectDetailButton_Click()
DoCmd.OpenForm "F_Detail",,,"Project_ID = " & Me.Project_ID
End Sub
Upon clicking the button, I get an error saying, "Compile error: Method or data member not found" and it highlights the .Project_ID at the end of my code.
I don't know what I'm doing wrong and would appreciate any help you can lend. Please let me know if I've left out any needed detail.
Use apostrophe delimiters for text field parameters. Use # for date/time, nothing for number.
"Project_ID = '" & Me.Project_ID & "'"
Me is alias for form or report the code is behind. To reference a field in its RecordSource:
Me!Project_ID
Code on main form referencing field on subform must reference through subform container control. I always name container different from object it holds, such as ctrProjects:
Me.ctrProjects!Project_ID
I name controls different from fields they are bound to, such as tbxProject:
Me.ctrProjects.Form.tbxProject

How to use a combo box to fill mutltiple field in a subform in MS-Access?

I have a sub form in Access that allows a user to select records using check boxes. In addition to this sub form there is a combo box with a list of names.
Is it possible to select a few records using the check boxes and assign a name to them with a press of a button?
The button is called [assignButton] , the sub form is called [list subform] and the combo box is called [personCombo] .
This seems like a simple task, but I am unfamiliar with how the VBA for the button triggering this kind of action would be written?
All records are bound to an existing table called "tbl_jobs" this table has the same amount of rows and columns as the subform in the screenshot
Disclaimer: The following code is only intended to illustrate how to update the subform rows, so it includes no error handling or other common process code. Also notice that the SQL statement has no additional criteria in the WHERE clause, primarily because the question lacks the subform's actual RecordSource query and/or information about how the subform table is prepared and/or filtered for each time the form is loaded.
Private Sub AssignPerson_Click()
Dim qry as QueryDef
Set qry = CurrentDb.CreateQueryDef("", _
"UPDATE tbl_jobs SET Person_Name = [ParamName]" & _
" WHERE [Select] = True")
qry.Parameters("ParamName") = PersonCombo.Value
qry.Execute
Me.list_subform.Requery 'Ensure subform displays updated rows
End Sub
Really this question is borderline "Too broad" for Stackoverflow. There are multiple ways to accomplish what you want, and your problem is one of design, coding and understanding. But in the end, this model code is rather simple and satisfies the basic requirements so I feel it not worth nitpicking.

VBA - Filter subform on unbound field in form

I am passing a value from a lookup form to another form. The value shows in an unbound field on the form (works great). I also want the subform to be filtered by what is in this unbound field. So, for example, I pick 2016 on my lookup form. It brings up another form, enters the value 2016 into an unbound field called yearvar. I cannot get it to filter what's in the subform. Here's my code that's not working:
yearvar.Value = Me.OpenArgs 'Enters year from lookup form into field call yearvar
Forms!frmEthEntCatYr.frmEthEntCatSubCatYr.Form.Filter = "[EthYear]= " & Chr(34) & Forms!frmEthEntCatYr.yearvar & Chr(34)
Form name is frmEthEntCatYr. Subform name is frmEthEntCatSubCatYr. Unbound field on form is called Yearvar. Field on subform that I want to filter is called EthYear.
What am I doing wrong? Thanks in advance!
Problem solved and it was an easy fix (thanks to someone helping me with my dah moment). I had originally tried to link the main form and subform using the Subform field linker wizard, but the unbound field was not showing (yearvar) in the Master Fields dropdowns in the Link wizard. Someone suggested just putting it in the Property Sheet - in Link Master fields, I typed yearvar and in Link Child Fields, I typed Ethyear. Works perfectly now. Lesson of the day for me: sometimes the wizards do not do what we want them to do, and you have to do it the manual way. And I didn't even have to write code to do this!

How to select table values from combobox selection?

I'm using an Access 2003 database and have 2 comboboxes I am trying to work with. The first box I have perfected already, which is a dropdown of different tables (categories of parts). Once that table is selected, I want to be able to look at the part numbers within that category through a dropdown box selection. From here I want to be able to pull up the correct report for that category with that part number in it so I can print a report for every part number. I'm sure I'll have to write some sort of VBA, Query or Macro AfterUpdate() code, but I just don't know how to fill that second combobox with the selected table's part numbers.
Click here for an image of my Menu layout
Here's my Query for the first box to show the tables I want:
SELECT Msysobjects.Name
FROM Msysobjects
WHERE (((Msysobjects.Name) not Like "MSYS*"
And (Msysobjects.Name) not like "_*"
And (Msysobjects.Name) not like "~*"
) AND ((Msysobjects.Type)=1))
ORDER BY Msysobjects.Name;
And I think this is what I'll need to print after the second box has it's selection:
Private Sub partnumberselect_AfterUpdate()
DoCmd.OpenTable Forms![_Datasheet Printing].Form.TagLabelSelection.Column(1), acViewNormal
End Sub
Thank you in advance and let me know if you have any questions.
You are attempting what are called "cascading comboboxes" which means the second box is dependent on the selection of the first.
This is accomplished through the control source of the second combo box.
The first thing you should do is write a query that returns all possible options of the second combobox, without caring so much about filtering it based on the first combo selection. Once you have it returning the correct data, you will add a WHERE clause to the second box's control source that's something like:
WHERE Msysobjects.Name Like Forms![_Datasheet Printing]!TagLabelSelection.Value
This is referencing your first combobox on your form. So after a selection is made in the first combobox, the underlying control source of the second will have the proper criteria to return the appropriate options.
However, you will need to add some VBA to the AfterUpdate() event on the first combobox. Once the selection is made, you need the second box to refresh the control source to populate the correct selections. The code is simply:
Forms![_Datasheet Printing]![MySecondComboboxName].Requery
Please see the example below.
Private Sub cboCountry_AfterUpdate()
On Error Resume Next
cboCity.RowSource = "Select tblAll.City " & _
"FROM tblAll " & _
"WHERE tblAll.Country = '" & cboCountry.Value & "' " & _
"ORDER BY tblAll.City;"
End Sub
You can read all about this concept, and others, in the link below.
http://www.fontstuff.com/access/acctut10.htm

Access 2007: Filtering a report's results using a drop-down box

My question is twofold.
I have around twenty assorted tables in a database. The table layouts are diverse; the one common thread is that all of them have a 'County' field.
I need to set up a series of reports which allow a user to select a county from a drop-down box, triggering the report to run and return only records attached to that particular county.
This is doable at the datasheet level using a filter-by-form, but that's pretty clunky and I have several tables/queries which will need this same county filter.
I may be halfway there with the following:
Create an unbound form.
Add a combo box.
Set the Row Source of the combo box to include the County field.
Set its Bound column to 1.
Set its Column Count property to 2.
Set the Column Width property to 0";1"
Name the Combo Box 'ChooseCounty'.
Add a Command Button to the form.
Code the button's click event as follows:
(Note: To write the code, in Form Design View select the command button. Display the button's property sheet.
Click on the Event tab.
On the On Click line, write:
[Event Procedure]
Click on the little button with the 3 dots that appears on that line.
When the code window opens, the cursor will be flashing between two already existing lines of code.
Between those lines, write the following code.)
Me.Visible = False
Close the Code window.
Name this form 'ChooseCounty'.
In the Query that is the Report's Record Source [County] field
criteria line, write:
forms!ChooseCounty!ChooseCounty
Next, code the Report's Open event:
(Using the same method as described above)
DoCmd.OpenForm "ChooseCounty", , , , , acDialog
Code the report's Close event:
DoCmd.Close acForm, "ChooseCounty"
When ready to run the report, open the report.
The form will open and wait for the selection of the Company.
Click the command button and then report will run.
When the report closes, it will close the form.
I can persuade the report to trigger the form, but only once - I can't seem to figure out where precisely the 'forms!ChooseCounty!ChooseCounty' needs to go. Perhaps someone can clarify or offer a more elegant way to do this?
I need to set up a large meta-report containing sub-reports on all of the tables - and, using the same drop-down 'choose a county' form, I need to have that choice cascade down through all the subreports. I don't have the faintest idea how to go about this. Suggestions welcome!
~ T
You seem to be asking two questions, the last of which is clear to me, but the first is not. The second one is in regard to how to cascade the filter to the subforms. You can do this in one of two ways:
put the form control reference as criterion in the recordsource of each subreport, OR
create a non-visible control on the report that has as it's controlsource "=Forms!ChooseCounty!ChooseCounty". Name that control "CountyFilter". Then, add CountyFilter to the link properties. If, for instance, you are linking the subreports on ID, you'd have:
LinkMaster: ID;CountyFilter
LinkChild: ID;County
(assuming, of course, that ID is your link field for the child reports, and that "County" is the name of the field in the child subreport).
Now, I'm wondering why you would have the County data not just in the parent record but in the child records -- that makes no sense. If you do have it, then the solution above will work.
If you don't, then I don't understand the question, as the whole idea behind subreports is that they are filtered by the parent record, so if the parent record is a person, and you filter by COUNTY, you're only going to get the child records in the subreport for that person, which by definition are already filtered by COUNTY because the parent has been filtered.
As to the earlier question, you write:
I can persuade the report to trigger
the form, but only once - I can't seem
to figure out where precisely the
'forms!ChooseCounty!ChooseCounty'
needs to go
You have two choices:
hardwire the recordsource of the report to use the form control reference, so the WHERE clause of your report would be "WHERE County=Forms!ChooseCounty!ChooseCounty" (and you should set this as a parameter of type text to insure that it gets processed correctly).
the better meethod is to set the recordsource in the report's OnOpen event.
After you open the form as a dialog, you'd have something like this:
Me.Recordsource = "SELECT * FROM MyTable WHERE County='" _
& Forms!ChooseCounty!ChooseCounty & "'"
And immediately after that line, you can close the form, since it's not needed any longer.
You will likely want an OnNoData event for the case where no records are returned. This is usually something simple like:
MsgBox "No records found!"
DoCmd.Close acReport, Me.Name
I hope this answers your questions, but if not, I'm happy to offer more explanation.