I'm using
DoCmd.SetOrderBy
for sorting a form by a specific field. But right now it sorts ascending or descending if I use DESC keyword. How can I do a check to see what sorting is active and do the opposite? If that field is ordered ascending and I click the button, the order become descending and vice versa.
You can get the current sort order by doing in the form code
debug.print me.orderby
It will come out something like
[FORM].[COLUMN] Desc
So then do an if statement
if me.orderby = [FORM].[COLUMN] Desc then
docmd.setorderby "[COLUMN] ASC"
else
docmd.setorderby "[COLUMN] Desc"
end if
Try that out. Changing the FORM and COLUMN to match yours of course
As #Sam suggested, check the value of the Me.OrderBy property to decide whether you want ascending or descending for the new sort order.
The following code sample assumes you want an ascending sort if there is no current sort (i.e. Me.OrderBy is an empty string).
Otherwise check whether Me.OrderBy is Like "* DESC". Note, if the current sort order is ascending, don't assume ASC will be present in Me.OrderBy. And don't assume the column name piece of Me.OrderBy will be present as [FORM].[COLUMN].
Dim strOrderBy As String
Dim strDirection As String
strOrderBy = Me.OrderBy
If Len(strOrderBy) = 0 Then
strDirection = "ASC"
Else
If strOrderBy Like "* DESC" Then
strDirection = "ASC"
Else
strDirection = "DESC"
End If ' Like
End If ' Len(strOrderBy)
DoCmd.SetOrderBy "[YourColumnName] " & strDirection
Related
I've looked all over and found nothing. What I'm doing is showing one table using a sort/filter function in another sheet. I've simplified my formulas for your viewing pleasure, which are as follows:
=SORT(FILTER(A2:J23,
(C2:C23<>L2)+(D2:D23*M2),
(C2:C23<>L3)+(D2:D23*M3),
(C2:C23<>L4)+(D2:D23*M4),
(C2:C23<>L5)+(D2:D23*M5),
(C2:C23<>L6)+(D2:D23*M6),
(C2:C23<>L7)+(D2:D23*M7)),
A29, TRUE,
B29, FALSE,
C29, FALSE,
D29, FALSE,
E29, FALSE,
F29, FALSE,
G29, FALSE,
H29, FALSE,
I29, FALSE,
J29, FALSE)
The goal is simple. I want to simplify them, perhaps by using a range. They're the exact same formulas using one ascending variable.
If it helps, this is what it's doing:
(C2:C23<>L#) checks if col C equals a "type" in col L.
(D2:D23*M#) column M contains check boxes (True/False). Multiply that by arbitrary positive numbers in col D to get array of numbers to OR with first part.
Together, these formulas say, "If type matches, and button is unchecked, don't show row". Boolean logic is A+B' or (A'B)'.
In the sort part, there is a row of check boxes A29:J29 (1/0, 2/0, 3/0, ...). When pressed, the table is sorted by that column, A-Z in col A, and largest first Cols B-J.
EDIT: I've made a mock sheet to better illustrate what's going on, and updated the code to match this sheet. It can be found here: https://docs.google.com/spreadsheets/d/1cOre8sVOb3TE2OsaNC823UB18DAMO4pD4-mZqJtxu0k/edit?usp=sharing
Here is the formula:
=QUERY(
A2:J23,
"SELECT * "
& "WHERE C is not null "
& IF(COUNTIF(M29:M34, False) = 0, "", "AND NOT C MATCHES '" & JOIN("|", IFNA(FILTER(L29:L34, M29:M34 = False))) & "' ")
& IF(COUNTIF(A29:J29, ">0") = 0, "", "ORDER BY " & JOIN(", ", IFNA(FILTER(REGEXEXTRACT(ADDRESS(29, COLUMN(A29:J29), 4), "^\D+") & IF((COLUMN(A29:J29) = 1) + (COLUMN(A29:J29) = 3), "", " DESC"), A29:J29)))),
0
)
This one is a bit different than your previous question, because column letters must be used instead of ColN. More on that here.
And I made 3rd column sorted asc if checked because it is of a string type.
I want to use the ascending or descending option dynamically based on xpath value.
let $order := "ascending"
for $aRslt in ("1","2","3","4")
order by ($aRslt)
if($order eq "ascending") then ascending else descending
return $aRslt
Using this throwing Error.
We can have the if condition for the whole "for" statement. But when we have more conditions in where; order by; and lot of statements in return, then it looks code duplicating just for the ascending or descending.
Is there any option to use without using condition for the whole "for".
Whenever I've had to order dynamically this is whats worked best for me.
It takes advantage of multiple order spec. (http://www.w3.org/TR/xquery/#id-orderby-return) This is a bit different because of order Modifier being a token and not a variable or expression so it can be hard to work with.
And that's why you have to have the two if statements and the empty else statements.
let $order := "ascending"
for $aRslt in ("1","2","3","4")
order by
if($order eq "ascending") then $aRslt else() ascending,
if($order eq "descending") then $aRslt else () descending
return $aRslt
I have a collection string date values some of which are empty strings ("").
when I use OrderBy clause, following statement
theList = theList.OrderBy(Function(x) x.age).ToList()
returns empty string collection.
However, if I use OrderByDescending operator, the result is correctly ordered by descending data values.
Sample date values are "2014-10-31 00:00:00.000", "2014-09-30 00:00:00.000", "2014-11-30 00:00:00.000", "", "2014-08-31 00:00:00.000".
What could be the problem please?
Thank you,
Piyush Varma
My first instinct would be that if x.billedthru is ever shorter than 10 characters, you could see this behavior. Does it return rows if you try this:
Dim returnCollection As List(Of FundedAccountsDetail) = New List(Of FundedAccountsDetail)
Select Case sortBy
Case "billedthru"
If Ascending = "ASC" Then
returnCollection = reportCollection.OrderBy(Function(x) x.BilledThru).ToList()
ElseIf Ascending = "DESC" Then
returnCollection = reportCollection.OrderByDescending(Function(x) x.BilledThru).ToList()
End If
Case Else
returnCollection = reportCollection
End Select
If it does, then the problem is the substring. If not, I admit that it is perplexing. What I'd probably try is then removing the order by from the first branch of the if and see if it returns rows. Basically work my way back until that branch of the if works and then rebuild it step by step until I start having the problem.
Hey, can I ask you something? I'm using VB6.0 and I have some problem with the connection of my Database through DataControl. I have a table named tblEmployee and the other one is tblPosition then I passed the value of the two tables to two DataControls respectively. How can I then get the value of a certain row of Position field. With my code, my Position field returns only the first row. Here's my code
Private Sub cmdSearchEmployee_Click()
With datEmployee.Recordset
datEmployee.Recordset.Index = "idxid"
datEmployee.Recordset.Seek "=", txtIDNumber.Text
If .NoMatch = True Then
MsgBox ("No Record Found!")
Else
Me.txtLastName.Text = .Fields("lname")
Me.txtFirstName.Text = .Fields("fname")
Me.txtMiddleName.Text = .Fields("mi")
With datPosition.Recordset
Me.txtPosition.Text = .Fields("position")
End With
End If
End With
End Sub
I can't see that you have "passed the value" to your DataControl named datPosition. Could this be the problem? e.g. where you have
With datPosition.Recordset
Me.txtPosition.Text = .Fields("position")
End With
...should be more like this:
With datPosition.Recordset
.Index = "some_index??"
.Seek "=", "some_value??"
Me.txtPosition.Text = .Fields("position")
End With
Also consider using the recordsets' Filter to remove the rows that do not match your criteria then RecordCount to loop through the rows that do match your criteria.
Further consider returning a single recordset by creating a join between tblEmployee and tblPosition, either in SQL code or returning a hierarchical recordset using the MsDataShape with its SHAPE syntax.
I have a table that is filled with random content that a user enters. I want my users to be able to rapidly search through this table, and one way of facilitating their search is by sorting the table alphabetically. Originally, the table looked something like this:
myTable = {
Zebra = "black and white",
Apple = "I love them!",
Coin = "25cents"
}
I was able to implement a pairsByKeys() function which allowed me to output the tables contents in alphabetical order, but not to store them that way. Because of the way the searching is setup, the table itself needs to be in alphabetical order.
function pairsByKeys (t, f)
local a = {}
for n in pairs(t) do
table.insert(a, n)
end
table.sort(a, f)
local i = 0 -- iterator variable
local iter = function () -- iterator function
i = i + 1
if a[i] == nil then
return nil
else
return a[i], t[a[i]]
end
end
return iter
end
After a time I came to understand (perhaps incorrectly - you tell me) that non-numerically indexed tables cannot be sorted alphabetically. So then I started thinking of ways around that - one way I thought of is sorting the table and then putting each value into a numerically indexed array, something like below:
myTable = {
[1] = { Apple = "I love them!" },
[2] = { Coin = "25cents" },
[3] = { Zebra = "black and white" },
}
In principle, I feel this should work, but for some reason I am having difficulty with it. My table does not appear to be sorting. Here is the function I use, with the above function, to sort the table:
SortFunc = function ()
local newtbl = {}
local t = {}
for title,value in pairsByKeys(myTable) do
newtbl[title] = value
tinsert(t,newtbl[title])
end
myTable = t
end
myTable still does not end up being sorted. Why?
Lua's table can be hybrid. For numerical keys, starting at 1, it uses a vector and for other keys it uses a hash.
For example, {1="foo", 2="bar", 4="hey", my="name"}
1 & 2, will be placed in a vector, 4 & my will be placed in a hashtable. 4 broke the sequence and that's the reason for including it into the hashtable.
For information on how to sort Lua's table take a look here: 19.3 - Sort
Your new table needs consecutive integer keys and needs values themselves to be tables. So you want something on this order:
SortFunc = function (myTable)
local t = {}
for title,value in pairsByKeys(myTable) do
table.insert(t, { title = title, value = value })
end
myTable = t
return myTable
end
This assumes that pairsByKeys does what I think it does...