Solved

Validate Currency Fields

Posted on 2002-05-14
12
877 Views
Last Modified: 2010-08-05
I'm using VB6 against an sql server 7 database.
I need help in validating four text fields for dollar amounts.
The fields will be used to retrieve money amounts from and update money amounts to several database tables.

What is the best way and most efficient way to test for valid currency amounts entered.

Should I accept all input and then format it later or is there a best validation for currency entered?

Thanks in Advance
John
0
Comment
Question by:jtrapat1
  • 4
  • 2
  • 2
  • +3
12 Comments
 
LVL 4

Expert Comment

by:RichW
ID: 7009315
Do you mean something other than If IsNumeric(Text1) Then?

0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 7009436
what determines if a numeric entry is "valid" - other than the fact that it is numeric?

If you want to GUARANTEE that only Numbers and possibly a decimal point (only one though), is accepted, that can be doen in the KeyPress event handler of each text box for which you want to 'validate' the currency entry.

Arthur Wood
0
 
LVL 4

Expert Comment

by:trkcorp
ID: 7009494
You could also resort to a masked edit control, instead of a text box, setting the format property to allow so many decimal places, etc...
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

Author Comment

by:jtrapat1
ID: 7009595
trkcorp,

that maskededit control looks really good
and I think I'd like to use that one.

Let me ask you another question.
Which event is the best one to test for a user entering invalid data?
I got so confused with all of the events:
Lost_Focus, Validate, KeyPress, etc.

Thanks in Advance,
John  
0
 
LVL 4

Expert Comment

by:RichW
ID: 7009599

The IsNumeric validation would take care of all that.  It would handle the decimals and currency symbols as well.

A single decimal validates as numeric, and so does the $.

You could say this when validating the text boxes.

If Not IsNumeric(Text1) Then
    MsgBox "Please enter a numeric"
    Text1.SetFocus
End If

0
 

Author Comment

by:jtrapat1
ID: 7009646
trkcorp,
I'm trying to use the maskededit control and when I use this piece of code:
If Not IsNumeric(Text1) Then
   MsgBox "Please enter a numeric"
   Text1.SetFocus
End If

I can't seem to set the focus back to the text field.
It shows up in the list of properties in the drop down.
Am I doing something wrong or is something set to disabled by default?

Thanks
John
0
 
LVL 1

Expert Comment

by:carpediem
ID: 7009654
I'd have to agree with trkcorp.  Use the masked edit.  And to answer your later question.  You don't need to test for valid data with the masked edit, because if you setup the mask this is handled automatically by the control without you having to add any code.
0
 
LVL 4

Expert Comment

by:RichW
ID: 7009674
So you would rather add dependency information to the application for the ocx file than do a simple validation on a standard VB text file?

0
 
LVL 1

Expert Comment

by:carpediem
ID: 7009698
I would yes.  Its all your opinion and preferance of course.  He could also not include any dependencies (not even for the text box) and use Win API calls to create the entire GUI to lesson what needs to be distributed.  I distribute my apps on CDs or over a LAN so an extra 100KB ocx doesn't bother me.  If it needs to be downloaded by a slow internet connection, I guess that the ocx may not be the best use of bandwidth.   Either way, I think both are perfectly fine solutions.
0
 
LVL 4

Accepted Solution

by:
trkcorp earned 100 total points
ID: 7009742
<<Let me ask you another question.
Which event is the best one to test for a user entering invalid data?
I got so confused with all of the events:
Lost_Focus, Validate, KeyPress, etc.>>
As carpediem said, the control itself will do much of the validation for you, but if you wish to check the values for a range or something then you can use lost_focus or validate.  The difference is that if in validate you set cancel=true, you cannot escape the control; ie, there is no need for a setfocus to keep the user there.  If you use the lost_focus, you check the value and if you don't like it, issue a message and setfocus yourself.  lost_focus may be preferable under some circumstances and validate under others, it depends ion the interface.
<<I'm trying to use the maskededit control and when I use this piece of code:
If Not IsNumeric(Text1) Then
  MsgBox "Please enter a numeric"
  Text1.SetFocus
End If

I can't seem to set the focus back to the text field.
It shows up in the list of properties in the drop down.
Am I doing something wrong or is something set to disabled by default?>>
Is Text1 your control?  Why would you rename a masked edit box "Text1"?  The default name would be "MaskEdBox1".  (Convention is to rename it to medSomething) Are you sure you are addressing the correct control?
0
 
LVL 4

Expert Comment

by:RichW
ID: 7009911
jtrapat1,

Text1 is the default name that VB gives to a Texbox control when you add it to your form.

You can place my code inside a sub routine and call it before you try to update the user input.

For example:

Private Sub ValidateTxtBoxes()
Dim txt As Object
For Each txt In Form1
  If TypeOf txt is TextBox Then
      If txt.Name = "NameOfTextBoxWithCurrency" Then
           If Not IsNumeric(txt) Then
              MsgBox "Please enter a valid number in " & txt.Name"
              txt.SetFocus
              Exit Sub
           End If
      End If
  End If
Next txt
End Sub

You will call the ValidateTxtBoxes right before you post the values that the user entered:

Call ValidateTxtBoxes

A word about the MaskedEdit control.  I agree with all that was said about the validations of the control, etc.  My problem is that the asker asked for the easiest way to validate text boxes for currency entries from users.

If a method can be done in the normal realm of using VB code without having to use an extra ocx file, I prefer to do it this way.

The reason I prefer to do it this way is because it makes less of a hassle for the users to make sure that they install the application with all of the dependency files being loaded.

carpediem, your answer is a valid one, however, I believe that since the default VB textbox doesn't need extra verification when creating the install package as an ocx file does, (meaning you have to check to make sure that the ocx file is found and in the package) the best way to do this is to validate with regular VB code.

Again, as you said, it is up to one's own preference.  I too would use the MaskedEdit control if I had to make use of more of its functions than just the validations, and it would really simplify the task, rather than writing a lot of VB code.

however, in this case, I believe that the most simple way to validate would be to use VB code on the TextBox control.

Cheers,

RichW


0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7009954
RichW,

One problem you are going to encounter with the IsNumeric approach is that it will accept value such as 123e300 or 123d-300 (or 123d-999999999 for that matter).  While these may be valid numeric values they will more than likely overflow most currency fields.

Anthony
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question