Dim a string

irresistible007

Board Regular
Joined
Nov 24, 2005
Messages
173
i wanna declare a string whose name has to be whatever the user has kicked in the Text Box.... is it possible?

It comes up wid the error "Expected end of statement" (at ".") when i tried:
Dim country_name.Text as String
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
I'm not sure, but it looks like you're trying to both declare type and assign value at once.

Maybe:

Dim Country_Name as String
Set Country_Name = country_name.Text
 
Upvote 0
not really i am just setting up a string, value will be there after sometime... for now just declare a string whose named country_name.Text i.e. what ever the user typed
 
Upvote 0
Ahhh ... I believe that variables can't have "." in their names. hence your error, and my interpretation as above.

Edited to add:

From VB help:

Variable names must begin with an alphabetic character, must be unique within the same scope, can't be longer than 255 characters, and can't contain an embedded period or type-declaration character.

Hope this helps!
 
Upvote 0
well i didnt meant "." but what i mean is that whatever the text property of Textbox object is, should be the name of string that i wanna declare when i press a certain command button...

hope its clear now
 
Upvote 0
Ohhh.

Hrm.

Variable variable names. I'm not sure you can *do* that. How would you refer to it later?
 
Upvote 0
irresistbile,

To assign a value to an object, you can use dot notation. For example -

MyTextbox.text = "Hello"

MyTextbox is the object, and text is a property of that object that allows text to belong to it.

If countryname is a control on a form, then you do not need to explicitly dim it. If however, you want to have a string variable that will be used in processing (such as when the user clicks a button), you can dim that as a string like so:

Code:
 dim countryname as string

You do not use the dot notation unless you are dealing with objects. If it is an object, it is a form control, otherwise it is a type, which in your case is a string, which does not use dot notation.

Hope this clears things up

Patrick

EDIT: and if you are getting at trying to dynamically assign the name of a variable that you are going to dim - i really don't think you can do that, i'd be very surprised if you managed to pull that off - but i can't think for the life of me why you would want to do that anyway
 
Upvote 0
thanks that more ppl are now taking interest in this topic...

okay since this discussion is getting even extensive... i am now trying to give you a clear picture of what exactly i am trying to do...

Here you are i am upto an imaginery project which is as under:

There is a Text Box where a user can type anything and will then click "Declare" button which will declare a string (of same name as typed by the user) and sametime will add the item on a list box... this list box will be having a list of all variables the user has declared as yet...

Another a text box, a command button "Set Value" are grouped by a group box... now this set of controls can be used to set the values for the variables set thus far.... this can be done by clicking the item on listbox (showing the variables) for which the user want to setup the value, then enteing something on text box and clicking the "Set Value" button..

Doesn't that sounds different?
 
Upvote 0
Okay, I think I know what you mean. To paraphrase:

You want to be able to dynamically assign the names and types of variables during run-time, at the users request.

So if the user said they wanted to dim a string with the value "Hello World", they would click teh relevant buttons on your form, and that would then be instantiated as a variable.

I think thats right, let me know if not.




Now, as I understand it, you cannot dimension variables at run time. At compile time, VBA will throw up errors because it simply does not know what the value of any proposed variables are (or their names or types).

There is one way that I would consider getting round this.

I would create a variant array of a pre-defined datatype that I had created to simulate what you are after. For example:

Code:
private type UserDataType
   variable_type as String
   variable_value as Variant
end type

dim simulated_array(1000) as UserDataType

So here, there is an array with 1000 possible entries, each of the type 'UserDataType'. As a user selects a variable that they want to create (say a String of "Hello World", and an Integer of 306) you can access the next empty point in the array and assign the variable_type to "Integer", or "String", and then assign the variant variable_value to "Hello World" or 306.


Hope this makes sense as some sort of a solution for you

Patrick
 
Upvote 0
Well you got it...

Now, as I understand it, you cannot dimension variables at run time. At compile time, VBA will throw up errors because it simply does not know what the value of any proposed variables are (or their names or types).

I amn't very very sure... but near to me, its possible by just triggering the click event of "Declare" Button it can dim a variable as Variant as far as values are concerened... i am pretty sure that VB doesn't care at what time the variable was declare and when was the value assigned... so much so that the types can be changed and thats where the Variants has played much imp role...

Anyways i have just gone through your code... but being a newbie.. i always get confused wid such thingy...

Couldn't figure out which sort of event is used here... where are the objects ...

Plz provide lil bit of explanation...

Thanks in advance...
 
Upvote 0

Forum statistics

Threads
1,213,533
Messages
6,114,179
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top