Solved

Validate Currency Fields

Posted on 2002-05-14
12
869 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now