?
Solved

VB 6.0  Only one subroutine to be called to validade multiple TextBoxes

Posted on 2003-03-20
6
Medium Priority
?
272 Views
Last Modified: 2010-05-18
To validiate a TextBox ("txt_TextBox") I wrote a code for each text box in my programme, to make sure the user enters a numeric value.

I use this code for more than 20 text boxes on different forms ("Form_A" , "Form_A",....)

The coding is:
______________________________________________________
Private Sub txt_TextBox_Validate(keepfocus As Boolean)
    If Not IsNumeric(txt_TextBox) Then
        txt_TextBox.BackColor = &HC0C0FF
        If txt_TextBox <> "" Then
            keepfocus = True
            MsgBox ("please use a numerical value")
        End If
    Else
        txt_TextBox.BackColor = &HFFFFFF
    End If
End Sub
_________________________________________________________

I think this is a very cumbersome way of programming, instead of putting the full code for each text box I intended to write a seperate subroutine wich are called in every "txt_TextBox_Validate" event.

For example, instead of the above mentioned code something like this for each text box
Private Sub txt_TextBox_Validate ()
    Call module1.validiate_numeric_value (txt_TextBox, Form_A)
end Sub

And then one code for all textboxes in module1 like:
Public Sub validiate_numeric_value (txt_TextBox as TextBox, Form_name as Form)
  With Form_name
    If Not IsNumeric(.txt_TextBox) Then
        .txt_TextBox.BackColor = &HC0C0FF
        If .txt_TextBox <> "" Then
            keepfocus = True
            MsgBox ("please use a numerical value")
        End If
    Else
        .txt_TextBox.BackColor = &HFFFFFF
    End If
  end With
end Sub

______________

Unfortuatelly this does not work, I think it is related to what I put into the brackets of the subroutine and the calling line.
Can give anyone a coding which works???


0
Comment
Question by:themroc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 6

Expert Comment

by:marconovaro
ID: 8172553


Here is

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=38711&lngWId=1

a piece of code of mine that does what you need.

The trick is to have a reference to your textbox in a class, and receive all the events of the textbox there. So, you create that validation code only once. The class is then a "wrapper" for the textbox, adding a "new" functionality to it.

Have a look to that code: it will help you.

Hope this helps
M
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8172577
You can define a subroutine globally as:

Sub TextValidate ( txt As TextBox, frm As Form)
// your code

End Sub

And call it directly in all those _Change () subroutines (so that the validation occurs every time the text changes) as:

Private Sub txt_TextBox_Change
  TextValidate ( txt_TextBox, Me )

End Sub

See if that helps you!

Mayank.
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 1040 total points
ID: 8172623
Hi themroc,

Actually you don't need the reference to the form.

To understand what is needed you need to understand the difference between ByReference and ByValue. When declaring a sub or function you have the choice of declaring arguments byref (default) or ByVal.

ByRef means that the function is passed a reference to the variable/object whereas byval means that only the value of the object is passed. This is important because passing a variable byvalue and then changing that value does not affect the variable/object in the calling procedure. Passing a variable/object byref does!

So you are passing a textbox byref, this means that within your sub in the module, any time you use txt_TextBox you are actually referring directly to the object that you specified in the call to this subroutine.

Private Sub txt_TextBox_Validate ()
    Call module1.validiate_numeric_value (txt_TextBox)
end Sub

Public Sub validiate_numeric_value (txt_TextBox as TextBox)
    If Not IsNumeric(txt_TextBox) Then
        txt_TextBox.BackColor = &HC0C0FF
        If txt_TextBox <> "" Then
            keepfocus = True
            MsgBox ("please use a numerical value")
        End If
    Else
        txt_TextBox.BackColor = &HFFFFFF
    End If
  end With
end Sub

Is actually all that is required here because you are passing in the object rather than the value then you can explicitly refer to that object by reference and work with it directly.

Tim Cottee MCSD, MCDBA, CPIM
http://www.timcottee.tk 

Brainbench MVP for Visual Basic
http://www.brainbench.com

Experts-Exchange Advisory Board Member
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Expert Comment

by:rdrunner
ID: 8172807
Try to change it this way :

Public FUNCTION validiate_numeric_value (txt_TextBox as TextBox) as boolean
   If Not IsNumeric(txt_TextBox) Then
       txt_TextBox.BackColor = &HC0C0FF
       If txt_TextBox <> "" Then
           validiate_numeric_value = true
           MsgBox ("please use a numerical value")
       End If
   Else
       txt_TextBox.BackColor = &HFFFFFF
   End If
 end With
end FUNCTION


Private Sub Text1_Validate(Cancel As Boolean)
cancel = validiate_numeric_value(me.text1)
End Sub
0
 
LVL 11

Expert Comment

by:rdrunner
ID: 8172827
This will set the cancel of the validate to true and thus the focus wont leave the textbox. You dont need to set the keepfocus variable, since thats handeled with the cancel of the validate

Hope this helps
0
 

Author Comment

by:themroc
ID: 8172945
Thanks TimCottee

That was the simplest solution apparently I did not have to transfer any name of the form etc.

Only as a comment I had to keep the "(keepfocus as Boolean) in the brackets in Order to keep it running properly:
 So finaly this works:
Private Sub txt_TextBox_Validate(keepfocus As Boolean)
    Call module1.validate_numeric(keepfocus,txt_TextBox)
end Sub

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Suggested Courses
Course of the Month9 days, 4 hours left to enroll

764 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