How to make it DESC in such VBA ?

  • Thread starter Thread starter Martin \(Martin Lee\)
  • Start date Start date
M

Martin \(Martin Lee\)

The following VBA is workable:

If TypeOf Screen.PreviousControl Is TextBox Or ComboBox Then
Me.OrderBy = Screen.PreviousControl.ControlSource
Me.OrderByOn = True
Endif

MY QUESTION IS:

I want to make the ORDER BY arranged by DESC, HOW should the VBA ?

( Normally, me.orderby="...... desc" can work. However the upper sentence
without " " , so, I don't know how to do with the desc)


Martin Lee
 
That should work unless:
- The control is unbound, or bound to an expression (starts with "=".)
- The field name contains a space or other strange character, so you need to
enclose the name in square brackets.
- There is no previous control. (It happens!)
- The form is dirty with a record that cannot be saved (e.g. required field
missing), so the reorder cannot take place.
- The form is unbound.
- You forgot the space before DESC.

Add error handling to something like this:

With Screen.PreviousControl
If (.ControlType = acTextBox) or (.ControlType = acComboBox) Then
If (.ControlSource <> vbnullstring) and not (.ControlSource Like
"=*") Then
If Me.Dirty Then
Me.Dirty = False
End IF
Me.OrderBy = .ControlSource & " DESC"
Me.OrderByOn = True
End If
End If
End With
 
Martin (Martin Lee) said:
The following VBA is workable:

If TypeOf Screen.PreviousControl Is TextBox Or ComboBox Then

Actually, that code will not work. You need either this:

If TypeOf Screen.PreviousControl Is TextBox _
Or TypeOf Screen.PreviousControl Is ComboBox _
Then
' ... do something
End If

or else something like this:

Select Case Screen.PreviousControl.ControlType
Case acTextBox, acComboBox
' ... do something
End Select
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top