Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple vb question

Status
Not open for further replies.

jmongan

Mechanical
Oct 7, 2004
39
I am writing a bit of code to run with a macro in SW. I have the following:

If txtNumber.Text <> IsNumeric Then
MsgBox ("Use only numbers.")
GoTo waithere
End If

The IsNumeric give an error "The argument is not optional" I have a test box being populated with 5 characters I want to ensure that only numbers are uses else give the error "use only numbers"

Any thoughts on making this work.

Thanks,

Josh
 
Replies continue below

Recommended for you

If you are using isnumeric then it should return some value like true or false. So the arguement should also be given in your if statement stating the message box pops up for isnumeric value = true or false. I havent done much VBA and am not a programmer, but I learnt VB once so I know this much. Try passing an arguement as true or false for IsNumeric and it should work.

Regards

vik
 
I found a way to make this work.

If (Not IsNumeric(txtNumber.Text)) Then
MsgBox ("Use only 5 digit number at end of file name.")
GoTo waithere
End If
 
from VB help (where else???)
Code:
MyVar = "45 Help"    [COLOR=green]' Assign value.[/color]
MyCheck = IsNumeric(MyVar)    [COLOR=green]' Returns False.[/color]

[bat]I could be the world's greatest underachiever, if I could just learn to apply myself.[bat]
-SolidWorks API VB programming help
 
Just one question jmongan. What is the purpose of the GoTo waithere? Something I have not seen when using an event driven language like VBA (not even sure it is considered good progarmming practice anymore).

Regg
 
Regg,

To be honest with you I dont know the purpose. I am about 3 days into programming with VB. I found that code on the www, copied it and tweaked it to look for txt.number.txt.

 
Regg

You're right, but you must be tolerant. Some of us are not programmers (or didn't learn this type of object/event driven language at school). Some hepful macros are done just "by gathering" some lines of code and a lot of trial and error. They may not look nice or reveal a good programming practice, but if they work then they are fine!

Regards
 
GoTo isn't such a bad call, really. It's not common, but sometimes nothing else will do. I use it to break out of loops or set an exit/cleanup strategy for a subroutine (rather then just Exit Sub). GoTo is also good for error handling.

"waithere" must be the name of a line in the program. Look for a line that says "waithere:"--the colon denotes that it is the name for a line used for GoTo.
 
If the help for your VB or VBA is unavailable, look for a file named "VBUI6.chm". I keep a shortcut to it on my desktop.
 
Very few programmers nowadays use "GOTO" command.

If you want users to limit data entry to 5 digit numbers, you should use the following two functions in VB (or VBA):

1. Set your text box length to 5;
2. Use "IsNumeric()" command.

Alex
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor