Removing the selected record from a combobox - ms-access

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

Related

How to filter subform using listbox

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.

Clear out dropdown value once another drodown value is changed in vba access?

I have three combo box controls(MFG, Code, and GrpID) in my VBA access form. Once the user selects an option from the first combo box (MFG), rest of combo boxes give me available options. But I need to do some validation i.e. what if the user decided to change the value of first combo box? The values of rest combo box should be cleared. All I need to do is once the first combo box is changed the second and third combo box need to be cleared out or at least set to focus on them so that users will realize that they can't use old values as first value is cleared in the first combo box. I added a code block 'AfterUpdate for first combo box as shown below:
Private Sub MFG_AfterUpdate()
Code.Value = " "
GrpID.Value = 0
End Sub
The problem after writing above code is: they don't get empty until they(Code and GrpID) get clicked. In other words, I need to click them to empty them every time I change the value of MFG. Can anyone direct me how do I clear them or at least focus them?
Set the combo to null to wipe any displayed value in them;
Me.Code = Null
Me.GrpID = Null
This assumes your combo controls are called the same as your field names.
Edit for clarity: Including the Me. makes sure your are changing the form control values. If your field names are the same as the controls then Access will change the underlying field values, and as you have discovered on your form these aren't reflected until you click in that fields bound control.

MS Access de-select listbox after running macro/query

So I have a form (frmBookingForm) in Access, and a listbox (lstMyBookings) that displays the results of a query.
Below this, I have a button (cmdDeleteBooking) which runs a delete query using the lstMyBookings selection as input. The button then runs a macro that firstly checks whether a record in the listbox is selected, and if one is, runs the delete query and requeries the data, so the deleted record is no longer shown in the listbox. If not, an error message is displayed.
However, if I then click the button again, it again runs the delete query, even though there is apparently nothing selected in the list box.
Essentially, how can I 'clear' the listbox selection?
I'd prefer a solution that can be done in a macro format, as I have little to no understanding of VBA. However, if providing a VBA solution, I'd greatly appreciate a short explanation of it.
Thanks :)
Looks like this website has a nice little function to do it. Essentially, you need to test if it's a multiselect, and then do one of two things. I suppose if you know ahead of time that it is/isn't a multiselect, you wouldn't even need the "if" statement:
If List0.MultiSelect = 0 Then
List0 = Null
Else
For Each varItem In List0.ItemsSelected
List0.Selected(varItem) = False
Next
End If
If the control's MultiSelect property is set to None, this code just sets List0 to Null. If the control's MultiSelect property is set to anything else, the code loops through all items that are currently selected, and sets the Selected property of that item to False. My example assumes your control is called List0.
EDIT
To use this code, use an event instead of a macro. Here's how you do this:
Right click on the button, select properties
In the Property Sheet window, click on the "Event" tab
Click inside of the "On Click" blank area, and click the dropdown arrow, then select "[Event Procedure]"
Click the ellipsis ("...") to go into the code editor.
In the code editor, your should already have your event for the button (let's assume the button is called Command1:
Private Sub Command1_Click()
End Sub
Add your code in between (assuming the listbox is called List0):
Private Sub Command1_Click()
If List0.MultiSelect = 0 Then
List0 = Null
Else
For Each varItem In List0.ItemsSelected
List0.Selected(varItem) = False
Next
End If
End Sub

Continuous form only referring to the first record

I have a continuous form - one of the fields on the form is RecordID.
I have a label on that form, that when clicked should produce a message box with the RecordID in it via VBA:
MsgBox Me.RecordID
The label is reproduced on each row of the Continuous Form, but will only reference the first record. Even though I can see the RecordID field is different in each row of the form, I always get the same result, in this case 80029.
What's up with that?
Me.RecordID refers to the RecordID of the current record, as indicated by the black triangle in the record selector:
A Label control on a form cannot receive the Focus, so when you click on the label in another record the current record does not change and you keep getting the same RecordID. Note that if you put the same code into the Click handler for a textbox (or some other control that can receive the Focus) then the current record will change and you'll get the RecordID of that record.

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.