How to get element value of selected row in a subform - ms-access

I have a Sub Form in MS Access as below :
Is there any way to get the string value of the selected row when the user click on the row ?
I have tried using the ReportKey as follow :
SELECT MyRowName From TAB_MySubFormName WHERE TAB_MySubFormName.ReportKey=" & ReportKey
but the result of the ReportKey is always 1 which I guess it's the first element even thought I have clicked on the last element in the table

I end up finding my error
I had to precise the SUB_Form.Form.ReportKey no need to precise the sub form name just simply add SUB_Form.Form.ReportKey , which means I had to do this :
SELECT MyRowName From TAB_MySubFormName WHERE TAB_MySubFormName.ReportKey=" & SUB_Form.Form.ReportKey

Try this code in your main form:
MsgBox Nz(Me.YourSubform.Form!SomeField)

Related

How to pass value from subform to mainform ms access

I have a form with a subform (Datasheet view form). When i click a row in subform, i want pass that row value to text box in main form . In subform even wrote this
Private Sub Form_Click()
MsgBox (Noloan)
End Sub
But i don't know how to pass that subform form_click no_loan to mainform.textbox. Thanks for your help.
Additional data:In my db , i have a table which consist field No,NoLoan,Name. NoLoan Field value as i wanna clicked .From that table i create a subform/datasheet view ,subform name is T_Loan1. Then I create Main Form, name is FindLoan2. In that main/parent form i create text box, called Text7, and put T_Loan1 in subform object at footer.
I put this
=[Forms]![FindLoan2]![subformTLOAN].[Form]![Noloan]
in my Text7 Control and It's work.
Thank's for all attention.
Goto to the subform and click on the field which has the row value
Select Field property - click on events tab --> On Click --> Code Builder
Put below code in the Click event where Form_frmMain is your form name and Me.ID is the field.
Private Sub ID_Click()
Forms!FindLoan2.Text7 = Me.ID
End Sub
The best way is to write code that ignores the forms!names. If you copy the form, or change the sub form, or even drop the sub form into a different form, then all your code breaks.
To reference the textbox in the parent form from code, use:
me.Parent!Text7 = me!id
I am not sure which version of MS Access you are using, but it is also possible to use Tempvars to store values for use elsewhere if you have a complicated application which makes referencing another form/field/control difficult.
To create:
Tempvars.Add "TempVarName", TempvarValue
To use:
Tempvars!TempVarName
To remove:
Tempvars.Remove "TempVarName
I try to limit my use of them, as it becomes hard to manage. I am always careful about when I set the values and I ensure that they are removed after I am finished with them to avoid confusion or issues with values persisting when they shouldn't.

Select multiple values in a dropdown listbox on html via VBA

Issue : Hey, I am trying, from VBA,to select multiple values in a dropdown list on a webpage.
The HTML code of the select element is
I managed to select this element with objIE.document.getElementsByTagName("select")(0) since it's the first element with the tagname "select" on the whole webpage.
If I write objIE.document.getElementsByTagName("select")(0).Value= "E31", the value "E31" will be highlighted in the drop box, that's fine.
But how can I select multiple values ?
I tried objIE.document.getElementsByTagName("select")(0).Value= "E31" + "E32" or objIE.document.getElementsByTagName("select")(0).Value= "E31" & "E32" but it doesn't work...
EDIT : Further attempts : I am currently trying to fix this issue using the SendKeys command where I click on one element of the list and then select other elements with SendKeys "+{DOWN 4}" which should select the 4 elements in the list under the initial one selected.
'objIE.document.getElementsByTagName("select")(0).Children(8).Selected = True' If I activate this line it selects (= highlight) the 9th element of the list
'objIE.document.getElementsByTagName("select")(0).Children(8).Click'if I activate this line it clicks on the 9th element of the list but doesn't highlight it
SendKeys "+{DOWN 3}"
Unfortunately this doesn't work yet. Moreover, I don't know if there is a way to send a key CTRL + MouseLeftClick somehow as mentioned in here but for the command Send which is not valid in VBA.
Any ideas ?
Solution : Ok I found a way to do it, pretty easy actually.
With objIE.document.getElementsByTagName("select")(0)
.Children(23).Selected = True
.Children(56).Selected = True
.Children(98).Selected = True
End With
This command will highlight (select) the 3 elements

Removing the selected record from a combobox

I have an access form with a combobox for Suppliers. I have a '...' button control next to the combobox, which opens a new supplier form if the combobox is empty or goes to the selected supplier if its occupied. My issue is if the user selects a record and then realises its wrong and wants to add a new supplier.
When deleting the supplier name, either by delete button or backspace, the record seems to be still selected. However, the '...' button doesn't work. Trying to navigate away from the record means that I get an error saying You must enter a value in the Order.supplier_ID field.
Is there any way of clearing the selection easily?
Can I clear the selection without this error? Allowing the user to navigate away from the combobox and select the '...' button
Will I need VBA, and where do I even start?
Something like this should help you :
store the index of selected item
change it to the next one or the previous one
remove the item that was selected before
Here is the code :
Dim SelectedITM As Long
With Order.supplier_ID
SelectedITM = .SelectedItem
If SelectedITM <> .ListCount - 1 Then
.Selected (SelectedITM + 1)
Else
.Selected (SelectedITM - 1)
End If
.Items.Remove (SelectedITM)
End With

How to determine if any row is selected in a listbox

I have a list box in my Access form. I need to know if any of the rows in this list box has been selected by the user. Is there any simple Access property or method exists for this purpose? I do not want to loop through the listbox to check if any row's selected property is true, because I am only interested to know if a selection action is done.
A list box has the ItemsSelected property which returns a read-only reference to the hidden ItemsSelected collection. And you can ask for the Count property of that collection ...
MsgBox Me.YourListBoxName.ItemsSelected.Count & " items selected"
The code
If ListBox.ListIndex = -1 then
MsgBox "Nothing selected"
end if
should help...
this worked for me...a mix of both comments before, thank you both
If List4.ItemsSelected.Count = 0 Then
MsgBox "Nothing selected"
So if nothing is selected I get the message

combobox cascade effect not working for different rows

I have two comboboxes on a subform.The first combobox is used to populate the second combobox. These are placed in the detail section of the form. I want them to work this way:when I select any value from the first combobox, I want the second combobox of the same row to get populated by relevant value.
As of now, I have tried to implement this and as I select any value from the first combobox of row 1 I see the second combobox of the same row gets populated but as I go on selecting values from the first set of comboboxes I see that the values in the second set of the comboboxes above changing or becoming null.
Here's the code:
The 1st combobox is cboRCMTask:
Private Sub cboRCMTask_AfterUpdate()
Me.cboRCMTaskOptions.RowSource = "SELECT ID, RCMTaskOptions FROM tblRCMTaskOptions WHERE RCM_ID=" & Me.cboRCMTask.Column(0) & ";"
Me.cboRCMTaskOptions = Me.cboRCMTaskOptions.ItemData(0)
Me.cboRCMTaskOptions.Requery
End Sub
cboRCMTaskOptions is the second combobox.
The form_current event:
Private Sub Form_Current()
Me.cboRCMTask.RowSource = "SELECT ID, RCMTask FROM tblRCMTask;"
If IsNull(txtRCM_ID) Then
Me.cboRCMTask = Me.cboRCMTask.ItemData(0)
End If
Me.cboRCMTaskOptions.RowSource = "SELECT ID, RCMTaskOptions FROM tblRCMTaskOptions WHERE RCM_ID=" & Me.cboRCMTask.Column(0) & ";"
If IsNull(txtRCMOption_ID) Then
Me.cboRCMTaskOptions = Me.cboRCMTaskOptions.ItemData(0)
End If
End Sub
From your description, you are using a continuous form. While it looks like a continuous form has many rows, from the point of view of coding you can consider it to have just one row, the current row. I suspect that the control source for combo 2 is a hidden, numeric column in the combo, when you change the row source for the combo, the visible row can no longer be found, so it cannot be displayed. You will either have to provide a pop-up form for editing, or a textbox to store the value for the form and a slightly dodgy combo to edit that value. You can do a little to improve the appearance with conditional formatting.
in your first piece of code, shift the code to the on_click event. I am not sure what you are trying to achieve with the reference to ItemData but I think it is unnecessary, comment out that line.
Similarly the third to last line in the Form_current event, replace with a requery.