Solved

Number to String

Posted on 1998-07-02
8
165 Views
Last Modified: 2010-05-03
I need to convert an iniger variable to a string.  Iv'e tried this

Dim S As String
Dim X As Integer

S=X

This works sometimes but no others. When i use it repeatedly i get a type thireen error. I looked into the help files and found a ToText function under  "String Functions", however when i try to use it i get an error Sub or "Function not defined"

Sorry i only have five points but this question should be fairly simple.
0
Comment
Question by:hess
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 

Author Comment

by:hess
ID: 1464838
hold up on the answers for a little whiile i think i've got it
0
 
LVL 1

Accepted Solution

by:
Staplehead earned 0 total points
ID: 1464839
Hess,

Assuming that the data in S is numeric, you could use:

dim S as String
dim X as Integer

S = "1234"
X = CInt (S)

There's a whole family of Cxxx functions for conversions...

        Larry
0
 

Expert Comment

by:wsanchez
ID: 1464840
You could also try using Str(number).  For example:

Dim ThisString

ThisString = Str(123)  'returns " 123"
ThisString = Str(-123) 'returns "-123"

When a number is converted to a string, a leading space is reserved for its sign. Hence the space in " 123" just before 1.  This does not only convert integers but also numbers with decimal points.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Expert Comment

by:wsanchez
ID: 1464841
You could also try using Str(number).  For example:

Dim ThisString

ThisString = Str(123)  'returns " 123"
ThisString = Str(-123) 'returns "-123"

When a number is converted to a string, a leading space is reserved for its sign. Hence the space in " 123" just before 1.  This does not only convert integers but also numbers with decimal points.
0
 

Author Comment

by:hess
ID: 1464842
I siad hold up on the answers i think i've gott it and i did. It's fixed. i did it myself.
All i had to do was put the integer into a temp string and then i added the temp stings together. I guess i have to give up my five points though still cause you answered it any ways. Mabey you were writing your answer before i posted my comment but anyways, thankyou.
0
 
LVL 1

Expert Comment

by:Staplehead
ID: 1464843
hess,

yup, i didn't see your "hold on"... nonetheless, when i came back, i noticed that i typed the answer backward; it should have been:

s = CStr(x)

your comment confused me, though: what do you mean by "added temp strings together"?  if you did:
   s = "" & x
or the "less correct"
   s = "" + x
then vb did a Cstr for you, anyway...
0
 

Author Comment

by:hess
ID: 1464844
dim x as integer, y as integer
dim temp as string
dim temp2 as string
dim temp3 as string
x=6
y=4
temp=x
temp2=y
temp3=temp+temp2

this is similar to what i did
Why is string + string less correct.
It appears to work fine.
Is & faster than +
0
 

Expert Comment

by:wsanchez
ID: 1464845
+ and & can both be used in your situation but to eliminate ambiguity, avoid using + for string concatenations since it could also be used for addition of two or more expressions depending on the type of variables declared.

For example:

if + is used on two string Variants, the result is a concatenation;
if it is used on two numeric Variants, the result is addition;
if it is used on one numeric and one string Variant, the result is addition.

On the other hand,
if & is used and one expression is not a string, it is converted to a string Variant and the result is a string Variant;
if it is used and both expressions are string expressions, the result is a String;

In what you did, since temp and temp2 are both Strings, the result is also a string, other than the fact that temp3, the result, was declared as String.

0

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question