2

I am trying to place a sub on one line in Access VBA. Is there a setting or special syntax that can be used to accomplish this?

    private sub cmd_print() docmd.printout end sub 'creates an error

vs

    private sub cmd_print() 
    docmd.printout 
    end sub
Erik A
  • 31,639
  • 12
  • 42
  • 67
Daniel L. VanDenBosch
  • 2,350
  • 4
  • 35
  • 60

1 Answers1

10

Technically, yes, using the instructions separator - the colon : token:

Private Sub PrintCommand() : DoCmd.PrintOut : End Sub

I would strongly advise against it though, for obvious maintainability and readability reasons.

This particular layout is used by some add-ins such as vbWatchDog, for some generated code. Because of how it challenges the expected structure of a module, it is known to cause problems with add-ins that process the code, such as Rubberduck (which I'm heavily involved with) (parser has been updated since then; this is no longer an issue) - that's because the member attributes will be stored (you need to export the module to see them, the VBE doesn't display module and member attributes), rather counter-intuitively, outside the procedure scope:

Private Sub PrintCommand() : DoCmd.PrintOut : End Sub
Attribute PrintCommand.VB_Description = "This procedure does XYZ"

The "normal" layout looks like this, and doesn't cause issues:

Private Sub PrintCommand()
Attribute PrintCommand.VB_Description = "This procedure does XYZ"
    DoCmd.PrintOut
End Sub

It's easier to read, too.

Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
  • Hey thanks, My own readability of my code is what drove this question. I find it more readable because it allows for more screen real estate along the y axis of my monitor. – Daniel L. VanDenBosch Apr 07 '17 at 14:58
  • Cramming code in as little vertical space as possible rarely *improves* readability, **especially** when scopes are involved. YMMV. – Mathieu Guindon Apr 07 '17 at 15:05