Combining a variable and part of a control in VBA - 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".

Related

linking fields between a form and a subform fails

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.

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

Select combobox intem after bounded with SQL Query in Access

I have the following pair of combobox that are used for two inserts in two different access tables from a single form.
The problem I have is that I am not able to make anything else load the form is selected both in Name_OT and in Year the first value that contains corresponding combobox.
I think the solution is with:
Combobox1.Selected (0) = True 'First value
But the combobox goes blank, no text or anything appears.
Solved with this
Cuadro_combinado79 = Cuadro_combinado79.ItemData(0)
Cuadro_combinado85 = Cuadro_combinado85.ItemData(0)

Search functionality on form not working properly

I have a form Main with a subform Issue on it. To implement search functionality on Main so that users can search for records in Issue that have a given substring, Main has a text box keyword and a submit button SubmitBtn. Here is the VBA code I am using to try to make this work:
Private Sub SubmitBtn_Click()
Dim keyword As String
Dim recordSourceSql As String
keyword = Nz(Me.keyword.value)
recordSourceSql = "select * from [Issue] where [Details] like " & quoteWrap(keyword)
Me.Issue.Form.RecordSource = recordSourceSql
Me.Issue.Form.Requery
End Sub
Private Function quoteWrap(value As String) As String
quoteWrap = "'*" & value & "*' "
End Function
The problem is that after this line:
Me.Issue.Form.RecordSource = recordSourceSql
there is only one record showing in Issue--it's the first record in the original recordset, when there should be at least 20 records showing with the value of keyword that I tested. Once this occurs, the Me.Issue.Form.Requery call does not change the contents of Issue.
I know that the correct recordSourceSQL is being created, because when I put in, e.g., "data" for keyword, I get this string for recordSourceSQL:
select * from [Issue] where [Details] like '*data*'
and when I create a query in Access and set this as the SQL, I get all the correct results returned.
What's wrong with this code to search the subform according to the given criteria?
UPDATE: I was able to get this to work by setting Me.Issue.Form.Filter to the WHERE clause in recordSourceSql. I don't understand why .Filter works but changing .RecordSource doesn't.
UPDATE 2: The .Filter solution is not working either. I've described this issue in this SO question.
I created a sample, copied your code and was able to replicate your problem and solve it.
If your subform was unbound (i.e., recordsource was empty) and the primary key of your main table and Issue table had the same field name (e.g., both were named "ID", etc.) and you did not link any parent or child fields, then it will only work if you specifically name each field like this:
recordSourceSql = "SELECT Issue.IssueID, Issue.Details FROM Issue WHERE Issue.Details Like '*Data*'"
You are correct! Wildcards in the select don't work in this scenario, including Select Issue.* From Issue.
Alternatively, if you rename the primary key in your Issue table so it is not the same as your main form main table, then the wildcards work as you would expect. When I made this change your exact code worked:
recordSourceSql = "SELECT * FROM Issue WHERE Details Like " & quoteWrap(Keyword)
Note the Me.Issue.Form.Requery is not necessary. Just setting or changing the RecordSource automatically requeries.
It does not seem to matter if the two primary key fields are dragged to the main or subform. It also doesn't help to make the subform databound but empty with an initial recordsource in the property sheet of "Select * from Issue Where 1=2" (one way to create an empty recordset, but keep the form bound).
I don't know if this is an MS-Access quirk or something intentional. It seems to me that I must have come across this scenario many times (primary key was "ID", subform unbound, no child fields linked) but I don't recall coming across this limitation. Maybe I didn't use a wildcard. When I googled, I didn't find this reported by others but no doubt some MS-Access expert out there will know the reason.
Hope this helps.

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.