Requerying a subform with unbound controls - ms-access

I have a subform with unbound image controls that are being populated through code. I am using this subform to display search results when a SEARCH button is clicked. The problem is that I cannot refresh/requery the subform to reflect the current search status, except when I close and open again the form (subform) as a standalone/popup.
I tried this but failed to requery:
With Me.F_Person_Thumbnails_control
.SourceObject = Forms.F_Person_Thumbnails
End With
I will appreciate any help.
Joseph

Try accessing your unbound subform from the parent form.
' To Requery
me.mySubForm.form.requery
' To set a TextBox
me.mySubForm.form.controls("MyTextBox").value = "Test"
You can also access fields from your subform using the form.parent object, although I personally consider this a messier approach - since you are generating errors if the subform is opened by itself. Using the above approach lets you re-use the subform in other places in your database, with different logic (if required). Accessing parent object:
Me.MyTextBox.Value = Me.Parent.Form.Controls("IDNumber").Value

Related

Error 3251 when setting record source for subform

I've been having this error where I am unable to set the record source for a subform.
Just a bit of background, the form in question is structured as such: frm1View is my unbound main form which contains 2 subforms, subfrm1Particulars and subfrm1Datasheet. The issue is with subfrm1Particulars, which itself contains 2 subforms in a tab control.
I am attempting to change the recordsource of my subform in subfrm1Particulars dynamically, based on the records found in the other subform (one sub form displays courses completed and the other displays courses scheduled; the idea is to remove scheduled courses they have already completed from that sub form view).
I am using the following code to do this dynamic recordsource change. The code is contained in subfrm1Particulars(which contains subfrmScheduledCourses)
strSQL = *blah blah blah where etc etc not like etc*
Me.subfrmScheduledCourses.Form.Recordsource= strSQL
I've checked numerous times, my subform control name is correct (although my subform shares the same name as the control, so maybe that's the issue). I have almost the exact same form configuration in another form, with the same code (i.e. the same recordsource and SQL statements and method of assigning recordsource) and it works perfectly, so I can't tell why this isn't working.
It gives me an error 3251. I'm running this in the On Enter event of the subform control (in both of my forms where this particular code appears).
Any help at all is much appreciated!!
Referring to subform objects can be tricky, especially from other subforms. Try calling the object differently:
Forms!frm1View!subfrm1Particulars.Form!subfrmScheduledCourses.Form.RecordSource
What this does it begins referring to form objects by navigating from mainform to subform to the desired area.

How to partially clear userform in ms access

I have a bound userform with lot of fields in it, I use submit button to let user make entries in shared form. But whenever user clicks on submit button it requery whole form. However, there are around 4-5 fields which should not be cleared on submit button, they should retain the value and rest of the fields should get cleared on every submit button click.
Can this be done in access and how ? below is the simple code is use to submit and requery the record.
Private Sub SaveBtn_Click()
RunCommand acCmdSaveRecord
Me.Requery
Me.DataForm.Requery
End Sub
When you have a "Bound Form" in Access, this means that the Access Form is tied to the underlying data source, which is specified by, and stored in, the "Record Source" property for the form. In the "SaveBtn_Click()" method you provided above, this code does the following things:
RunCommand acCmdSaveRecord - Saves the current record in the form.
Me.Requery - Requeries the entire form.
Me.DataForm.Requery - Requeries the subform named "DataForm" on your main form.
So when you "Requery" the entire form (depending on how the "Record Source" and other form property settings are setup), the requery operation in some cases moves the cursor to the new record in the data source (...its the default settings for the drag and drop form designer tools in later versions of Access), and I suspect that is why you see the form being "cleared" when you call "Requery." A couple of items I would point out, just for clarity:
Note 1: "Requery" actually saves the record, so explicitly calling "RunCommand acCmdSaveRecord" should not be necessary if you're going to call "Requery" too.
Note 2: Depending on how the Record Source (and other) form properties are set, Requery in some cases just saves and refreshes the currently selected record, but it doesn't sound like that is how your form is working (based on what you said above), so I'm assuming that is not the case in your form.
Note 3: The subform will also be requeried when you call "Requery" for the main form, so that line of code may also be redundant here. The reason to call "Me.DataForm.Requery" is if you only want to requery the subform and NOT requery the entire main form.
Note 4: The "DataForm" subform (on your main form) will have a separate data source for it's own "Record Source" property, which is separate from the parent (main) form, so it is important to be aware of that fact, depending on which field values you want to save.
So, that said, there are a couple of options I might suggest, depending on exactly how you want your form to behave:
If you want to keep some of the field values and use those for the NEW RECORD when you hit the requery button, you could always save them off in a variable and then set those controls again after requerying from your variables. Just create a seperate variable for each value you want to save, copy the TextBox control values into each of variables respectively, call requery on the form, and then copy those values back into your TextBox controls after you requery. That code would be something like the following (depending on the exact names of your TextBox controls, I used the fictitious name "txtMyTextBox" for this example):
Private Sub SaveBtn_Click()
Dim vValue1 as Variant
vValue1 = txtMyTextBox
Me.Requery
txtMyTextBox = vValue1
End Sub
Or, if you're just trying to create "Default Values" for certain controls, TextBox controls have a "DefaultValue" property you can set to any value you would like to use for the default. Those can be set from the Property Sheet on the right side of the Access window when the form is opened in Design mode or Layout mode and the TextBox control is selected. (Press F4 to open the Property Sheet if it's not already open).
But, if you really want to Requery and then go back to the same record you were previously on, you could try the following code:
Private Sub SaveBtn_Click()
Dim iRecord as Integer
iRecord = Me.CurrentRecord
Me.Requery
DoCmd.GoToRecord , , acGoTo, iRecord
End Sub
Anyway, I hope this all makes sense and you find it helpful. Please let me know if you have any further questions about this answer.

Access - Do Not Query Subform When Parentform is Opened

I have an MS Access form, that contains a subform whose recordset is a query that takes quite a while to process and isn't actually needed right away when the parentform is opened, but only after a button on the form is pressed.
Is there a way to implement this with VBA other then completely changing the form with something like Set Me.MySubForm.Form.Recordset = NULL? I've tried Me.MySubForm.Enabled = False but that doesn't work unfortunately.
The easiest way:
Leave the SourceObject property of the subform control empty. Then when the button is pressed, do
Me.MySubForm.SourceObject = "mySubFormName"
This way the subform and its data isn't loaded until needed.

Access VBA: Unbound subform controls are not clickable

I do have an unbound search form, in which a listbox is filled with search results. Upon selection of one listbox item and click on a button "view details" the bound form RSdetails is displayed
30 DoCmd.OpenForm "RSdetails", , , "[ID]=" & selectedID, , acDialog
The bound form RSdetails contains an unbound subform RSmails. All controls in subform RSmails are unbound as well.
The intention of this subform is to establish a connection to a Lotus Notes mailbox and to perform a search for mails containing a certain key.
The subform has 2 dropdowns to select the view and the range of time to search in, 2 buttons (search and abort) and a listbox to display the matches.
Now if I open this form RSmails directly I can make selections in the dropdowns and click the buttons.
If this form (RSmails) is viewed as subform of RSdetails I can NOT make any selection. The dropdowns and buttons are enabled, but show no reaction.
What I already tried, but failed to succeed:
open RSdetails in acWindowNormal instead in acDialog
bind RSmails to a table which has a relation to the table RSdetails is bound to and definitely has a recordset with the selectedID
Any idea is appreciated!
Thanks,
Thomas
Apparently you put the subform with lock edition. Check the properties for the subform (into the form) and verify that:
- Can add? (i think must be NO if you have all content created into)
- Can Delete? (i think must be NO because only to read records)
- Can Edit? (i think actually you have NO and must be YES)
Conclusion: The problem was solved by exporting every table/ table-link, form, query, modul to a blank new project. The problem disappeared, the app works like expected.
Did not find the real reason, but according to above work around, it looks like an undefined hick up in Access.

Forms and Subforms in Access 2003

I have only one table and I would like to create a form for the users to populate the table easily. There are at least 5 fields in the table that only needs to be completed if a certain type of inspection (Fire) is selected from a drop down list and is left blank otherwise.
I would like to create a subform that will only pop up when inspection type "Fire" is selected from the drop down list in the main form. How can I do this?
I used the wizard to create the form and I am stuck because I really don't know VBA. So far, I went to the inspection type field on the form, clicked on "Properties", then clicked on "After Update", then selected the Macro that I created to open the subform when the inspection type = "Fire", but it isn't working.
What happens is the subform gets opened up no matter what type of inspection I select, then the ID number on the subform doesn't correspond to the main form (the subform ID will remain on id#1). Also, when I do input data in the subform, the information ends in the next record.
I was wondering if that is happening because I am using both the form and the subform to enter data into the same table. I hope this is a clear explanation of what I want to do.
Just to try to improve a bit on Kovags excellent proposition:
Sub InspectionType_AfterUpdate
Subform1.Visible=(InspectionType.Text="Fire")
End Sub
Sub Form_Current
InspectionType_AfterUpdate
End Sub
Just change this code to adapt your needs and add it to the Change Event of the Inspection Type textbox:
Sub InspectionType_Change
if InspectionType.Text="Fire" Then
Subform1.Visible=True
else
Subform1.Visible=False
endif
End Sub
If the fields are on the same table then I'd suggest placing the fields on the subform onto the main form. Then making them visible or not depending on the state of the combo box.
Using a subform gets a bit more complex as, among other things, you will need to save the record in the main form before opening the subform. Otherwise the subform won't be able to update the record and will give you a locking message.