473,725 Members | 2,212 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can a variable hold 2 values simultaneously - a string value and a numeric?

MLH
120 MyString = "How many copies of each letter do you need?"
150 MyVariant = InputBox(MyStri ng, "How Many?", "3")
If MyVariant = "2" Then MsgBox "MyVariant equals the string '2'"
If MyVariant = 2 Then MsgBox "MyVariant also equals the value 2"
160 If MyVariant = "" Then HowManyCopies = 1
170 If Not IsNumeric(MyVar iant) Then HowManyCopies = 1
MsgBox "OK. HowManyCopies has a value of " & CStr(HowManyCop ies)
180 For i = 1 To HowManyCopies
190 DoCmd.OpenRepor t "rptITSnoti ces"
200 Next i

OK, what I'm trying to accomplish with the above code is to
have the user input a NUMBER indicating how many copies
of a report he wants. AND, I am trying to determine whether
the user entered gibberish (IE, "q1w2e3r4t5 ") instead of a
number. AND I'm trying to determine whether the user entered
nothing at all. So, I've dim'd MyString as string and MyVariant as
variant.

So, when I run it and I enter the number 2 at the InputBox prompt,
MyVariant equals BOTH the string "2" and the value 2 when I check
it immediately afterwards. That's very confusing for a single variable
to have 2 values - one numeric and the other a string.
Dec 19 '05 #1
20 3713
MLH <CR**@NorthStat e.net> wrote:
: 120 MyString = "How many copies of each letter do you need?"
: 150 MyVariant = InputBox(MyStri ng, "How Many?", "3")
: If MyVariant = "2" Then MsgBox "MyVariant equals the string '2'"
: If MyVariant = 2 Then MsgBox "MyVariant also equals the value 2"
: 160 If MyVariant = "" Then HowManyCopies = 1
: 170 If Not IsNumeric(MyVar iant) Then HowManyCopies = 1
: MsgBox "OK. HowManyCopies has a value of " & CStr(HowManyCop ies)
: 180 For i = 1 To HowManyCopies
: 190 DoCmd.OpenRepor t "rptITSnoti ces"
: 200 Next i

: OK, what I'm trying to accomplish with the above code is to
: have the user input a NUMBER indicating how many copies
: of a report he wants. AND, I am trying to determine whether
: the user entered gibberish (IE, "q1w2e3r4t5 ") instead of a
: number. AND I'm trying to determine whether the user entered
: nothing at all. So, I've dim'd MyString as string and MyVariant as
: variant.

: So, when I run it and I enter the number 2 at the InputBox prompt,
: MyVariant equals BOTH the string "2" and the value 2 when I check
: it immediately afterwards. That's very confusing for a single variable
: to have 2 values - one numeric and the other a string.

I have *an opinion* as to why this is, but I will let the experts
tell you the actual situation.

But this input-check seems very incomplete to me: it doesn't check
for negative numbers, nor for large numbers -- surely you don't
mean to allow a print run in the billions. And this is a personal
preference: I would force reinput for an entry like q1w2e3r4t5,
and if for some reason that's not an option, I'd set HowManyCopies to
the original default value, 3.

--thelma
Dec 19 '05 #2
I'm no expert myself but it seems to me that your variant is equating
to the string "2" and the integer 2 because it is a variant. Variants
have to explicit data type (and therefore take up a lot of overhead),
and I would think that Access simply stores the value you assign it (in
this case, 2) and tries to accurately compare it to any data type you
test the value with. For example, if this is correct, Access will try
to cast the variant to a Currency value if you try If
myVariant=myCur rencyValue .

What I would do is get rid of the variant altogether, unless there is
another reason it's there, and stick with the strongly-typed String
variable. After the user inputs the number, do your checks and perhaps
even cast it to an integer. I agree with Thelma that you need to do a
lot more input validation before using the value. One thing you could
do to lower the amount of code you need is to check the length of the
input first. For example, if the user enters more than three
characters, you know it's either a mistake or they're just fooling
around. Then you could check legitimate 3-digit values for negatives,
IsNumeric, etc., as Thelma noted.

Dec 19 '05 #3
As others have said a variant holds data of any type also VBA will try to
cast the data to the type you want.

So in your example the MyVariant variable can be considered to be equal to
both "2" and 2.

If you are trying to force a number from the user you might look at

Dim intInput as Integer

intInput = Val(InputBox(My String, "How Many?", "3"))

'intInput now contains a number you can then test for special cases e.g.

Select case intInput
case 0
MsgBox "You entered 0 cancelling the operation"
case > 10
MsgBox "You entered more than 10 cancelling the operation"
case else
For i = 1 To HowManyCopies
DoCmd.OpenRepor t "rptITSnoti ces"
Next i
end select

--
Terry Kreft

"MLH" <CR**@NorthStat e.net> wrote in message
news:hr******** *************** *********@4ax.c om...
120 MyString = "How many copies of each letter do you need?"
150 MyVariant = InputBox(MyStri ng, "How Many?", "3")
If MyVariant = "2" Then MsgBox "MyVariant equals the string '2'"
If MyVariant = 2 Then MsgBox "MyVariant also equals the value 2"
160 If MyVariant = "" Then HowManyCopies = 1
170 If Not IsNumeric(MyVar iant) Then HowManyCopies = 1
MsgBox "OK. HowManyCopies has a value of " & CStr(HowManyCop ies)
180 For i = 1 To HowManyCopies
190 DoCmd.OpenRepor t "rptITSnoti ces"
200 Next i

OK, what I'm trying to accomplish with the above code is to
have the user input a NUMBER indicating how many copies
of a report he wants. AND, I am trying to determine whether
the user entered gibberish (IE, "q1w2e3r4t5 ") instead of a
number. AND I'm trying to determine whether the user entered
nothing at all. So, I've dim'd MyString as string and MyVariant as
variant.

So, when I run it and I enter the number 2 at the InputBox prompt,
MyVariant equals BOTH the string "2" and the value 2 when I check
it immediately afterwards. That's very confusing for a single variable
to have 2 values - one numeric and the other a string.

Dec 19 '05 #4
InputBox will always return a string. When you execute "If MyVariant = 2"
Access converts the string to a number to do the comparison. As others have
posted, that is not an effective way to determine if the entered data has a
numeric value.

--
Randy Harris
tech at promail dot com
I'm pretty sure I know everything that I can remember.
"MLH" <CR**@NorthStat e.net> wrote in message
news:hr******** *************** *********@4ax.c om...
120 MyString = "How many copies of each letter do you need?"
150 MyVariant = InputBox(MyStri ng, "How Many?", "3")
If MyVariant = "2" Then MsgBox "MyVariant equals the string '2'"
If MyVariant = 2 Then MsgBox "MyVariant also equals the value 2"
160 If MyVariant = "" Then HowManyCopies = 1
170 If Not IsNumeric(MyVar iant) Then HowManyCopies = 1
MsgBox "OK. HowManyCopies has a value of " & CStr(HowManyCop ies)
180 For i = 1 To HowManyCopies
190 DoCmd.OpenRepor t "rptITSnoti ces"
200 Next i

OK, what I'm trying to accomplish with the above code is to
have the user input a NUMBER indicating how many copies
of a report he wants. AND, I am trying to determine whether
the user entered gibberish (IE, "q1w2e3r4t5 ") instead of a
number. AND I'm trying to determine whether the user entered
nothing at all. So, I've dim'd MyString as string and MyVariant as
variant.

So, when I run it and I enter the number 2 at the InputBox prompt,
MyVariant equals BOTH the string "2" and the value 2 when I check
it immediately afterwards. That's very confusing for a single variable
to have 2 values - one numeric and the other a string.


Dec 19 '05 #5
MLH <CR**@NorthStat e.net> wrote in
news:hr******** *************** *********@4ax.c om:
So, when I run it and I enter the number 2 at the InputBox prompt,
MyVariant equals BOTH the string "2" and the value 2 when I check
it immediately afterwards. That's very confusing for a single
variable to have 2 values - one numeric and the other a string.


Well, first off, don't use a variant for your input type. If you use
an integer, you can then create an error handler that will return a
useful message for anything that can't be stored in an integer. The
principle here is to *never* use a data type that can hold values
other than the type that you really need.

And why is 2 = 2 and 2 = "2"? Because VBA does a lot of implicit
type coercion. When you ask:

?2="2"

and get back TRUE, what VBA is telling you is that it *can* be true,
i.e., that the value you are comparing can be coerced to a data type
that makes the statement true.

That's not a terribly useful piece of information when you really
want to know that the value really *is* a number. That's why it's
important to never rely on implicit type coercion.

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/
Dec 19 '05 #6
MLH:

I may be way off base, but maybe it would be easier to use a form with
either a text box format to an integer, or maybe even a combo box with
the values 1 through 10 that would GUARANTEE that you get proper
input...

Just a thought,
Jana

Dec 19 '05 #7
I am MOS certified. Personally I would use a form. In fact, you may
want to reference a table so you can store the value the user used.
Then next time he calls the form, the previous form value will be the
default. Create a table that has only one record, then set your form
not to allow additional records. In turn, you can set validation on
the form if you have a print button.

Also on the form a text box called msg but do not reference a field in
the table.

If Me.count>10 then Me.msg = "Cannot print more than 10 copies"
IF Me.count<1 then me.msg = "Not able to print zero copies"
else Me.Msg = "Printing " & Me.Count &" copies"

Something like that.

Dec 19 '05 #8
MLH

I have *an opinion* as to why this is, but I will let the experts
tell you the actual situation.

But this input-check seems very incomplete to me: it doesn't check
for negative numbers, nor for large numbers -- surely you don't
mean to allow a print run in the billions. And this is a personal
preference: I would force reinput for an entry like q1w2e3r4t5,
and if for some reason that's not an option, I'd set HowManyCopies to
the original default value, 3.

--thelma

Yeah, you're right thelma. Its not complete. Dim'd number of copies
as byte - limits copies somewhat.
Dec 20 '05 #9
MLH wrote:
Yeah, you're right thelma. Its not complete.


It's not complete?

Dec 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
6954
by: keweiming | last post by:
I have a project which needs to open hundreds to thousands of files for writing. The following is a simplified test program I wrote to see if I can use a map<string, ofstream> object to keep the list of ofstreams. I need to have them open simultaneously for writing -- I have millions of rows of data to write to them so opening and closing all the time will be unacceptable in efficiency. The following program compiles but failed to run....
16
25416
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have the properties: mode1, mode2, and mode3. This seems simple but I can't quite figure it out... Any ideas anyone?
4
13585
by: DerekM | last post by:
I have a database project that I created with several forms and reports. I would like to be able to declare a single title and be able to change the title on all the forms and reports by changing a single variable. I think this is possible, however I don't know Visual Basic very well. I created a module named DBcommon with the following statements in it: Option Compare Database Global Const DBname As String = "Title String"
14
5842
by: Luiz Antonio Gomes Pican?o | last post by:
How i can store a variable length data in file ? I want to do it using pure C, without existing databases. I'm thinking to use pages to store data. Anyone has idea for the file format ? I want to store data like a database: ---------------------------------- Custumer:
14
2382
by: Rich | last post by:
Yes, I need to store some values in an array type collection object that can hold 3 or more parameters per index. I have looked at the collection object, hashtable object and would prefer not to hassel with a multi-dimensional array. Is there such an object in VB.Net? Dim obj As someCollectionObj obj.Add("parmA1", "parmA2", "parmA3") obj.Add("parmB1", "parmB2", "parmB3") ....
12
8457
by: obdict | last post by:
Hello, I have a simple C question regarding scanf() /* What happens if user inputs a char when he/she is supposed * to input an integer? */ #include<stdio.h> int main(void){ int n;
6
5337
by: yaron | last post by:
Hi, my application use environment variable, let call it FOO. how can i add the FOO environment variable to my project or my solution, so the line string foo = Environment.GetEnvironmentVariable("FOO");
61
3768
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch (myBranch + x) = new Branch(i); // it doesn't x is a loop iterator, i is an int for the constructor to define an array. What am I doing wrong here.
8
2842
by: Jerry | last post by:
I am a MySQL and PHP newbie. I am having trouble getting the $w variable in my code below passed to mysql. When I use the value of $w directly in the Where clause, the correct rows are returned. However, when I try to use the variable in the Where clause, either an error occurs or no rows are returned. Any thoughts greatly appreciated! I am using php-4.4.2 and MySQL-4.1.18-0 on Suse 9.1 Jerry
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.