How to filter subform using listbox - ms-access

Good day,
I have one form call Check Router. In that subform I have one text box (TxtPartNo), one list box (LstPartNo) and one subform (CheckRouteSubform).
These are the roles of those function
TxtPartNo --> User will key in the Item Description that they wanted to check its Route.After that,they will click add button.
LstPartNo --> its row source type is value list. After the user click Add Button, the Item Description that they key in will insert in this list box. Then they will select all data list and click on check button.
CheckRouteSubform --> This subform is based on CheckRouteQuery. After check button is click, I wanted it to be filter based on that Item Description. If it contain but not exactly equal that part_No, it will show in this subform.
example : user key in = FCLF-8531
subform will show data that contain FCLF-8531-RX, FLCF-8531-AX.
here I attached the picture of the form
Below are the picture of query :
As you can see i create criteria of Item Description based on the listbox.
and in check button I used this code:
Private Sub CmdCheckRoute_Click()
Dim i As Integer
For i = 0 To Forms!CheckRoute!LstPartNo.ListCount - 1
Forms!CheckRoute!LstPartNo.Selected(i) = True
Next i
DoCmd.RunSQL "CheckRouteQuery"
End Sub
However, it does not want to show what I need. :( Help me please.

Related

Populate a ComboBox with the Result of a Text Box

I have a form in my Access database called:
formAcademyRegister
Inside this form I have a combo box called:
EmployeeCode
This combo box retrieves its data from a table called:
Employees
The primary key on this field is EmployeeID
To make it easier for the user I created a form called formsEmployeeSearch. In this form use some text fields to make it easier to search for the employee using a first name or last name, returing the values to a list box on this form. This list box has three fields:
EmployeeID
FirstName
LastName
All these fields are also populated using the Employees table so there won't be any data type issues.
Below the list box on the search form I have a button called
butUpdateEmployeeID
When the button is clicked, I want the following to happen:
The EmployeeCode combo box in the formAcademyRegister be updated with the employeeID as selected in the list in the formsEmployeeSearch form
The formsEmployeeSearch to be closed
I am comfortable with the process of closing the form, I am however struggling with the code to populate that combo box with the value selected in the list box.
I tried the following:
Me.Form!formAcademyRegister!EmployeeCode = Me!lstEmpSearchResult.Value
But this is not working.
Try the below.
Forms.formAcademyRegister.EmployeeCode.Value = lstEmpSearchResult.Value
DoCmd.Close acForm, Me.Name, acSavePrompt
A couple of notes.
The keyword Me is not required when the code is behind the form itself, but in some situations it makes the code more readable.
The .Value property is default property (where applicable) thus can be omitted. It's best practice to keep it for clarity.
Me.Form is reference to current Form (where code is).
Forms is collection. Assignement to collection item "Collection(ItemNameOrItemIndex)
Forms("formAcademyRegister").cmbEmployeeCode.Value = Me.lstEmpSearchResult

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

Toggle Sorting of a Continous Subform between 2 fields Access VBA

I have a Form that has a continous subformcontining a few fields, important to this question is an ID field which is unique text and a due date field. Right not the form loads all records and they are sorted by the ID field by the OrderBy property of the subform, not the query.
In the parent form I have headers for each column and a toggle button to sort activate and deactivate sorting by due date. Right now the form loads sorted by ID and when the toggle button is clicked it sorts by date as I want it to. But when the toggle button is clicked again, deactivating it, the form retains its sort by date.
Here is the code I currently have behind the toggle button:
Private Sub tglSortDueDate_Click()
If Me.tglSortDueDate = True Then
Me.sbfrmFindRecords.Form.OrderBy = "DueDate, ID"
Me.OrderByOn = True
Else
Me.sbfrmFindRecords.Form.OrderBy = "ID"
Me.OrderByOn = True
End If
Me.sbfrmFindRecords.Requery
End Sub
Is this possible or should I instead change the record source to switch between 2 queries with different sorts based on my record source table?
Thanks in advance
I ended up taking a different approach, rather than sort on the form side I instead made 2 queries with the 2 diffent sorts based on the source query. I then switch between the 2 queries when the button is toggled.

Populate textboxes with value from listbox selection on child form

I realise this question has been asked before and I have tried numerous suggestions, but I guess I am just too much of a novice when it comes to VBA coding.
Here is my scenario:
I have an Access 2007 "Application" with a few forms. On the main form, I have 2 text boxes. For simplicity let's call them textbox1 and textbox2.
textbox1 is used to enter a style code. a button on the main form then opens another in modal/dialog mode and runs a query with the style code as the where clasue. the modal popup gives me a list box which is populated from the database based on the query that was passed. I then need to select one of the products in the list and upon closing the popup, populate textbox2 with the brand (column2 in the list from the popup) number.
Please remember that I am a novice. Any help would be greatly appreciated.
I would put a button on the second form, and in the _Click() function, put something like this
If ListBox1.ListIndex = -1 Then
MsgBox "Nothing Selected!"
Exit Sub
End If
UserForm1.TextBox1.Text = ListBox1.List(ListBox1.ListIndex)
Unload Me
Where ListBox1 is the list box containing the content you need the user to select from, where UserForm1 is the name of the calling form, and TextBox1 is the name.
Explanation
The ListIndex property of the list box returns the index of the selected list item. If nothing is selected, it returns a -1.
To reference an item on one from from another, you reference the form, the object and the property.
In The example I gave, the form is UserForm1, the object is TextBox1 and the Property is Text. While typing, intellisense should auto complete the object and properties once you type the period.

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.