|
|||
|
Hi guys,
I wrote this standard module, NumericInputErrorCheck, used to validate thatthe input from a text box is a numeric integer between the values of MinVal & MaxVal: Option Explicit Dim Message As Integer, N As Integer Public Function NumericInputErrorCheck(TextInput As TextBox, MinVal As Integer, MaxVal As Integer) As Boolean NumericInputErrorCheck = True For N = 1 To Len(TextInput) If Asc(Mid(TextInput, N, 1)) < 48 Or Asc(Mid(TextInput, N, 1)) > 57Then NumericInputErrorCheck = False: Exit For Next N If TextInput = "" Or Val(TextInput) < MinVal Or Val(TextInput) > MaxVal Or Val(TextInput) <> Int(Val(TextInput)) Or NumericInputErrorCheck = False Then Message = MsgBox("Please enter an integer between " & MinVal & " and " & MaxVal & ".", vbExclamation, "Error") NumericInputErrorCheck = False TextInput.SetFocus TextInput.Text = "" End If End Function Which seems to work a treat when I call from a textbox subroutine: Private Sub txtInput_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = vbKeyReturn Then If NumericInputErrorCheck(txtInput, 1, 10) Then Print "Correct!" End Sub BUT I have a few queries... 1) Do you think this is the best was of error checking for an integer value? 2) Why does my system beep when I hit return? Any way of stopping it? 3) In the standard module is it necessary to declare Option Explicit? 4) In the standard module should the variables Message & N be declared within the function itself? 5) Why does 'If KeyCode = vbKeyReturn Then If NumericInputErrorCheck....'work but 'If KeyCode = vbKeyReturn and NumericInputErrorCheck....' callsthe function any time a key is pressed? I thought the whole point of AND was that both arguments had to be valid. Your feedback would be much appreciated guys ![]() |
|
|
||||
|
||||
|
|
|
|||
|
"xyzzy" <xyzzy1974@gmail.com> wrote in message news:653bdff7-41f4-4ade-aa97-413f25c5e32c@googlegroups.com... >Hi guys, <snip> >BUT I have a few queries... >1) Do you think this is the best was of error checking for an integer >value? >2) Why does my system beep when I hit return? Any way of stopping it? >3) In the standard module is it necessary to declare Option Explicit? >4) In the standard module should the variables Message & N be declared >within the function itself? >5) Why does 'If KeyCode = vbKeyReturn Then If NumericInputErrorCheck....' >work but 'If KeyCode = vbKeyReturn and >NumericInputErrorCheck....' calls >the function any time a key is pressed? I thought the whole point of AND >was that both arguments >had to be valid. >Your feedback would be much appreciated guys ![]() You might want to look at the IsNumeric function, it'll save you a bit of effort. You can suppress the beep by adding: If KeyAscii = vbKeyReturn Then KeyAscii = 0 to the KeyPress event ALWAYS use Option Explicit, without it typing errors can go undetected Scope: If a variable is only used in one routine then it should be declared in that routine, if a variable spans multiple routines then as a module variable. AND does work that way, all parameters have to be evaluated - even if the first one fails any subsequent ones will still be worked out. DaveO |
|
|||
|
On Thursday, August 9, 2012 10:10:01 AM UTC+1, DaveO wrote:
<snip> > You might want to look at the IsNumeric function, it'll save you a bit of > effort. Right. See below. > You can suppress the beep by adding: > If KeyAscii = vbKeyReturn Then KeyAscii = 0 > to the KeyPress event That's brilliant! If KeyAscii = vbKeyReturn Then KeyAscii = 0: If NumericInputErrorCheck(txtInput, 1, 10) Then Print "Correct!" Works a treat. > ALWAYS use Option Explicit, without it typing errors can go undetected Sure. > Scope: If a variable is only used in one routine then it should be declared > in that routine, if a variable spans multiple routines then as a module > variable. OK. There is only the one function in the standard module. > AND does work that way, all parameters have to be evaluated - even if the > first one fails any subsequent ones will still be worked out. Gotcha. So Vesion 2: Option Explicit Public Function NumericInputErrorCheck(TextInput As TextBox, MinVal As Integer, MaxVal As Integer) As Boolean Dim Message As Integer If (Not IsNumeric(TextInput)) Or (Val(TextInput) < MinVal) Or (Val (TextInput) > MaxVal) Or (Val(TextInput) <> Int(Val(TextInput))) Then Message = MsgBox("Please enter an integer between " & MinVal & " and " & MaxVal & ".", vbExclamation, "Error!") TextInput.SetFocus TextInput.Text = "" Exit Function End If NumericInputErrorCheck = True End Function BTW note that I have enclosed each comparison operation within brackets so it's easier to undersand at a glance. Is this considered bad programming practice? Also, can we not declare Message as type Byte (seems to work ok) or by convention you declare it as an integer? Thanks for your help! |
|
|||
|
On 09/08/2012 11:12, xyzzy wrote:
> Also, can we not declare Message as type Byte (seems to work ok) or > by convention you declare it as an integer? It should actually be VbMsgBoxResult which is an enum (internally an integer type) > Message = MsgBox("Please enter an integer between " & MinVal & " and > " & MaxVal & ".", vbExclamation, "Error!") But as you're not asking a question, why both with the answer? > MsgBox "Please enter an integer between " & MinVal & " and " & MaxVal > & ".", vbExclamation, "Error!" Would work just as well. -- Deanna Earley (dee.earley@icode.co.uk) i-Catcher Development Team http://www.icode.co.uk/icatcher/ iCode Systems (Replies direct to my email address will be ignored. Please reply to the group.) |
|
|||
|
On Thursday, August 9, 2012 11:45:24 AM UTC+1, Deanna Earley wrote:
> It should actually be VbMsgBoxResult which is an enum (internally an > integer type) Interesting. Enumerations look useful. > But as you're not asking a question, why both with the answer? > > > MsgBox "Please enter an integer between " & MinVal & " and " & MaxVal > > & ".", vbExclamation, "Error!" > > Would work just as well. Well yes I always wondered this. I quote from my Learn VB6 manual: 'Although Visual Basic still supports the MsgBox statement, Microsoft recommends that you use only the MsgBox() function because of its inspection ability fora return value.' That was why - I wanted to do it 'properly', but, as Microsoft no longer supports VB6, what the hell ![]() |
|
|||
|
On Thursday, August 9, 2012 11:45:24 AM UTC+1, Deanna Earley wrote:
> It should actually be VbMsgBoxResult which is an enum (internally an > integer type) Interesting. Enumerations look useful. > > Message = MsgBox("Please enter an integer between " & MinVal & " and > > " & MaxVal & ".", vbExclamation, "Error!") > > But as you're not asking a question, why both with the answer? > > > MsgBox "Please enter an integer between " & MinVal & " and " & MaxVal > > & ".", vbExclamation, "Error!" > > Would work just as well. Well I always wondered this. My Learn VB manual says that although VB6 still suppoerts the MgsBox statement, Microsoft recommends using the MsgBox function for its ability to return values - but as Microsoft no longer suppoerts VB6 what the hell! |
|
|||
|
On Thursday, August 9, 2012 12:28:04 PM UTC+1, xyzzy wrote:
.. .. > Well I always wondered this... This throws up another question. My standard module code is now: Option Explicit Public Function NumericInputErrorCheck(TextInput As TextBox, MinVal As Integer, MaxVal As Integer) As Boolean If (Not IsNumeric(TextInput)) Or (Val(TextInput) < MinVal) Or (Val(TextInput) > MaxVal) Or _ (Val(TextInput) <> Int(Val(TextInput))) Then MsgBox "Please enter an integer between " & MinVal & " and " & MaxVal & ".", vbExclamation, "Error!" TextInput.SetFocus TextInput.Text = "" Exit Function End If NumericInputErrorCheck = True End Function Question: I guess there is no need now for the Option Explicit? |
|
|||
|
On 09/08/2012 12:28, xyzzy wrote:
> On Thursday, August 9, 2012 11:45:24 AM UTC+1, Deanna Earley wrote: >>> Message = MsgBox("Please enter an integer between " & MinVal & " and >>> " & MaxVal & ".", vbExclamation, "Error!") >> >> But as you're not asking a question, why both with the answer? >> >>> MsgBox "Please enter an integer between " & MinVal & " and " & MaxVal >>> & ".", vbExclamation, "Error!" >> >> Would work just as well. > > Well I always wondered this. My Learn VB manual says that although > VB6 still suppoerts the MgsBox statement, Microsoft recommends using > the MsgBox function for its ability to return values - but as > Microsoft no longer suppoerts VB6 what the hell! It's still a function, you're just ignoring the return value. The Manual doesn't list it as a statement: http://msdn.microsoft.com/en-us/libr...(v=vs.60).aspx Also, why are you learning a 15 year old language and obsoleted 5 (very soon to be 6?) times IDE? While there is still a large pool of VB6 code out there, I wouldn't want to get into the maintenance market at this stage. There are many more capable and newer languages and environments to choose from. -- Deanna Earley (dee.earley@icode.co.uk) i-Catcher Development Team http://www.icode.co.uk/icatcher/ iCode Systems (Replies direct to my email address will be ignored. Please reply to the group.) |
|
|||
|
On Thursday, August 9, 2012 12:44:05 PM UTC+1, Deanna Earley wrote:
> It's still a function, you're just ignoring the return value. > The Manual doesn't list it as a statement: > http://msdn.microsoft.com/en-us/libr...(v=vs.60).aspx OK. I presumed functions always had brackets after the keyword. > Also, why are you learning a 15 year old language and obsoleted 5 (very > soon to be 6?) times IDE? > While there is still a large pool of VB6 code out there, I wouldn't want > to get into the maintenance market at this stage. Excellent question. I guess it's because I'm used to VB6 & don't like VB.Net & can't be asked to learn another dialect of Basic. VB6 lets me throw applications together quickly & I do it just for fun. > There are many more capable and newer languages and environments to > choose from. Out of interest, if I could be asked, what would you recommend? |
|
|||
|
On 09/08/2012 12:56, xyzzy wrote:
> On Thursday, August 9, 2012 12:44:05 PM UTC+1, Deanna Earley wrote: >> There are many more capable and newer languages and environments to >> choose from. > > Out of interest, if I could be asked, what would you recommend? It's difficult to say, as I'm not a newbie (no offence , I usewhatever is suitable for what I'm doing ![]() I have 12 years professional VB6 experience and I can't remember how many before that. Most of my time is spent on the 10 years of legacy VB6 code with most (not all) new code being in C# 2.0. My colleague primarily uses C++ as most of it communicates with hardware, Direct3D, etc. If I was starting from scratch, I'd probably try C# 4.0/WPF "as it's new" and I can get an IDE for free (#develop and VC# express) YMMV though. If you have the experience with VB6 (I assumed you didn't from the "newbie to VB6") then you can stick with it. -- Deanna Earley (dee.earley@icode.co.uk) i-Catcher Development Team http://www.icode.co.uk/icatcher/ iCode Systems (Replies direct to my email address will be ignored. Please reply to the group.) |
|
|||
|
On Thursday, August 9, 2012 1:43:47 PM UTC+1, Deanna Earley wrote:
> YMMV though. If you have the experience with VB6 (I assumed you didn't > from the "newbie to VB6") then you can stick with it. Well I have some limited experience, but now, as I have some free time on my hands, I thought I'd get down & learn it properly. I'll probably look around at other dialects of Basic after that. C++ & C# I am not a fan of. Thanks for your feedback ![]() |
|
|||
|
| > YMMV though. If you have the experience with VB6 (I assumed you didn't | > from the "newbie to VB6") then you can stick with it. | | Well I have some limited experience, but now, as I have some free time on my hands, I thought I'd get down & learn it properly. I'll probably look around at other dialects of Basic after that. C++ & C# I am not a fan of. Thanks for your feedback ![]() These days, any discussion of programming languages has to take Microsoft's plans into account. Except for being limited to 32-bit, VB is arguably the best supported tool on all Windows versions. (Even C++, if used in VS, will almost always require a sizable runtime installation on many machines -- unless one uses an older version.) On the other hand, if Microsoft is successful with the Metro-everywhere strategy, writing software on Windows may not have much of a future. One can use .Net, C++, or even script on Metro, but the only option is to write phone/tablet trinkets, approved by Microsoft and sold through their store. (With them getting a 30% cut.) It's not hard to imagine a scenario in the near future where actual computer software programs may only be available on Linux or by pseudo-Web subscription. (I say pseudo-Web because the cloud craze is mainly a money- making scheme. The actual logistics are secondary. One could have a massive program installed locally, but if a webpage login is required to use that program then it will be known as a cloud subscription.) Adobe is already selling Photoshop in a 3-month subscription version. MS is trying to make MS Office into a Web business. Etc.) |
|
|||
|
Something Like The Code Below May Be A Bit Wiser if you really need to check
the value before user can do anything else :0) Ivar Option Explicit Public Function NumericInputErrorCheck(StrVar, MinVal As Integer, MaxVal As Integer) As Boolean Dim TestInt As Integer TestInt = CInt(Val(StrVar)) Select Case True Case TestInt <> StrVar, TestInt < MinVal, TestInt > MaxVal, Not IsNumeric(StrVar) Case Else NumericInputErrorCheck = True End Select End Function Private Sub txtInput_KeyPress(KeyAscii As Integer) Dim BoolVar As Boolean If KeyAscii = vbKeyReturn Then KeyAscii = 0 txtInput_Validate BoolVar If Not BoolVar Then 'NextControl.SetFocus End If End If End Sub Private Sub txtInput_Validate(Cancel As Boolean) If Not NumericInputErrorCheck(txtInput.Text, 1, 10) Then Cancel = True MsgBox "Please Enter A whole Number Between 1 and 10.", vbExclamation, "Invalid Input" txtInput.SelStart = 0 txtInput.SelLength = Len(txtInput.Text) End If End Sub |
|
|||
|
"xyzzy" <xyzzy1974@gmail.com> wrote in message news:44317642-083d-48c3-8e57-962384261432@googlegroups.com... > On Thursday, August 9, 2012 12:28:04 PM UTC+1, xyzzy wrote: > If (Not IsNumeric(TextInput)) Or (Val(TextInput) < MinVal) Or > (Val(TextInput) > MaxVal) Or _ Except in certain cases such as Label = "Hello World" I would avoid relying on default properrties so the above should really be: If (Not IsNumeric(TextInput.Text)) Or (Val(TextInput.Text) < MinVal) Or (Val(TextInput.Text) > MaxVal) Or _ You asked earlier about brackets, if they make to expression clearer and avoid ambiguous evaluation then yes, the more the merrier. There is no problem with the brackets in the above line. >Question: I guess there is no need now for the Option Explicit? Wrong, wrong and a thousand times wrong! It may well be OK now but if you return and add something else later and were to type "MinVa1" instead of "MinVal" (Number 1 instead of lower case L) without Option Explicit you could search for the weird error for weeks, with Option Explicit it just won't run and will highlight the typo. I'd strongly suggest going to the Tools/Options menu and ticking "Require Variable Declaration". This will automatically add "Option Explicit" to all new forms and modules when they are first created. DaveO |
|
|||
|
"Ivar" <ivar.ekstromer000@ntlworld.com> wrote in message news:FWPUr.1066144$3s1.921011@fx12.am4... > Select Case True This is not going to end well! I think I'll sit back and wait for the inevitable opprobrium :-) DaveO. |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|