Public Const Question

N

Nick Ward

Hi

Public Const m_strDIR As String = "C:\Chosen_Directory\Templates\" works
fine, so I don't have to declare it each function or sub. When I try:
Public Const m_strDIR As String = Application.CurrentProject.Path &
"\Templates\"
it doesn't like it. I get "constant Expression required" .Help is as much
use as a chocolate fire guard on this

TIA

Nick
 
T

Tim Ferguson

Public Const m_strDIR As String = _
Application.CurrentProject.Path & "\Templates\"
it doesn't like it. I get "constant Expression required"

Well, that is because Application.CurrentProject.Path is not a constant
expression, but a call to a method of an object (strictly speaking two
calls to two objects). It would be really nice if VBA noticed that some
functions are runnable outside the application, but it doesn't so we all
have to find another way round.

You could recast the thing as a public function:

Public Function m_strDIR() As String
m_strDIR = _
Application.CurrentProject.Path & "\Templates\"

End Function


and use the rest of the code in exactly the same way.

Hope that helps


Tim F
 
C

Chadlon

Hi Nick,

Application.CurrentProject.Path is not a constant, and to define a constant
you may only use a constant value or an expression involving constant
values.

You have (at least) two easy work arounds:
1. Change the Public Const into a variable - which you will then have to
initialise exactly as you tried to do in the Const declaration that was
refused.
Public m_strDIR as String
Sub Main
' somewhere very early in the processing
m_strDIR = Application.CurrentProject.Path & "\Templates\"
... etc

2. Even easier ... change m_strDIR into a Function:
Public Function m_strDIR As String
m_strDIR = Application.CurrentProject.Path & "\Templates\"
End Function

There are pro's and con's to each method, but you at least have a way
forward.

Good Luck
CD
 
C

Chadlon

So you had your eye on that chocolate fireguard too!

Beat me by a minute or two.
 
E

Emilia Maxim

---------- "Nick Ward said:
Public Const m_strDIR As String = "C:\Chosen_Directory\Templates\" works
fine, so I don't have to declare it each function or sub. When I try:
Public Const m_strDIR As String = Application.CurrentProject.Path &
"\Templates\"
it doesn't like it. I get "constant Expression required" .Help is as much
use as a chocolate fire guard on this

Nick,

it is exactly what it says. To declare a constant you must specify an
explicit value, you cannot use variables, functions and such. The only
thing allowed is another - previously defined - constant, something
like this:

Const conPath As String = "C:\"
Const conOtherPath As String = conPath & "MyDocuments"

So I guess you would need a variable in your case, either declared
Private in the module header so it's global within the module, or
passed as a parameter from one routine to the other.

Best regards
Emilia

Emilia Maxim
PC-SoftwareService, Stuttgart
http://www.maxim-software-service.de
 

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

Top