[RESOLVED] Quick Question from a n00b
I am just learning and I found that I should be Declaring my variables as strings or integers from the start. I learned that I can use an * number to determin length.
Example: Dim yourName as String * 10
But if I try and do this to an integer it doesn't like it (Dim yourAge as Integer * 2)
So, does this mean I have to declare it as a string then reference it later using the val() or is there a different way to define allowed integer length?
Re: Quick Question from a n00b
Quote:
Originally Posted by
SakuyasLove
Example: Dim yourName as String * 10
You can only define fixed length for string type variables. That should answer your question about attempting to create fixed length integers.
Re: Quick Question from a n00b
Numeric data types like Integer do not have a length, what they have is a minimum and maximum value based on the data type.
For example, Integer can store whole numbers from -32,768 to 32,767
Long (long integer) can store whole numbers from -2,147,483,648 to 2,147,483,647
There are various options for storing numbers with decimal places, such as Single, Double, and Currency.
You can find info about all of this in VB's help, particularly if you search (by title) for "Data Type Summary".
Re: Quick Question from a n00b
So my thought was correct, if I want to limit the user on how many numbers to put in I need to collect them as a string, that has a length of 2 and then call it as a val() later.
Re: Quick Question from a n00b
There are many ways to limit what a user inputs. You can write conditions using If - Then blocks if you wish. For TextBoxes you can set its MaxLength property. The TextBox won't accept chrs exceeding MaxLength.
Code:
Text1.MaxLength = 8 ' TextBox limit is 8 chars, including spaces
I could be wrong but I think you're not quite grasping what Dim means. It will not restrict what a user tries to input. Instead, you will throw an error if a data is entered that does not agree with its declaration type.
Re: Quick Question from a n00b
Here's another example: ...:wave:
Use a Textbox and a Commandbutton for testing....
Code:
Private Sub Command1_Click() '~~~> On clicking the commandbutton named 'Command1'
If Val(Text1.Text) <= 0 Or Val(Text1.Text) >= 10 Then '~~~> Check the user's entry in textbox named 'Text1'
MsgBox "No...! You have to enter number within the range of 1 to 9" '~~~> If it is not in the required range, display a message
Exit Sub '~~~> Prevent the rest of the codes from being executed
End If
'~~~ If the checking is a success, then the rest of the codes will be executed
MsgBox "Good..! You have entered a value in the range of 1 to 9"
End Sub
Re: Quick Question from a n00b
Thanks, that makes more since now.