Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 922
  • Last Modified:

Validate Currency Fields

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
jtrapat1
Asked:
jtrapat1
  • 4
  • 2
  • 2
  • +3
1 Solution
 
RichWCommented:
Do you mean something other than If IsNumeric(Text1) Then?

0
 
Arthur_WoodCommented:
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
 
trkcorpCommented:
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
jtrapat1Author Commented:
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
 
RichWCommented:

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
 
jtrapat1Author Commented:
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
 
carpediemCommented:
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
 
RichWCommented:
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
 
carpediemCommented:
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
 
trkcorpCommented:
<<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
 
RichWCommented:
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
 
Anthony PerkinsCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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.

  • 4
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now