Link to home
Start Free TrialLog in
Avatar of Derek Brown
Derek BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

OrderBy

What is the correct expression  for a command button in the form header on a continuous subform ("SetsSubForm") to order the records  by IDNumber (which is a Text field if it is relevant)
Avatar of Hamed Nasr
Hamed Nasr
Flag of Oman image

Command button click event:

Me.RecordSource = "Select * from tbl Order BY ID Desc"
Me.Refresh

modify tbl, ID, Desc as needed.
Default is ascending.
Code behind your command button:

Me.OrderBy ="[IDNumber]"   ' OR Me.OrderBy= "IDNumber"
Me.OrderByOn = True

Descending
Me.OrderBy ="[IDNumber] DESC"   ' OR Me.OrderBy= "IDNumber DESC"
if that is a subform, try

me![subformcontrolName].orderby="[IDNumber]"
for descending
me![subformcontrolName].orderby="[IDNumber] Desc"

me![subformcontrolName].orderbyon=true
or

me![subformcontrolName].Form.orderby="[IDNumber]"
for descending
me![subformcontrolName].Form.orderby="[IDNumber] Desc"

me![subformcontrolName].Form.orderbyon=true
Avatar of Derek Brown

ASKER

Thanks Hnasr

Don't really know if I should post this here but previous thread a week ago has closed.

In a in a procedure that runs from the attached continuous  form.  I'm using

Me.RecordSource = "Select * from SetsQuery Order By Val(TrimNumString([DoorNumber]))"

Where setsQuery is the forms record source

I copied a function from another thread earlier

I get out of stack space.

What am I doing wrong?

Public Function TrimNumString( _
  ByVal strNumString As String, _
  Optional ByVal strDecimalChr As String, _
  Optional ByVal booAcceptMinus As Boolean) _
  As String

' Removes any non-numeric character from strNumString including hexadecimal characters.
' If strDecimalChr is specified, first occurrence of this is not removed.
' If booAcceptMinus is True, a leading or trailing minus sign is accepted.
'
' 1999-08-27. Cactus Data ApS, CPH.
' 2001-06-21. Speed optimized for large string (64 K).
' 2003-12-10. intOffset changed to lngOffset.
 
  Const cbytNeg   As Byte = 45  ' "-"
 
  Dim lngPos      As Long
  Dim lngLen      As Long
  Dim lngOffset   As Long
  Dim booDec      As Boolean
  Dim booNeg      As Boolean
  Dim bytChr      As Byte
  Dim bytDec      As Byte
  Dim strNum      As String
 
  strNumString = Trim(strNumString)
  lngLen = Len(strNumString)
  If lngLen > 0 Then
    If Len(strDecimalChr) > 0 Then
      bytDec = Asc(strDecimalChr)
    End If
    ' Create empty result string of maximum possible length.
    strNum = Space(lngLen)
   
    For lngPos = 1 To lngLen
      bytChr = Asc(Mid(strNumString, lngPos, 1))
      Select Case bytChr
        Case 48 To 57
          ' Digit.
        Case bytDec
          ' Decimal point.
          If booDec = False Then
            ' One decimal point only.
            booDec = True
          End If
        Case cbytNeg
          ' Minus sign.
          bytChr = 0
          If booAcceptMinus = True And booNeg = False Then
            If Len(Trim(strNum)) = 0 Or lngPos = lngLen Then
              bytChr = cbytNeg
              ' One minus sign only.
              booNeg = True
            End If
          End If
        Case Else
          ' Ignore any other character.
          bytChr = 0
      End Select
      If bytChr > 0 Then
        ' Append accepted character by inserting it in result string.
        lngOffset = lngOffset + 1
        Mid(strNum, lngOffset) = Chr(bytChr)
      End If
    Next
  End If
 
  ' Trim and return result string.
  TrimNumString Left(strNum, lngOffset)
End Function
Thanks Ray

Do I need the brackets around "[DoorNumber]"
ASKER CERTIFIED SOLUTION
Avatar of Hamed Nasr
Hamed Nasr
Flag of Oman image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great thanks
Welcome!