to make a fucnation return mutliple values

hi Experts,
              i have got a problem. iam having forms which has 70 controls like txtbx,combo,etc..while saving data i have to write validation code for all the controls which is tiresome..

so i decided to   a function   in which i loop through each and every control and if i found empty i will genrate a msg that a box is empty.. the function i working
eg:
public function empty(frm as form)as boolean
dim ctrl as control
for each ctrl in frm
 if type of ctrl is text box and  ctrl.text="" then
  msgbox" txt box empty"
  empty=true  
   exit function
 end if
next


but the problem is iam having so much controls i want the focus to set the control to help the end user
 after genrating the msg. so i want to return control name or some method by which i can set focus to it



 so please help me out. iam counting alot  on your expertness than you you do on yours


regards
vairavamuthu

LVL 1
vairavamuthuAsked:
Who is Participating?
 
x_terminat_or_3Connect With a Mentor Commented:
After the comment "I am trying this out and will say the result as soon as possible" we haven't heard a thing, so I *guess* my reply worked....
0
 
jimbobmcgeeCommented:
This would work if there was only one field that did not validate -- focus could be passed to it and filled in.  However, if more than one field was empty, there might be an issue as focus is moved to each field, by the code, before the user can do anything about it.

Perhaps you could try (pseudo code follows):

select case type of ctrl

   case text box
            If ctrl.text = "" then
               ctrl.text = InputBox("Please enter a value for the " & ctrl.name & " field:")
            End if

   case picture box
            If ctrl.picture = Nothing/Null/Empty/?
               ctrl.picture = LoadPicture(CommonDialog1)
            End if

   case else
            'etc

end select

Regards,

Jamie
0
 
mladenoviczCommented:
You can set focus with setFocus method

Private Function SafeSetFocus(Ctl As Object)
    On Error Resume Next
    Ctl.SetFocus
End Function
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
x_terminat_or_3Commented:
NO problem!


First of all, let me suggest a few changes.

If you have many textboxes, then stop checking on the first one you find empty, that way you won't present the user with 50 messageboxes at a time, so I will implement this in the code example.

public function empty(frm as form,optional byref ctrProblem as Control)as boolean

dim ctrl as control

for each ctrl in frm
 if type of ctrl is text box and  ctrl.text="" then
    msgbox" txt box empty"
    empty=true  
    ctrProblem=ctrl
    ctrProblem.SetFocus
    exit function
 end if
next

I've added the ctrProblem variable as optional.  Notice the byref keyword.  It means that the variable will be passed to the function by reference and not by it's value, it means that you can change the variable there and when the function exits, it will update the variable in the called procedure.

Example of calling procedure

Private sub Button1_Click
 Dim blnResult As Boolean, ctrEmpty As Control

  blnResult=empty(form1,ctrEmpty)

  Msgbox("I found an empty textbox named " + ctrEmpty.name)

End If

With kind regards


Ramses (will follow the c0de - Assembly, the language of the Gods!)
0
 
mladenoviczCommented:
When you find empty field you should exit for to prevent multiple setFocus calls
0
 
x_terminat_or_3Commented:
Update

Example of calling procedure

Private sub Button1_Click
 Dim blnResult As Boolean, ctrEmpty As Control

  blnResult=empty(form1,ctrEmpty)

  If not blnResult then Msgbox("I found an empty textbox named " + ctrEmpty.name)

End If

With kind regards


Ramses (will follow the c0de - Assembly, the language of the Gods!)
0
 
fulscherCommented:
vairavamuthu,

just a thought: When executing your validation, you could set the background color of the empty fields to something else (yellow) to indicate the fields which have to be filled in. Also, you can use SetFocus to navigate to the first field.

Something like this:

public function empty(frm as form) as boolean
dim ctrl as control
dim OneFound as Boolean

for each ctrl in frm
   if type of ctrl is text box and  ctrl.text="" then
      ctrl.BackColor = RGB(255, 255, 180)
      empty=true  
      if not OneFound Then
         ctrl.SetFocus
         OneFound = True
      End if
   end if
next

HTH, Jan
0
 
ChenChenCommented:

Control array might help.
To do it, drop a textbox on the form, and change its index value to 0, and then you can just copy & paste the textbox to as many as you need. after that you can just easiely loop them through:

for i = 0 to text1.ubourd
if text(i)="" then msgbox "got it!"
next

do the same thing to your combo box

hope it helps.
0
 
vairavamuthuAuthor Commented:
hi Experts team,

 iam working on it and will let  u know immediately

regards
vairavamuthu
0
 
Programmer_JeffCommented:
vairavamuthu,

     Read your question.  A function by definition can only provide a single answer.  So with that in mind, try writing your function to verify each text box as the data is entered.  Make the focus stay with that text box until validated.  Just call the validation function (written once) at the end of each text box code.

Good luck.
0
 
x_terminat_or_3Commented:
I don't agree Programmer_Jefff

A function can provide as much answers as you want, provided you pass some variables by reference


With kind regards


Ramses (x_terminat_or_3)
0
 
vairavamuthuAuthor Commented:
Hi Team,
  i tried the function ..iam able to get the  text box name  as a string but not able to focus my control to  the text box using the textbox name.. pls help me out.. i know we are near

expecting a solution
yours
vairavamuthu
0
 
x_terminat_or_3Commented:
You can try to change my example a bit, like this:

public function empty(frm as form,optional byref ctrProblem as Control)as boolean

dim ctrl as control

for each ctrl in frm
 if type of ctrl is text box and  ctrl.text="" then
    'found an empty textbox
    empty=true  
    ctrProblem=ctrl
    exit function
 end if
next

And you call the function with


Private sub Button1_Click
 Dim blnResult As Boolean, ctrEmpty As Control

  blnResult=empty(form1,ctrEmpty)

  If blnResult = False Then
      Msgbox("I found an empty textbox named " + ctrEmpty.name)
      On Error Resume NExt
      ctrEmpty.SetFocus
  End If
End Sub


With kind regards


Ramses (will follow the c0de - Assembly, the language of the Gods!)

0
 
x_terminat_or_3Commented:
Please note that I post only 'tested' code and that the example provided above should work.  Use the built-in debugger to follow the code to see what happens.


With kind regards


Ramses (will follow the c0de - Assembly, the language of the Gods!)
0
 
vairavamuthuAuthor Commented:
Hi team,
         Iam trying out this and will say the result as soon as possible

regards
vairavamuthu
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.

All Courses

From novice to tech pro — start learning today.