Losing ability to change properties when changing SourceObject of subform in Access 2007 - vba

I have a subform that changes where it gets its data from based on user input by using the Me.SubForm.SourceObject = Query.SomeQuery. It seems that by doing this, I am losing the ability to set the BeforeUpdate property.
The code I am using is as follows:
Forms![PartsDatabase]![RepsSubform].Form![Pack Rank].BeforeUpdate = "=ToTracking()"
I have confirmed that this works before the SubForm.SourceObject is changed, but afterwards it throws the following error: RTE 2455 "You entered an expression that has an invalid reference to the property BeforeUpdate."
I was wondering if this is a known issue or if I just need to modify my code to adjust.

You are getting things muddled up here. You should never be changing Source Objects, rather you should be changing the Record Source. The code involved in the Form is Form level. If you wish to use the Before Update event, it belongs to the Form and not the Recordsource. So you always go to change the RecordSource.
You would use,
Forms!Parentform!SubForm.Form.RecordSource = "SELECT someFields FROM someTable;"
Or,
Forms!ParentForm!SubForm.Form.RecordSource = "yourCompiledQueryName"

Related

Subform showing up blank after code executed

My goal is to change the SourceObject of a subform. I can do this manually by going into design view selecting the subform object and changing SourceObject and removing the master and child links. I save changes and reload into form view and all works well.
When I try doing this via VBA it does not seem to work and gives me a blank subform.
Here is the code that I am currently running:
Private Sub ondacapBtn_Click()
Me!DACAPRosterQ_subform.SourceObject = "ondacap_subform"
Me!DACAPRosterQ_subform.LinkMasterFields = ""
Me!DACAPRosterQ_subform.LinkChildFields = ""
Me!DACAPRosterQ_subform.Requery
I find this even crazier because this is code I've used before except the Me!subform is a different name, and it works perfectly elsewhere! I am complete baffled at how this works in the other subform but not this one.
I've tried changing the name, the tab order, made sure the subform I am changing to works, made sure the code matches the other subform shown below, I am out of ideas and I made this account just so I can ask this question. It's really upsetting me.
Private Sub FilterBtn_Click()
Me!Retrain_subform.SourceObject = "UpcomingRetrain_subform"
Me!Retrain_subform.LinkMasterFields = ""
Me!Retrain_subform.LinkChildFields = ""
Me!Retrain_subform.Requery
EDIT - 26SEP22
I found out that one of the fields in my RecordSource is causing this issue.
My subform has a RecordSource called DACAPRosterQ and in that query I've input a field that is called "SearchTerms:" [with] & [other fields] & [listed] & [to use as search bar criteria]. The table PersonnelT that the query DACAPRosterQ pulls from does not have the "SearchTerms:" field naturally. Only the query.
When I remove this field from the query the subform can be switched with no issue via VBA.
Something I noticed when trying to open Ondacap_subform individually without the parent form is it asks for input "Forms!DACAPRosterF!DummySearchTextbox" which is a control on the parent form DACAPRosterF alongside the subform.
I have a feeling I must input some code to reinitialize that field as it is not done so via VBA. I'm just not sure what it is.
I think this would also explain why I am able to switch SourceObject manually via design view because it it already being reinitialized when reopening the form in form view.
Anyone have any ideas?
I figured it out... I couldn't tell you why it works or why it didn't work without this solution.
All I had to do was enter into the subforms OrderBy property, I ordered it by [Field1], [Field2]. Works like a charm now. Search bar still works and everything functions as normal.
If anyone could provide insight as to why this occurred, that would be amazing. Otherwise, problem is fixed.

Calling user reference properties in CATIA using VBA

I'm looking to call user referenced properties in CATIA. Currently I am able to do this through a direct path using:
xyz = CATIA.Activedocument.product.UserRefProperties.Item("DESIGNER").Value
MsgBox (xyz),0
I'm looking to generalize so that what ever the user is selecting in the window, that information will be displayed. I'm able to do something similar to what my goal is using code like this:
abc = CATIA.ActiveDocument.Selection.Item(1).Value.Name
MsgBox (abc),0
This gets to my issue. The above code will call system defined properties based on selection, however, this will not work on the user defined properties.
How can I modify this code to take the information stored in a user defined property and display it?
You might have a problem due to the fact that starting at level 2 substructure, your selection does not hold a Reference, but an instance, try this code (I display the 1st User Added Property of the selected product)
MsgBox CATIA.ActiveDocument.Selection.Item(1).LeafProduct.ReferenceProduct.UserRefProperties.Item(1).ValueAsString

Why did I have to manually set the .value from the .text on losing focus in an unbound textbox

I have an unbound textbox to accept the delete older than: number of days. It is in the report header. I set it to 30 days but I want the user to be able to change it. I was banging my head trying to figure out why entering 40 was not being accepted and it reverted back to 30 every time. I finally decided on using the lost_focus event to set .value to .text. That worked.
Further research showed that when the textbox get's focus text and value are both the same, 30 in my case. Changing the number in the text box to 40 shows the values of text at 40 and value at 30. Unless I specifically set Value to the value of text Access changes text to the value of value. This is different behavior than other places in Access such as forms.
Can anyone tell me why this might be? I can't find any setting that might do this. Is it because it's in a report header? what is the difference between this and every other text box I've ever used?
From a "best practices" viewpoint, Access Reports are not intended to be used interactively despite the ability to manipulate some unbound controls. Although workarounds can be implemented that function sufficiently well, such solutions are often incomplete and buggy and function differently depending on the active view: Report View vs. Print Preview. Appropriate design patterns include using Access Forms for specifying report options which then open the Report in a static configuration.
This may not satisfy the question "Why?" if seeking a deeper answer as to why Microsoft implemented inconsistent binding behavior in Access, or why they allowed interactive controls in reports at all if they don't behave the same way as in forms. But Access has plenty of other quirky behaviors that have no known/published explanation.
Regarding the priority of the Value property updating the Text property (and not vice versa): Value is the key field because it contains the actual data for the control (bound or unbound). Although it is natural to have a single control for both display and input (uh, that's how almost all controls work), the processes of displaying data and parsing user input are two distinct functions. The visual representation returned by the Text property can be manipulated using the various formatting properties, and technically could display an incomplete representation of the underlying Value data. If there are any conflicts between the stored Value property and the Text property, it is natural that the existing Value property has precedent.
My guess is that the automatic binding behavior was "relaxed" for reports to allow more flexible custom reporting output. First consider an Access Form in Datasheet view: An unbound Form control shows the same value for all records. Even if the control is edited while on a particular row, the updated value is displayed for all rows. The same control object is essentially repainted for each row and there is no concept of individual instances of the control that can hold different values. Bound controls have built-in code that repaint the control with data from the particular row, but there are still not multiple instances each "holding" the individual values. The visual output differs from an intuitive object-oriented paradigm where our minds what to assign each visual row its own in-memory instance of the controls--it just doesn't work like that in Access.
Unlike the Form behavior just described, the Report's Print Preview (and actual printed output) allows unbound controls to display different data per row using the Detail_Format() event. Within the Detail_Format() event, one can set the Value property of a control at which time the Text property is automatically updated according to various formatting properties. This update Text is then output for the current row. Perhaps (just guessing) that this behavior would not function properly if the Text property updated the value property. I suspect it would cause recursive events during report generation. Because reports are not meant to be interactive, relevant text-input parsing code was "disconnected" so that it doesn't behave like on a form.
All that explanation doesn't make Access any less frustrating nor remove its limitations, but at least learn to adapt and design things in the "Access-esque" way rather than fighting it.
your best bet is to design a form with the unbound combo boxes and have your data displayed in a subreport. I like to design my reports so that when values are updated the query for the recordsource of the report is generated doing this requires 2 queries to exist, one with all data possible and a filtered one as subreport recordsource. This will control the data for printing and also allow users to close or navigate away from the report and return to the data later.
Private Sub ComboBox1_AfterUpdate()
Dim Query1 as Object
Dim Temp_Name as Variant
Temp_Name = SubReport.SourceObject
SubReport.SourceObject = Empty
Set Query1 = Me.Form.Application.DBEngine.Workspaces(0).Databases(0).QueryDefs ("SubReport_Query")
Query1.SQL = "Select * Unfiltered_Query WHERE Field1 <= " ComboBox1 & ";"
SubReport.SourceObject = Temp_Name
End Sub

VB.Net ComboBox (as Dropdown) not translating text to DisplayMember with Databinding

I inherited a fairly large project at work that is undocumented and written in VB (originally started pre .NET, ended around .NET 2). I'm in the process of updating / refreshing a lot of the code, but have run into an annoying issue that I haven't found the solution for yet. This system utilizes a UI, a Web Service, and a SQL DB.
Problem: I have a Databound Combobox (originally set to DropDownList - I'm changing it to DropDown, which is what started this mess - going back isn't an option) that is tied to a DataSet that comes from a Web Service. When a user types in the item they want manually, the data from the text field doesn't seem to associate itself with the DisplayMember, which forces the WS/SQL query to fail (it is sent a blank value when it's expecting a ValueMember). If the user types in a partial selection and then chooses the value they want from the DisplayMember list using the arrow keys or tab, the query goes off without a problem.
My Question: How do I get the text field to translate to the DisplayMember which will then properly tie itself to the ValueMember which will then allow the query to execute correctly? Sorry for making this sound complicated or convoluted; I'm sure the answer is easy and I'm just glazing over it.
The relevant bit of code is:
With cmbDID
If dtsLU.Tables.Contains(reqTable) = True Then
.DataSource = dtsLU.Tables(reqTable)
.DisplayMember = "zip"
.ValueMember = "gridID"
End If
End With
cmbDID.DataBindings.Clear()
cmbDID.DataBindings.Add("SelectedValue", dtsData, strDT & ".gridID")
I've tried changing "SelectedValue" to "Text", which almost works - but it directly translates to gridID and skips zip which ends up with an incorrect Web Service response since the zip and gridID field values are not synced (zip (DisplayMember) may be 5123 while gridID (ValueMember) may be 6047). I've tried changing "SelectedValue" to "SelectedIndex", and that got me no where.
Any help is greatly appreciated.
EDIT
To add some clarification to the process, the below pseudo code / description is roughly what happens. I could post the whole module, but I feel that would just muddy the whole question even more.
Private Sub A
FormAlpha is created with 1 ComboBox in the form of a DropDown
This DropDown is populated with a DataSet
DataBinding with a blank DataSet is added to the control to keep track of the users input
End Sub
lblSubmit_Click event is triggered on FormAlpha by the user after they have populated the DropDown with their data. lblSubmit_Click calls Private Sub Submit
Private Sub Submit
BindingContext(DropDown DataSet, tableName).EndCurrentEdit() is called
DataSet.HasChanges() is processed
If changes are present, changes are processed
HERE lies the problem
If the user has manually typed in the DropDown field, but not hit an arrow key or tab, then the DataSet registers a change, but returns a null value in all fields - it knows something was entered, but that data apparently didn't pass through the DataSet for the ComboBox (ListItems or SelectedIndex didn't change / fire I'm guessing). If the user selects the item with the arrow keys, the DataSet has the proper input (I'm assuming the Data was validated by the control at this point).
If the processed data is good, a value is entered into the database
If the processed data is bad (empty), an error is returned
End Sub
If the above can't be solved with what I've provided, but someone still knows a better way to handle this type of situation, I'm all ears. Rewriting the module isn't ideal, but fixing this problem is a necessity.
Alright, while this fix may not be ideal, it is a fix none the less.
The bare bones problem was that the text value of the DropDown wasn't causing the data to actually affect the SelectedIndex / SelectedValue of the control unless you interacted with it using the arrow keys or a mouse click. So, while the DropDown would read "1234", in reality the control saw "".
The fix I have in place for this is simply calling comboBox.text = comboBox.text whenever the user hits the submit button.

AutofillNewRecord type mismatch in subform

I need to fill a new record with data from the previous record.
I don't know vba but I found this article:
https://support.microsoft.com/en-us/kb/210236
Since I want all the fields to be automatically written in, I've just c/p the code and put =AutofillNewRecord([Forms]![Abc]) on the current event property of Abc. It works perfectly.
Now the problem is Abc is actually a subform for MainForm. So, if I open Abc directly it's all fine but when I open it through MainForm it says "type mismatch" and the function (?) doesn't work at all.
The expression OnCurrent you entered as the event property setting
produced the following error: Type mismatch.
The expression may not result in the name of a macro, the name of a user-defined function, or [Event Procedure].
There may have been an error evaluating the function, event, or macro.
I've searched on Google but I couldn't find a solution.
I'm attaching a bare-bones example hoping someone could help.
https://www.dropbox.com/s/6fpnhkpreb75rxw/AutofillTest.accdb?dl=0
Btw, since I'm using a non-english version of Access you might need to change =AutofillNewRecord([Maschere]![abc]) under on current event to =AutofillNewRecord([Forms]![abc]).
One solution would be to use an event procedure instead of directly entering =AutofillNewRecord([Forms]![Abc]) for the Form_Current() event:
Private Sub Form_Current()
AutoFillNewRecord Me
End Sub
I've tried with your database and it worked here, independently of whether the form is used as subform (also independent of which parent) or opened as main form.