Toggle Sorting of a Continous Subform between 2 fields Access VBA - ms-access

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.

Related

How to keep focus after requery in MS Access?

I am using the following code in VBA to keep focus on a certain recordset even after requery :
With Forms!frmMain!frmMainSub
strControlName = .Form.ActiveControl.Name
lngCurrentPos = .Form.Recordset.AbsolutePosition
.Requery
.Form.Recordset.AbsolutePosition = lngCurrentPos
.Form.Controls(strControlName).SetFocus
End With
The problem with this code is that the subform is in datasheet view and is usually sorted by a field. Since the code above records the absolute position on dynaset- or snapshot-type Recordset object, it cannot keep track of the sorted datasheet.
Is there a better way to keep focus on a specific field after the form is requeired?
Edit:
This question is not a duplicate because there are answers about keeping the focus from the same form. The problem is that I am in a different form and also I want to preserve focus even when filter is applied to one of the fields.

Autofilling my form in Access with the use of a Combo Box

I'm having a problem when I want to autofill my form in Microsoft Access. The idea is that I use a combo box to select a name. Then the onChange code of my Combobox automaticlly inserts all the other data in the proper field. I use this code on the Combo Box.
Private Sub cmbName_Change()
Me.tbPersonalNumber = Me.cmbName.Column(0)
Me.tbEmailadress = Me.cmbName.Column(2)
Me.tbBirthday = Me.cmbName.Column(3)
End Sub
This methode works fine for the personalnumber and the emailadress. But it doesn't work for the birthday date, it returns a null value but when I check my table there is is a date in the proper field.
Am I missing something? I tried everything but it wont work.
I was thinking that the problem is related to the birthday column being the last in the table. Or having the date type.
Thank you in advance for your time and efford!
Edit; The .Column(1) is missing because this is the name that is already inserted with the ComboBox.
There is some confusion caused by the wording of the question, I'll try to state back how I've interpreted and if I have it right it may lead you to an answer.
You have combo box called cmdName that is pre-populated with data from a table. The content of the combo box could look as below (you may have set column widths to zero to hide the data)
0001|Gary Evans|gary#email.com|01/Jan/1970
0002|J Rommers |JR#email.com |02/Jan/1970
When the user selects J Rommers Me.tbPersonalNumber is populated with Me.cmbName.Column(0) (0002) and Me.tbEmailadress is populated with Me.cmbName.Column(2) (JR#email.com) but Me.tbBirthday is not being populated with Me.cmbName.Column(3) (02/Jan/1970).
Assuming Me.tbBirthday is a text box with no code that might clear it out, I suspect the issue is within the combo box. Not being sure how your combo box is set up, I would suggets the following checks:-
In the combo box properties, does the Column Count equal 4?
In debug, with a breakpoint on Me.tbBirthday = Me.cmbName.Column(3), does it show you the date you are after?
If it is not there does the query that populates the combo box have it in?
Edit based on comments to help further: -
Change the query to SELECT Personel.PersonalNumber, Personel.Emailadress, Personel.Birthday, Personel.Name FROM Personel ORDER BY Personel.Name; this puts all the fields you want hidden at the front.
Change the column widths property of cmbName to 0,0,0, this first the ones you want hidden and leave the last one to fill the width of the combo box.
Ensure the column count property is still 4 as per the answer
Change your code as per below and Gustav's answer
Replacement code:-
Me.tbPersonalNumber = Me.cmbName.Column(0)
Me.tbEmailadress = Me.cmbName.Column(1)
Me.tbBirthday = DateValue(Me.cmbName.Column(2))
This accounts for the fields moving in the query and ensure the date shows as a date like you wanted.
Comboboxes (and Listboxes) always return a string, so convert that to a Date value:
Me!tbBirthday.Value = DateValue(Me!cmbName.Column(3))

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 put a label as link at the end of coloumns in a subform (Datasheet) in Access 2000?

I have a database created in access 2000. A form with a subform (datasheet - defaultview) in it. I have added a label at end of the coloumns in subform and given hyperlink to open the object present in database itself. But when the form open nothing is visible after the coloumn ? I got four coloumns and had hide two columns via onload event of subform. the code is below
Me.SubGroupname.ColumnHidden = True
Me.GroupName.ColumnHidden = True
Me.BNFno.ColumnHidden = False
Me.BNFno.ColumnWidth = -2
Me.SubGroupName1.ColumnWidth = -2
How can I make it visible so that it will appear as a link at each row ?
Please help me.
If I understand you properly, you cannot use Datasheet view to display a control at the end of a row. Switch to Continuous Forms view, it will give you more control, but you will have to work a little harder to get a nice layout.
Alternatively, add a click event to one of the existing columns.

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.