Reference a subform from main form Error - ms-access

Must be losing my mind. For some reason I can't get this simple procedure to work. It's a pulldown where user chooses what data they would like to see. I just need it to change the recordsource of the subform and refresh it. It's basically just substituting one filtered query for another. Can't seem to access the subform through main form. Not sure if this is the best way, but it's the way I know.
Private Sub PeriodSelect_Change()
If PeriodSelect.Value = "Active" Then
Me!ServiceWindow.SbfmService_Item.RecordSource = Service_Active
ServiceWindow.Requery
Else
If PeriodSelect.Value = "PDI" Then
Me!ServiceWindow.SbfmService_Item.RecordSource = Service_PDI
ServiceWindow.Requery
End If
End If
End Sub
I get Error 438 Object does not support this property or Method. Can't quite figure out what I've missed.
Any help is greatly appreciated.

You're trying to set the RecordSource on the subform control, not the form inside the subform control. Use the Form property to access that form:
Private Sub PeriodSelect_Change()
If PeriodSelect.Value = "Active" Then
Me!ServiceWindow.SbfmService_Item.Form.RecordSource = "Service_Active"
ServiceWindow.Requery
Else
If PeriodSelect.Value = "PDI" Then
Me!ServiceWindow.SbfmService_Item.Form.RecordSource = "Service_PDI"
ServiceWindow.Requery
End If
End If
End Sub

Related

VBA update subform recordsource

Quick and easy one (Probably) for you.
My home form has a lot of sub forms within tab controls. Some of these sub-forms have their own sub-form based off a query and that query has criteria input control on the parent form.
As you can imagine upon loading the home form I would have to enter all the parameters in pop-up boxes as the subforms/queries are also loading.
My work-around for this is setting those query sub-forms recordsource to ""
Then once the user control that applies the where condition is updated it changes their record source to the query.
Only, my code (Below) doesn't work. And After half hour of staring at it I can't figure out why, it produces no errors it just does nothing.
All fields/records just have #NAME?
Private Sub txt_EventID_AfterUpdate()
Me.txt_forcefocus.SetFocus
Me.sub_SpeakerOnboarding.Form.RecordSource = qry_SpkrOnboard
Me.Requery
End Sub
I also tried the following:
Private Sub txt_EventID_AfterUpdate()
Me.txt_forcefocus.SetFocus
Me.sub_SpeakerOnboarding.Form.RecordSource = qry_SpkrOnboard
Me.Refresh
End Sub
Again no results, nothing... All fields/records just have #NAME?
Hopefully I've missed something simple otherwise I'll have to rethink the entire form designs
RecordSource is a string:
Private Sub txt_EventID_AfterUpdate()
Me.txt_forcefocus.SetFocus
Me.sub_SpeakerOnboarding.Form.RecordSource = "qry_SpkrOnboard"
End Sub
To refer to a control on the subform:
Value = Me![SubformControlNAME].Form![txt_EventID].Value

Receiving Invalid Qualifier error when attempting to access a form and subform using variable names?

I am trying perform a RecordsetClone but I keep getting a
Invalid Qualifier
compile error.
Currently I have a form, and within it I have a subform that contains a Datasheet. I created a function to pass in the name of the form as well as name of the subform:
Public Sub testModel(nameOfForm As String, nameOfSubform As String)
Dim myForm As Access.Form
Dim mySubForm As Access.SubForm
If formIsOpen(nameOfForm) = False Then
DoCmd.OpenForm nameOfForm, acNormal
End If
Set myForm = Forms(nameOfForm)
Set mySubForm = myForm.Controls(nameOfSubform )
With Forms!myForm.name.mySubForm.name.Form.RecordsetClone
...
End With
End Sub
The reason for using a variable for the form and subform is because different forms and subforms will be using this function.
The error is definitely involved with the syntax Forms!myForm.name.mySubForm .name.Form.RecordsetClone, and I'm pretty confident it is incorrect.
I've also tried:
Forms!myForm.name.mySubForm.Form.RecordsetClone
If I replaced it, directly using the name of the form and subform name, there is no issue, i.e.:
Forms!frmParentForm.frmChildSubForm.Form.RecordsetClone
I have printed out the name of myForm and mySubForm via its .name property and it appears to display the correct name for each.
Can someone point me in the right direction on how exactly I can use variable names in place of the actual names of my form and subform? What should the correct syntax be?
Thanks!
Remove those .name things. They're not supposed to be there:
With Forms!myForm.mySubForm.Form.RecordsetClone
...
End With
If you're going to use that declared subform variable, it's even more simple:
With mySubForm.Form.RecordsetClone
...
End With

Access VBA - Turn off aggregation when opening form/sub-form

What I'm trying to do is, whenever a user opens a form (and the sub-form that opens by default), I want to search through all the columns (controls?) on the form, check to see if they are currently set to aggregate (sum, count etc.) with Access' built-in Totals row, and if so, set them to not aggregate.
The reason for this is there are several millions records that are stored, so when someone queries it down to 3-4 and turns on Sum, then closes it, when the next person opens it, it tries to sum millions of numbers and freezes up. The form displays the queried results from a table which is populated via SQL (I think, if that sentence makes sense). Here's what I have so far:
Private Sub Form_Load()
'this form_load is in the UserApxSub sub-form, for reference
Call De_Aggregate
End Sub
Private Sub De_Aggregate()
Dim frm As Form, con As Control
Set frm = Forms!UserAPX!UserApxSub.Form!
For Each con In frm.Controls
If con.ControlType = acTextBox Then
If con.Properties("AggregateType").Value <> -1 Then
'crashes on following line
con.Properties("AggregateType").Value = -1
End If
End If
Next con
End Sub
I have not so much experience in Access VBA (usually work in Excel VBA) so please forgive me if I'm entirely off the mark here. The command con.Properties("AggregateType").Value = -1 doesn't throw an error, but Access just straight-up crashes when reaching that line specifically.
I've tried a number of variations in the syntax with no success, and I've also tried looping through other elements of the file (tabledefs, querydefs, recordsets, etc.) as well to see if I'm trying to change the wrong value, but the controls on this subform are the only things in the entire .mdb file that results when I search for elements with the AggregateType property.
I switched out the line that errors with Debug.Print con.Name & " - " & con.Properties("AggregateType").Value and I can check, have nothing return anything other than -1, turn on aggregation in some column manually, and have it return the correct result (0 for sum for example), so I think I'm looking in the right place, just missing some key factor.
I've been working on this for a couple weeks with no success. Any way to fix what I have or point me toward the right direction would be greatly appreciated!
This is not necessarily the answer but I don't have enough reputation
to give a "comment"...
I tried your scenario and verified can change the property value as you are however I did not iterate through all controls and simply used an onDoubleClick event on a column to simulate.
I would suggest trying to fire your sub with Form_Open or Form_Current to see if the property is getting reset after your code has been called for some reason.
UPDATE:
You are referencing the "Subform" Object of your main Form:
Set frm = Forms!UserAPX!UserApxSub.Form!
Try referencing the actual UserApxSub FORM explicitly.
Something like Set frm = Forms!UserApxSub! (assuming UserApxSub is the name of the form)
then stick in the Form_Open of your main form:
Private Sub Form_Open(Cancel As Integer)
'// the following would set a single control only. You can add your loop through all controls
Me!{your control name}.Properties("AggregateType").Value = -1 '// set control in your main form
Form_UserApxSub!{your control name}.Properties("AggregateType").Value = -1 '// set control in your "sub" form
End Sub

Run Script After On Current

I am sure this is really simple but i just cant make it work. I have a form with three sub forms. The main form loads and finds the requested record fine and if i run the below from a button on the main form it works fine. What i want to do is have it run after form loads requested record and with out user interaction. I have tried on load and on current but as the form is not yet on screen there are no values to populate the variables and the msgboxes are blank. But like i said fired from a button on the main it works fine.
any help would be greatly appreciated.
actCount = [ActMovDelete].Form![Text93].Value
PlaCount = [PalnMovDelete].Form![Text91].Value
noCount = [NotesDelete].Form![Text50].Value
MsgBox actCount
MsgBox PlaCount
MsgBox noCount
I'm not sure if I understand your question, but if you are you trying to active the code upon initialize, then just
Private Sub UserForm_Initialize()
[your code here...]
End Sub
If however, you are trying to populate variables of forms that are not loaded, then a better approach might be to store the variables as public variables in the forms. This should give you access to the variables even when they are not loaded. Just as an example of how this might work:
In the form from where you want to get the variable (named UserForm1):
Public strFromTheForm As String
Private Sub UserForm_Activate()
TextBox1.Value = strFromTheForm
End Sub
From another form or a module, you can include the code:
Public Sub Test2()
Dim strTest As String
strTest = UserForm1.strFromTheForm
If strTest = "" Then
UserForm1.strFromTheForm = "Test"
End If
UserForm1.Show
End Sub

Make fields visible in MS Access form as certain fields are completed

I am building a form In MS Access for users to input data but there are too many possible fields. Most of the time only about half the fields will be used.
I thus would like to have certain fields appear only depending on what the user inputted on a prior given field.
Ex: user enters project number, title, then he checks a "yes/no" engineering. since he checked it this means engineering is impacted so a dozen fields that the user will have to fill out appear.
Is this possible:
1)without VBA
2)with VBA
Probably not possible without VBA.
With VBA for example:
Ensure your form is in Design view
Right click on your Combo Box, Build Event, Code Builder
This opens the code behind your form. It drops you into the default code for the BeforeUpdate event. We want the Change event instead, so at the top right change the drop down from BeforeUpdate to Change. This will give you a bit of code like this:
Private Sub Field1_Change()
End Sub
Inside here, you want to check the value of the combo box and hide fields as required:
Assuming the name of your combo box is Field1 (yours of course will be different), you add some code so it looks like this to hide Field2:
Private Sub Field1_Change()
If Field1.Value = "Yes" Then
Me.Field2.Visible = False
End If
End Sub
Note you need to know the names of all your fields - this is in the Other tab, Name field in the properties box (F4). You should give all of your fields sensible names so you can understand what is going on in the code.
For a check box, follow exactly the same procedure, but you probably need to use the Click event. Just experiment.
Sample check box code:
Private Sub Check5_Click()
' Note: vbTrue = -1
If Me.Check5 = vbTrue Then
MsgBox ("Ticked")
Else
MsgBox ("Not Ticked")
End If
End Sub
I have a form that will show certain fields after a list box value is selected. I use the AfterUpdate function. It has worked so far. My code is below. ProjectName and ProjectNumber are fields you only want displayed if Engineering is selected. OtherName and OtherNumber are fields you only want to show if it is a "NotEngineering" project.
Insert this code by clicking on the Field that selects the project type, go to the Event tab on the property sheet, and click "After Update" and choose code builder and paste in VBA.
Private Sub ProjectType_AfterUpdate()
If ProjectType.Value = "Engineering" Then
Me.ProjectName.Visible = True
Me.ProjectNumber.Visible = True
Else
Me.ProjectName.Visible = False
Me.ProjectNumber.Visible = False
End If
If ProjectType.Value = "NotEngineering" Then
Me.OtherName.Visible = True
Me.OtherNumber.Visible = True
Else
Me.OtherName.Visible = False
Me.OtherNumber.Visible = False
End If
End Sub
There is a way to do not-quite-this without VBA. I'd definitely recommend VBA though, because you can do a lot more with it.
Rather than hiding, try disabling the unnecessary fields with conditional formatting by expression.
right-click on the control you want disabled.
go down and click on 'Conditional Formatting'
Add a new rule
Select 'Expression is'
example:
[fld1]="yes"
hit the disabled box
click ok
click ok
now the control you've selected will disable if field 1 has "yes" selected.
I have the same problem and I did the following:
Private Sub Field1_Click()
If Field1 = "Yes" Then
Me.Field2.Visible = True
Else: Me.Field2.Visible = False
End If
End Sub
but now I have other problem, when I change record, the field that I choosen to be visible in the last record is now visible on the current record, although I have not choosen any option.
Thank you,