linking fields between a form and a subform fails - vba

I'm stuck with an x-file trying to do something simple that I did thousands of times but now fails in a weird way.
I have a parent form called TASKS and a subform called TASKS_LIST. They are populated with recordsets that share some fields. The two important fields here are BUNDLE_ID (long) and TASK_ID (long).
Depending on certaing condition I want to link the subform with the parent form by the field BUNDLE_ID or by the field TASK_ID.
This is how I do it ->
First, in another form, I open the TASKS form like this:
DoCmd.OpenForm "TASKS", , , mVntCrit, , , intType
Where mVntCrit filters the records in the form and intType is an int with two possible values (1,2) passed by as an openArgs.
Then, on the Form_Open event of the parent form I do this:
Dim intTypeAs Integer
intType= Nz(OpenArgs)
Select Case intType
Case 1
Me.TASKS_LIST.LinkChildFields = "BUNDLE_ID"
Me.TASKS_LIST.LinkMasterFields = "BUNDLE_ID"
Case 2
Me.TASKS_LIST.LinkChildFields = "TASK_ID"
Me.TASKS_LIST.LinkMasterFields = "TASK_ID"
End Select
When I execute the code it appears an error message on the instructions that changes the linked fields:
Runtime Error '2101'
The specified value is not valid for this property.
I've tried/checked:
Initialize first the property values with an empty string to reset
the linking fields.
Change the order of the instructions, changing the value of the LinkMasterFields property first (althoug in principle the child goes first).
Compile, compact and repair.
Checked that the fields to link are the same type and have no nulls.
Checked that the fields name are correct.
What am I doing wrong? What am I missing?
Thanks a lot.

I found the issue.
I realized that the code worked for some records and failed for other records. So I needed to know what's different between the records that work and those than do not work.
Ok, in the original form that opens de TASKS parent form, there is a list of elements from a table T1. In the TASKS and TASKS_LIST forms the table T1 joins with a table T2 by a field F.
When I open the TASKS form, I filter its records with a certain criterion being F=Some Id from T1. The thing is: there are some records where this Id exists in T1 but not in T2. Therefore, when I open and filter the TASKS form with an Id that does not exists in T2 the form return no records. If TASKS do not return any records, TASKS_LIST do not return any records either.
In that case, when TASKS_LIST return no records, if I try to meddle with properties of the form (in this case .LinkChildFields and .LinkMasterFields) an error happens. On the other hand, when TASKS_LIST return some records, I can alter the form properties without problem.
Therefore I deduce that accessing and altering some properties of forms that return no records is forbidden for some reason. It reminds me of the classic error when trying to access the value of a texbox in a form without records.

Related

access a specific field within a query that is part of a subform

I am trying to get an understanding of what the syntax would be to access a specific field within a query that makes up a subform within another form.
Form > SubForm/Query > Field
The field I am trying to get access to a field called Berthed that operates as a Boolean field. Either the vessel is present or it is absent.
This is what I was trying and it does not work.
Me.Boat_Move_Prior_Week.SourceObject.berthed
In this case here is the breakdown
Me = current form
Boat_Move_Prior_Week = query
Berthed = field I am trying to access through the VBA
My final goal will be to create an If statement, that upon requery of the Form, will look at the Berthed field, and if the field is checked (vessel is present) do nothing, but if the field is unchecked (vessel is not present) then remove the vessel from the query.
To reach the field (on the subform) bound to the field berthed, this is the syntax (assuming the textbox on the subform also is named berthed):
IsBerthed = Me!NameOfSubformControl.Form!berthed.Value
Here an amazing guide to know how to deal with controls in main form and subforms:
Link
Aside from my syntax issues I should have been using False for the value. Thanks to the two posters above for getting me in the right direction.
If Me.Boat_Move_Prior_Week.Form!BERTHED.value = False Then
Me.Boat_Move_Prior_Week.SourceObject = ""
End If

How do I validate a Form input field with a Table row value in microsoft access?

I am having issues with setting a Validation rule in Access.
I have a database with the tables Clients, TypeClient, Sales, SalesList, Items.
I have a form for Sales, with a sub-form SalesList inside which has a relationship with Items in order for me to put several stock items in Sales instead of only one item.
Inside Sales table is ID, Date Sold, ID Client.
Inside SalesList is ID_List(referenced to ID in Sales), ID_Item(referenced to ID in Items), Quantity.
Inside Items is ID, Name, Stock(how much we have in stock), Price
The issue is that I am trying to validate the data I enter in the sub-form Quantity field to be higher than 0, but no higher than the available stock.
The issue is that I have tried using the expression builder to get the value from a calculated query that has 2 fields - the ID and the value for that id, with criteria that is for the ID to get it from the main form, the sub-form, the combo box input and the query works.
But when trying to get it like this:
It shows an error "The expression [Query bla] you entered in the form control's ValidationRule property contains the error The object doesn't contain the Automation object 'Query bla'".
I tried using directly the table Value(but it won't work anyway as it doesn't know for which field to get the stock value from), still the same error. I guess it can't reference to anything else? Normal validation rules work, but I want to validate it not to exceed the value of the available stock for the item I am selling now.
Nowhere on the internet there is a tutorial how to do it with expression builder. How do people validate their fields then?
It doesn't stop me in this case to sell 200 items when I currently have stock of only 2 of them, for example.
Note: I have tried DlookUp in expression builder, straight up tells me No.
Sample wrong validation rule expression builder code:
<=[Query bla]![Stock]
<=[Items]![Stock]
I am currently using VBA and fetch the record I need(the current stock) with one of the following and my unbound text is changing on every subform to the same, it shouldn't happen like that. What can I use to populate a field for each record uniquely:
Private Sub ID_Product_IZBOR_Click()
'Me.Stock_ValueField = DLookup("[Nalichnost]", "Stoka", "[ID]=" & Me.ID_Product_IZBOR)
Me.Stock_ValueField = Me.ID_Product_IZBOR.Column(2)
End Sub
Partial solution: I created a new Dim as Integer and fetched the records based on the ID provided by the field in the form and used BeforeUpdate to validate the current stock. Unfortunately the only way to see the current stock level is to click on the combo box where you choose your product and check the column for that one, it doesn't show anywhere else :(
Don't use the expression builder (I NEVER do) - just type the needed expression in property.
>0 AND <=DLookup("Stock", "Items", "ID=" & [ID_Item])
Another approach is to return Stock in textbox ControlSource then ValidationRule references that textbox. Also, user can then see the quantity limit. Methods of pulling the Stock value from Items table:
include Stock field in Items combobox RowSource - textbox then references column by its index (index begins with 0): =[cbxItems].Column(2); VBA may be needed to Requery combobox after record entry/edit is committed to table.
include Items table in form RecordSource - bind textbox to Stock field (Locked yes and TabStop no)
DLookup() expression in textbox ControlSource
Use ValidationText property to give users a custom message.
However, ValidationRule will not prevent user not entering any value if they skip the control. If you want to make sure a value is entered, either set field as required in table or use form BeforeUpdate event to validate record data.

Why does subform not display all results

I have an Access Form with various dropdowns. The form contains a report in a subform. The report is based on the following SQL statement which references the dropdowns on the main form:
SELECT imp_rule_CURRENT.target
,imp_rule_CURRENT.Phase
,imp_phase.SortOrder
,imp_rule_CURRENT.Solicitation
,imp_rule_CURRENT.Qualification
,imp_rule_CURRENT.Activity
,imp_rule_CURRENT.ClientLocation
,imp_rule_CURRENT.ProviderLocation
,imp_rule_CURRENT.Answer
,imp_rule_CURRENT.Comment
,imp_ActivityList_Test_PB_MetaversionC2.HeadingText
,imp_answer.full_name
,imp_answer.icon2
,imp_phase.Chapter
,tbl_Country.short_name
,IIf([Forms]![frm_ClientModel]![ckbYesOnly], InStr([Answer], "is_yes") > 0, - 1) AS Is_Yes
FROM imp_phase
INNER JOIN (
imp_answer INNER JOIN (
tbl_Country INNER JOIN (
imp_ActivityList_Test_PB_MetaversionC2 INNER JOIN imp_rule_CURRENT
ON imp_ActivityList_Test_PB_MetaversionC2.SignatureTagSet = imp_rule_CURRENT.Activity
) ON tbl_Country.ISO_Country = imp_rule_CURRENT.target
) ON imp_answer.code_name = imp_rule_CURRENT.Answer
) ON imp_phase.code_name = imp_rule_CURRENT.Phase
WHERE (
((imp_rule_CURRENT.target) = [Forms]![frm_ClientModel]![cmbCountry])
AND ((imp_rule_CURRENT.Phase)
LIKE IIf([Forms]![frm_ClientModel]![cmbPhase] = "ALL", "*", [Forms]![frm_ClientModel]![cmbPhase]))
AND ((imp_rule_CURRENT.Solicitation) = [Forms]![frm_ClientModel]![txt_Initiative])
AND ((imp_rule_CURRENT.Qualification) = [Forms]![frm_ClientModel]![txt_Qualification])
AND ((imp_rule_CURRENT.ClientLocation) = [Forms]![frm_ClientModel]![txt_MeetingLocation_Client])
AND ((imp_rule_CURRENT.ProviderLocation) = [Forms]![frm_ClientModel]![txt_MeetingLocation_Bank])
AND ((imp_rule_CURRENT.Answer) <> "is_not_applicable")
AND ((IIf([Forms]![frm_ClientModel]![ckbYesOnly], InStr([Answer], "is_yes") > 0, - 1)) = True)
)
ORDER BY imp_phase.SortOrder;
When I have form open and the following part of the SQL statement evaluates to True Like IIf([Forms]![frm_ClientModel]![cmbPhase]="ALL","*",[Forms]![frm_ClientModel]![cmbPhase])) the query, when opened manually, shows all results as I would expect. Yet the subform which is based on this query shows no results.
Question: Why does the subform not show any results and how can I get the subform to show them?
Update 2:
I have tested and found out that if I view the report upon which the subform is based, then the correct results show. As noted in a comment below, the subform report works as intended when I selected anything other than "ALL". Results are shown for every other item seleted in the subform, just not when I select the option "ALL". Those results are not shown in the subquery, yet oddly they do show if I open the report manualy. So perhaps the problem is somehow linked to the subform?
I still don't understand why this is the case, but the answer to my problem was that the query of the mainform had to be adjusted to include the new statement. despite the subform NOT being linked to the main form.
As noted in my update, when I opened the query manually it worked fine. When I opened the report that was based on the query it worked fine. Only when I tried to open the report through the subform did it not show the expected result.
This finally led me to the insight that perhaps, under certain circumstances, the record set that was being shown was restricted somehow.
The main form that I had set up was itself based on a query. I had not updated that query to contain the new Like IIf([Forms]![frm_ClientModel]![cmbPhase]="ALL","*",[Forms]![frm_ClientModel]![cmbPhase])) command. Despite my subform NOT being linked to the main one, it restricted the recordset available to display.
Are you re-querying the subreport after the update?
Your code should include something along these lines, where rvSubReportView is the sub-report object:
rvSubReportView.Report.Requery
This would be in the AfterUpdate() function for the main form's ComboBox control.

Combining a variable and part of a control in VBA

I have a subform which could be different depending on a user choice (as they are make table queries). Consequently I have had to store the actual table name in a variable caled MyResultSO. This works fine and returns the correct table name. I am then trying to get the value of a field from the subform. I am then trying to combine the variable with the field name.text to get the value of the field from the subform but can't get the syntax correct.
I have tried variations but, for example here is one:
MyResultSO = Me.RESULTS.SourceObject
'this works fine and returns "Table.POWER PRICES Query Table 3"
MyProductCode = MyResultSO & !PRODUCT_CODE.Text
'which is where I am trying to combine the value of variable MyResultSO and the field which is PRODUCT_CODE.Text.
Combined this should return the value of the current record from the following:
Table.POWER PRICES Query Table 3!PRODUCT_CODE.Text.
As you will probably tell from the above, I am not very experienced with VB.
Somewhere in the private module of the main form, here Me:
MyResultSO = Me.RESULTS.SourceObject
MyProductCode = Me.RESULTS.Form.Controls("PRODUCT_CODE").Value
Not to use .Text, that requires .Focus: so is bad, it does not always work!
MyProductCode = Me.RESULTS.Form.Controls("PRODUCT_CODE").Text ' <= it's bad.
Here PRODUCT_CODE is the name of a control on the subform named "Table.POWER PRICES Query Table 3".

Access DoCmd.OpenForm Not Working

Looking for a second set of eyes to figure out my problem with an Access form filter. I created a search form, when filled in, appends search criteria to a string variable (strQuery) that is put in place to the [WhereCondition] for opening a form. However, when the script is ran, nothing comes up except for a filtered form with no records.
Here is the line that opens the form:
DoCmd.OpenForm "ADD_NEW_NCMR", , , strQuery
Before the line is ran, strQuery equals:
1=1 AND [NCMR].[NCMR_NUM] = '12-129'
The form name, and table.column combination are all correct. In fact, using the DCount function returns the result of 1, which is correct for this query, and returns the correct number for other queries as well. This makes me think that there is nothing wrong with the where condition.
DCount("[NCMR_NUM]", "NCMR", strQuery)
Check your form's Data Entry property. You can find it on the Data tab of the form's property sheet.
If Data Entry = Yes, the form will not display existing records.
Sounds like you want Data Entry = No, so that existing records which match your OpenForm WhereCondition will be displayed.