setting order to mutiple fields (1 Viewer)

John Sh

Member
Local time
Today, 19:47
Joined
Feb 8, 2021
Messages
408
I think I have a syntax problem.
If I use the following code the system works fine. I can scroll through the records as expected.
Code:
DoCmd.SetOrderBy "boxno" & "," & "family" & "," & "accessionnumber"
    Me.OrderByOn = True

If I use the following code I get a message " Can't go to that record" even before the form has loaded.
Code:
sortOrder = "boxno" & "," & "family" & "," & "accessionnumber"
    DoCmd.SetOrderBy sortOrder
    Me.OrderByOn = True

Ideally I would like to be able to change the sort order via a subform that loads the "sortorder" variable with the required fields.
These fields are selected via three comboboxes and combined with the following code
Code:
Private Sub btnSort_Click()
    If Me.cboSort1 <> "" Then
       sortOrder = cboSort1
   End If
   If Me.cboSort2 <> "" Then
        sortOrder = sortOrder & ", " & cboSort2
    End If
    If Me.cboSort3 <> "" Then
        sortOrder = sortOrder & ", " & cboSort3
    End If
    DoCmd.Close acForm, Me.Name
End Sub

The sort order is then changed in the parent form's oncurrent event
 

June7

AWF VIP
Local time
Today, 01:47
Joined
Mar 9, 2014
Messages
5,468
If the second code snip is in same event as the first, it should work just as well.

I don't understand use of subform to provide filter for main form. Code closing a subform does not make sense.

Might provide db for analysis. Follow instructions at bottom of my post.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:47
Joined
May 7, 2009
Messages
19,230
is it a "subform" or a "pop-up" form?

when you open the "sortorder" form, pass the calling form's name to it, so that when you close the "sortorder" form
you can "refresh" the calling form.

see this demo
 

Attachments

  • sortOrder.accdb
    672 KB · Views: 271

John Sh

Member
Local time
Today, 19:47
Joined
Feb 8, 2021
Messages
408
Junee7. I've fixed my problem. It isn't in the sorting but the move through the records that I was attempting afterwards.
My main form is a single form and doesn't take kindly to moving through multiple fields being ordered.
I'll have to do a bit more work on this, so I thank you for your time.
John

<is it a "subform" or a "pop-up" form?>
It is actually a pop-up form.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:47
Joined
May 21, 2018
Messages
8,527
Are you sure that works?
An empty combobox should have a value of Null not "". I would be surprised that worked. At least I would do
Me.cboSort1 & "" <> ""
 

Users who are viewing this thread

Top Bottom