Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Declare a Public Constant with VBA 1

Status
Not open for further replies.

Quintios

Technical User
Mar 7, 2002
482
US
I have a set string that I use in a lot of message boxes:

Code:
Chr$(13) & Chr$(13) & Chr$(10)

Instead of having to cut and paste this over and over again, I'd like to set the value of 'strSkipLine' to equal those commands in a global module.

What would be the correct procedure for doing this? According to VBA help, you can't use a constant. Here's the text:

"You can't use variables, user-defined functions, or intrinsic Visual Basic functions (such as Chr) in expressions assigned to constants."

Would I just declare the string variable and set it equal to the string in a module outside any sub or function procedures? Like this:

Code:
Dim strSkipLine as String
strSkipLine = Chr$(13) & Chr$(13) & Chr$(10)

Is that the correct way to do this such that all my code behind forms and such can use it?

Thanks in advance,
Onwards,

Q-
 
Public Const strSkipLine = "Chr$(13) & Chr$(13) & Chr$(10)
"
petersdaniel@hotmail.com
"If A equals success, then the formula is: A=X+Y+Z. X is work. Y is play. Z is keep your mouth shut." --Albert Einstein

 
That doesn't work. I get a message box like this now:

"Here is your message. Chr$(13) & Chr$(13) & Chr$(10) Have a nice day."

As opposed to:

"Here is your message.

Have a nice day."

I'd like to have this variable defined once and given a value that I can use throughout the code. How can I do this?
Onwards,

Q-
 
Option Explicit
Public strSkip As String

Sub SetstrSkip()
strSkip = Chr$(13) & Chr$(13) & Chr$(10)
End Sub

Use an autoexec macro to run this code (Or on the "OnOpen" of your startup form - either way...) Kyle [pipe]
 
Forgot to mention...

The "Public declaration has to come in a separate module. Not attached to a form, you can't declare a "Public" variable in a form module. so if you were to put this in a form it should be:

Private Sub Form_Open()
SetstrSkip
End Sub

With the above code in a module. Kyle [pc1]
 
You da man Kyle!!

Works like a charm.

THANK YOU THANK YOU THANK YOU
Onwards,

Q-
 
Happy to help!

Thanks for the acknowledgement!! Kyle [pc1]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top