PDA

View Full Version : Defining constants



chamster
10-09-2007, 12:35 AM
I tried to define
Private Const lineBreak = Chr(13)
but the box said it didn't like, since it's a function. Well, technically speaking it is but since i've specified a constant for parameter, it actually is a character. How can i solve this one?

yogi_bear_79
10-09-2007, 03:51 AM
Not sure I get the point, but this should work. Are we to assume that in place of typing Chr(13) somewhere in your code you would like to type lineBreak instead?

Private Const lineBreak As String = Chr(13)

Paul_Hossler
10-09-2007, 05:24 AM
There are already a number of VB constants available, including vbBack, vbCr, vbCrLf, etc.

Use the object browser (F2) and look for VBA.Constants

Paul

johnske
10-09-2007, 05:40 AM
There are already a number of VB constants available, including vbBack, vbCr, vbCrLf, etc.

Use the object browser (F2) and look for VBA.Constants

Paulalso look at vbNewLine

chamster
10-09-2007, 05:58 AM
Not sure I get the point, but this should work. Are we to assume that in place of typing Chr(13) somewhere in your code you would like to type lineBreak instead?

Private Const lineBreak As String = Chr(13)

Yes, you are to assume that. And aaha! to the other replies. I didn't know that
a) such constants existed (C++ damage, i guess)
and
b) you can/should/must specify the type of a constant.

Thanks to all of you, guys (girs?)!

chamster
10-11-2007, 11:26 PM
Not sure I get the point, but this should work. Are we to assume that in place of typing Chr(13) somewhere in your code you would like to type lineBreak instead?

Private Const lineBreak As String = Chr(13)

This code seems not to compile. To be specific, i need to define the quotation mark character as i can't use " in a string. So i tried:

Private Const vbQuote As String = Chr(34)

but the computer complains. What to do?

johnske
10-12-2007, 12:21 AM
Option Explicit
'
Private Const vbQuote As String = """"
'
Sub usequote()
MsgBox " Yes " & vbQuote & "No" & vbQuote & " questions"
'but what's wrong with using
MsgBox " Yes ''No'' questions"
End Sub

unmarkedhelicopter
10-12-2007, 04:22 AM
Or even :- MsgBox "Get """Apples""" Here"

johnske
10-12-2007, 05:10 AM
or MsgBox " Yes ""No"" questions" - but frankly, in a message box the double apostrophes appearance is much more like a quotation mark than the quotation mark itself UMH :)

unmarkedhelicopter
10-12-2007, 05:16 AM
Yeah, your are right on both counts, my other brain cell is still asleep :)

Paul_Hossler
10-12-2007, 03:49 PM
Chamster -- choices are

1. Use the already defined vbCrLf constant, or

2. Public linefeed as String, quote as String

and inside a Sub use

linefeed = Chr(13)
quote = Chr(34)


Option Explicit
Public linefeed As String, quote As String

Sub aaa()
linefeed = Chr(13)
quote = Chr(34)
Call MsgBox("1. This is " & quote & "TOP LINE" & quote & linefeed & "This is " & quote & "BOTTOM LINE" & quote)
Call MsgBox("2. This is " & quote & "TOP LINE" & quote & vbCrLf & "This is " & quote & "BOTTOM LINE" & quote)
End Sub


Paul

Cyberdude
10-14-2007, 11:56 AM
OK, you have the choice between
vbCr
vbLf
vbCrLf
vbNewLine

Why would anyone use vbCrLf or vbNewLine when they can type just 4 characters (vbCr or vbLf)??
Well, according to Help, vbNewLine will automatically select whichever is appropriate for the platform being used (Windows or Macintosh). I personally got into the habit of using vbCr since I always use Windows.

I STRONGLY advise that you forget trying to define your own symbol.