Setting Combobox.Selected programmatically, but I cannot get a selection to appear - ms-access

So in MS Access 2010 I have a main form for viewing client details, and tabbed subform navigation with subforms showing information in different tables for said client. On one page, i have a combobox to select a date for viewing a testing session related to the client. I am trying to get the first combobox value to be selected automatically when the user goes to this tab and/or cycles through other users while viewing this tab. My simple VB code is below:
Private Sub Form_Current()
Me.DateOfScreening.Requery
Me.DateOfScreening.Selected(2) = True
End Sub
The requery command is executing (paging through different clients will update the combobox values, and commenting that line out stops that behavior, so I know this code block is getting executed), but the Selected command appears to not select anything.
I hope I am just missing something obvious

For some reason, setting the selected row index for a combo box has not behaved well for me.
Could you just set the combobox value directly, as in
Me.DateOfScreening = "yourValue"
Also, when referencing the control, you can use either
me.dateofScreening.column(0) 'if 0 is your bound col index
or maybe
me.dateofScreening.itemdata(0)
Also, could you do a debug.print(me.dateofScreening.column(0)) or msgbox (me.dateofScreening.column(0)) and let me know if it says anything.

Related

Access SubForm recordsource revert to original

I have an Access form with two subforms, both in continuous mode. Since I cannot have a subform inside a continunous form, I had to do it that way (Datasheet is not an option either)
Either way, when I click on my first subform, I change the other subform record source using some rather simple code :
Public Sub MAJFiltre(intIdMembership As Integer)
IdMembershipFiltre = intIdMembership
Me.RecordSource = "SELECT * FROM T_PeriodeMembershipPartipant WHERE IdPeriodeMembreship=" & IdMembershipFiltre
Me.Requery
End Sub
This function is called from the first subform. I did this for another form and it was working fine. For this one, if I use a breakpoint, I can see the recordsource is changed, but nothing happen in the UI. However, if I put a breakpoint in a BeforeInsert event, I can clearly see the recordssource reverting to the original one (with no WHERE clause)
I also notice something unusual : If I save the form code while debugging, all of a sudden, it works. However, as soon as I close the form, it revert back to its "buggy" version.
Does anyway have an idea of what is going on and how I can correct /prevent it ?
Thanks
It sounds similar to a problem we suffer periodically, which relates to subforms linked to a parent form with link master/child ids: If you've (ie Access has done it for you) unintentionally saved a filter value as a property in one of the subforms, (filter by or in a record source), when your code reassigns the record source, this saved value prevents the new record source from being assigned correctly, and ms access then does what it thinks is best - in this case, reverting to the valid (prior) record source.
Check your subforms for unwanted saved values of data-tab properties.
Could the problem be that your field name is misspelled in your query criterion?
IdPeriodeMembreship
If the field in your T_PeriodeMembershipPartipant table is IdPeriodeMembERship instead of IdPeriodeMembREship your query would likely prompt you to enter the parameter value manually when it is run.
If that field does exist in your table but the value you're specifying in your criterion isn't found in that field, it will return no results and the second subform's recordsource will be set to an empty recordset.

Access: update query in sub form

Instead of using DoCmd.OpenQuery "qrySearch", , acReadOnly to view the results of a query I'm looking at using a form instead to help with presentation, including a couple of command buttons for things like export to Excel etc.
Note that the query can display a variable number of fields depending on the user's chosen criteria.
To do this I created 2 forms: frmResults and frmSub
frmSub was placed within frmResults by dragging frmSub from the Forms tab into frmResults (opened in Design view).
Under Properties of the frmSub:-
Source Object was changed from frmSub to Query.qrySearch.
The Name set at frmResultsSub
Since the original frmSub is now no longer used, I've deleted it (at least, with my limited Access experience I'm assuming it's safe to do so since there appears no effect on the query getting displayed in frmResults - the form displays nicely and the fields and field numbers vary according to the search criteria.
Question:
If the user performs another search, and frmResults is currently open, in order to update the results I have to close frmResults and Open it again. This does work but I'm thinking it's not the recommended way - is there any way to refresh frmResults while it's still open? I've tried various permutations of
Forms!frmResults!frmResultsSub.Requery / .Refresh
from behind the Search form itself but nothing works.
[ms Access 2003 compliance still required]
EDIT: based on HansUp and Alexander's 1st replies...
Alexander: the .Requery (/frmResults display) takes place close to the end of the VBA behind the Search button on form used to take in the user search criteria (frmSearch, a separate form not detailed above)...
If CurrentProject.AllForms("frmResults").IsLoaded Then
Forms!frmResults!frmResultsSub.Requery
Else: DoCmd.OpenForm "frmResults"
End If
--> it's the Forms!frmResults!frmResultsSub.Requery that is not updating for a new user search on the currently opened frmResults form.
HansUp: replacing the above line
Forms!frmResults!frmResultsSub.Requery
to
Forms!frmResults!frmResultsSub.SourceObject = "Query.qrySearch"
...did the trick, and works well - all new searches on an already open frmResults are updated without having to re-open the form. But I'm confused!...I though I already set SourceObject of the subform to the same value as detailed above (under the subform's properties in design view) - why does Access not honour this setting?
In your example code ...
Forms!frmResults!frmResultsSub.Requery
... frmResultsSub is a subform control which contains a query instead of a form. In other words, its SourceObject property is set to "Query.qrySearch".
In that situation, Requery of the subform control does not recognize changes to the query design.
Setting the subform's SourceObject property to its original value is enough to make Access recognize the query design change ...
Forms!frmResults!frmResultsSub.SourceObject = "Query.qrySearch"

Force Combo Box List Update with VBA

I am working on an MS Access Form. I am trying to have a Combo Box on the form who's list is narrowed based on information earlier in the form. I have a Query saved in the database that gets the list properly.
The problem I am having is that the query is not called after each update of the form so the Combo Box's list is always what it was when the form was opened.
I have tried to force an update using Me.MyComboBox.Requery in the AfterUpdate event of the previous control but this only requeries the record source of theform, not the row source of the Combo Box.
I have also tried changing the row source property to force a requery but that did not work eithier:
Private Sub PreviusControl_AfterUpdate()
Me.MyComboBox.RowSourceType = "Table/Query"
Me.MyComboBox.RowSource = "qryDynamicComboSource"
End Sub
Thanks in advance!
It looks like everything you're doing is correct. Check your query design if it's actually changing based on your parameters.

MS Access Double click event on query

MS Access 2007, Win 7 32-bits
Is there a way where I can access an open query in datasheet view in access to get the current field value and current field name?
I won't put it into a form since it is a crosstab query and i'd have to generate and get rid of controls dynamically but i'm not fond of messing with forms controls that way with VBA. I know i can put a dynamic column report and bind an event on the controls but I'm asking if there are events or objects that can let me access directly the query.
Perhaps a recordset clone? But I haven't find anything on google.
Of course this is in VBA
Best regards,
It may be possible to work around your requirements. The crosstab is contained by a subform:
Source Object : Query.xtab
The Control Source for the two textboxes is:
Ctrl : =[screen].[activecontrol].[name]
Content: =[screen].[activecontrol]
Which means they show which ever column ans column contents the user selects in the crosstab subform. However, they will also show any other selected control on the form. ClickMe does not change the selected control, so the selected items remain the same in the textboxes.
You can also enter MacDermott's code for getting the current control's index, so the current control selected on the xtab query subform is displayed dynamically
Public Function ControlIndex(ctl as Control) as long
Dim i as Integer
For i=0 to Me.Controls.Count-1
if me.Controls(i) is ctl then
ControlIndex=i
exit for
end if
next
End Function
And finally this can help when changing from one control to another in the same record to keep the textboxes current.

MS Access how can a subform get the value from another subform?

I have a main form that contains two other child subforms. When a textbox value changes on subform (A) I would like to set the value of a textbox on subform (B) equal to the changed textbox on subform (A).
So being completely new to Access I did some googling and it looks like this can accomplish this with some VBA. So on subform (A)'s textbox 'after update' event I have added this VBA.
Private Sub ID_AfterUpdate()
Me.Parent.Forms.formA.textboxDestination.Value = Me.textboxSource.Value
End Sub
I am doing this using the code builder. Then I save and run the form. Nothing happens. No errors.. nothing I am not even sure that piece of code is running. I'm not even sure how one debugs VBA in Access. Can someone help me out?
Thanks!
The after update event for a control fires when the user changes its value. If ID is bound to an autonumber field, the db engine will supply its value when you add a new record. However, since the user didn't make that change, the after update event does not fire.
For general debugging purposes, you can add a Debug.Print or a MsgBox statement.
Debug.Print "my event fired"
MsgBox "my event fired"
View the output from Debug.Print in the Immediate Window of the VB Editor. You can go there from your main Access window with the Ctrl+g keyboard shortcut.
Another technique is to set a break point on a line of your code. Right-click on the code line, then choose Toggle->Breakpoint from the context menu. Or click in the left margin so that a reddish dot appears to mark the breakpoint. Or press the F9 key. Or choose Debug->Toggle Breakpoint from the VB Editor's main menu.
You can also type Stop on a line by itself to trigger break mode. However, you would need to remove it later. Those other breakpoints I mentioned are temporary and don't get saved in the code module.
However you get to break mode, you can then step through the code one line at a time with the F8 key. That will show you which lines are executed. You could also inspect the value of a variable at any time in break mode by typing a question mark, then variable name, and then the Enter key in the Immediate Window:
? MyVariable
Explore the VB Editor's main menu to find additional debugging options. For example, the Watch Window will allow you to monitor the values of selected variables as you step through the code.
You don't have to specify the name of the parent form.
Me.Parent.CONTROLNAME.Value is a kind of working code.
Regards
On the update property of the textbox you are changing, put the following:
[Forms]![frmContainingValueYouWantToChange]![txtTextboxYouWantToChange] = "newValueHere"
Let us know how you get on?
I know it was 6 years ago, but I think the error here was that you were updating value from the formA to the same formA.
Me.Parent.Forms.formA actually igual to Me because you were calling :
subform (A)'s textbox 'after update' event
It should be formB and the word "Form" should be placed after (and the word "Value" isn't mandatory):
Private Sub textboxSource_AfterUpdate()
Me.Parent.formB.Form.textboxDestination.Value = Me.textboxSource.Value
End Sub
You can learn here some more informations how to Refer to Form and Subform properties and controls
Regards