MSAccess VBA: Pass values from a subform to a seperate form - vba

I have a form frmDetail that contains several locked fields I want populated only when another form has been filled out, in the name of having some kind of standardization in my data. This other form frmSignOut is used to enter in a date and location that will populate the fields on frmDetail. frmSignOut also contains a subform subULookup that looks up users from a different table using an identifier number. The resulting last name, first name and phone # should also be passed to frmDetail. I also hope to combine first and last name somehow into a Last,First format.
My approach so far has been to open frmSignOut modally with acDialog and I inserted Visible=False into the On_Click event on frmSignOut. Then I try to reference my subform fields and set them to the desired fields on frmDetail. I finish by refreshing and then closing the dialog form.
Private Sub CmdSignOut_Click()
DoCmd.OpenForm("frmSignOut"),,,,,acDialog
If CurrentProject.AllForms("frmSignOut").isLoaded=True Then
Set Forms!frmSignOut!SubULookup.PhoneNbrTxt=Me.ContactNbrTxt
DoCmd.Close (acForm), ("frmSignOut")
Me.Refresh
End If
End Sub
I have only been able to get this far, trying to pull the first field PhoneNbrTxt. I can get frmSignOut to open, I put in data and when I click my 'close' command button I get run-time error 438: Object doesn't support this property or method.
The line highlighted is where I try to reference my subform field. I also have a 'Cancel' button added to frmSignOut that just closes the form, I have no errors there.
Again I'm completely new to Access without much prior experience in anything related, I'd appreciate any knowledge you guys can throw at me.
EDIT: I've been able to successfully pull the value to Me.ContactNbrTxt by adjusting my code to the below.
Me.ContactNbrTxt = Forms!FrmSignOut!SubULookup.Form!PhoneNbrTxt
It looks like the missing part was the Form! right before the control name, along with formatting this correctly and dropping Set.
Im still trying to work out how to combine first and last name before also pulling those to frmDetail, if anyone can help there.

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.

Getting a type mismatch but combo box still searches and displays data. ID column is being used to pull through Name but doesn't work

Error popup after click ok on type mismatch
I am using a combo box to search for records and it is telling me I have a type mismatch. I have been unable to find where this mismatch would be. The combo box still performs the desired function of selecting all records based on their "region"
I am using an ID field which is selected using the combo box but I want to pull through a name as well so I have input an unbound textbox and used VBA on change in the ID to update the name to the corresponding ID in the unbound textbox
Form
For privacy reasons I snipped around some of the other fields showing data.
The VBA in the on_change event in ID goes
Private Sub txtSupplierID_Change()
Me.txtSupplerName.Value = Table("tblSuppliers").SupplierName
Where Table("tblSuppliers").ID = txtSupplierID
End Sub
I'm aware this code could be very wrong but I could not find anywhere else that showed me how to do what I was trying to do. Any suggestions on how to actually do it would be appreciated. If I have not been as detailed as needed or yo have any questions please ask.
Edit for Clarification:
The form is not being used to save data. It is only being used to display data and issue an output to a report.
Code is not just wrong, it is nonsense. Cannot reference a table object like that. And that Where usage is not valid. One way to pull value from a table is with DLookup domain aggregate function. Advise use AfterUpdate instead of Change event.
Private Sub txtSupplierID_AfterUpdate()
Me.txtSupplerName.Value = DLookup("SupplierName", "tblSuppliers", "ID = " & txtSupplierID)
End Sub
Might want to correct textbox name from txtSupplerName to txtSupplierName.
However, should not save both supplier ID and supplier name into this table. There really should be no need for this VBA. Just put DLookup() expression in a textbox ControlSource. For other methods of pulling the supplier name to form, review answer in MS Access Identical Comboboxes for Autofill

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

Macro in form stops working after opened with vba

Please help me explain, why this happens, after that the solution should be easy :)
I have two forms, showing different data.
Form_1: there is a combo box (with names in it), where you can choose which company you wanna see, and an after-update macro searches the record (an [ID] field), and shows the information. (To be more complicated, this [ID] field is hidden, and used for subforms, where the actual infos appear.)
Form_2: this is a continuous form, each record is in connection with the companies shown in Form_1, but several record can belong to one company. There is a button for every record to open Form_1 with the information connected to it. The vba code of the button is:
Private Sub Button_Click()
DoCmd.OpenForm "Form_1", , , "[ID] = " & Me![ID]
End Sub
In the code, the same [ID] field is used, as described above: hidden and used for subforms.
Both forms are working as needed, I am happy with them.
But after Form_1 is opened from Form_2 with the button, the combo box remains empty (actually I don't need it to be filled), and if I wanna use it to search for other items, it doesn't work, as if the macro wasn't loaded. The list of names appear, I can click on any of them, but the [ID] field is not refreshed (and of course neither the subforms). I have to close the form, and open it again from the side-list.
Why does the macro stop working?
What should I change, to make it work?
Thanks for your help!
Form1 has the filter turned on to a specific key value, so attempts to find and reposition the form's current record will fail without explicitly resetting the filter.
The Where condition of the OpenForm command does not change the form's Record Source property, nor does it perform a simple search/reposition. Rather, it applies a form filter:
DoCmd.OpenForm "Form_1", , , "[ID] = " & Me![ID]
This state is indicated in several ways
On the Home ribbon (i.e. toolbar): The Toggle Filter button is active... is highlighted by a different color.
The form's navigation bar at the bottom of the form show "Filtered" highlighted with a little funnel icon.
The Access status bar shows "Filtered" on the right near other indicators.
Of course it's possible that all of those indicators are hidden, so you just need to be aware of what each command and parameter does.
Possible solutions:
Form1's ComboBox.AfterUpdate macro should turn off the filter before searching for a new ID value.
Form2's Button_Click event opens the form without applying a filter and instead runs code that does the same thing as the ComboBox.AfterUpdate method--searches and repositions the form's record rather than filtering it.
This can be achieved in multiple ways and is largely beyond the scope of this answer, but a hint is to make a Public method in the Form1 module that performs the search. Both the ComboBox.AfterUpdate method and the other button call that same public method so they have the same behavior.

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.