By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,096 Members | 1,348 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,096 IT Pros & Developers. It's quick & easy.

Converting Numbers to Single Quotes

P: 37
I am using Visual Basic for Applications to convert a number to the same number of single quotes. For example, if the number is 6, then I need 6 single quotes in succession. The logic is easy:

Expand|Select|Wrap|Line Numbers
  1. Dim strOctave As Integer, strOctaveNew as String
  2. If strOctave = "6" Then
  3. strOctaveNew = "    ' ' ' ' ' '    "
  4. End If
Above code does not work, but the logic is correct. I need the code to replace the "6" with ' ' ' ' ' '

The below code DOES work for letters though:

Expand|Select|Wrap|Line Numbers
  1. Dim strOctave As Integer, strOctaveNew as String
  2. If strOctave = "6" Then
  3. strOctaveNew = "aaaaaa"
  4. End If
So, the code will replace the number "6" with "aaaaaa"

My logic works only if I use characters, but does not work if I use single quotes. I need single quotes to replace the number, this is important. Is there is some kind of data type I need to use for single quotes other than "string?"
Mar 23 '07 #1
Share this Question
Share on Google+
4 Replies


cyberking
P: 84
I am using Visual Basic for Applications to convert a number to the same number of single quotes. For example, if the number is 6, then I need 6 single quotes in succession. The logic is easy:

Expand|Select|Wrap|Line Numbers
  1. Dim strOctave As Integer, strOctaveNew as String
  2. If strOctave = "6" Then
  3. strOctaveNew = "    ' ' ' ' ' '    "
  4. End If
Above code does not work, but the logic is correct. I need the code to replace the "6" with ' ' ' ' ' '

The below code DOES work for letters though:

Expand|Select|Wrap|Line Numbers
  1. Dim strOctave As Integer, strOctaveNew as String
  2. If strOctave = "6" Then
  3. strOctaveNew = "aaaaaa"
  4. End If
So, the code will replace the number "6" with "aaaaaa"

My logic works only if I use characters, but does not work if I use single quotes. I need single quotes to replace the number, this is important. Is there is some kind of data type I need to use for single quotes other than "string?"
Hi,

Try this, but I am not sure. Dont have my VB studio with me now.

Dim strOctave As Integer, strOctaveNew as String
If strOctave = "2" Then
strOctaveNew = " (') " + " (') "
End if

If this does not work, try without the paranthesis.

Regards
CyberKing
Mar 23 '07 #2

P: 37
That still doesn't work. Works for all characters, even the question mark. Let me be a little clearer with the code. I am using this in Access, using DAO. Here is the watered down version:

Expand|Select|Wrap|Line Numbers
  1. Dim strOctave As Integer, strOctaveNew as String
  2. .
  3. .
  4. .
  5. .
  6. Do Until rst.EOF
  7. strOctave = rst!Octave
  8.  
  9. If strOctave = "2" Then
  10. strOctaveNew = " (') " + " (') " Does't work with or without parenthesis
  11. End if
  12.  
  13. rst.MoveNext
  14. Loop
  15.  
The bold code is what I need to get working. I still can't seem to figure out why single quotes are giving me problems....It produces wacky output, and I can't understand where the output is comming from.
Mar 23 '07 #3

SammyB
Expert 100+
P: 807
Your original code works for me, but I would use the String function instead. Here's what works for me:
Expand|Select|Wrap|Line Numbers
  1.         strOctaveNew = String(6, "'")
  2.         MsgBox strOctaveNew
  3.         strOctaveNew = "''''''"
  4.         MsgBox strOctaveNew
  5.  
Mar 23 '07 #4

P: 17
I'm not sure about this as I've not got access to Visual Basic at the moment, but ' is the symbol to start a comment, so where ever a ' is in the line of code it's converted into commenting instead. This would explain why you're getting the problem you are.

If this is the reason, then there is no way around it as far as I know.

If it isnt then there is a simple solution for any number you desire,
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.  Dim intNumber, intA as Integer
  3.  On Error goto errhandler
  4.  intNumber = Inputbox("Please enter a number")
  5.  For intA = 1 to intNumber
  6.   lblDisplay.Caption =  lblDisplay.Caption & " ' "
  7.  Next intA
  8.  Exit Sub
  9.  errhandler:
  10.  Msgbox"The entered data is invalid"
  11. End Sub
Pretty sure that'll work for you.
Mar 23 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.