Link to home
Start Free TrialLog in
Avatar of vairavamuthu
vairavamuthu

asked on

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

Avatar of jimbobmcgee
jimbobmcgee
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of mladenovicz
mladenovicz

You can set focus with setFocus method

Private Function SafeSetFocus(Ctl As Object)
    On Error Resume Next
    Ctl.SetFocus
End Function
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!)
When you find empty field you should exit for to prevent multiple setFocus calls
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!)
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

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.
Avatar of vairavamuthu

ASKER

hi Experts team,

 iam working on it and will let  u know immediately

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

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!)
Hi team,
         Iam trying out this and will say the result as soon as possible

regards
vairavamuthu
ASKER CERTIFIED SOLUTION
Avatar of x_terminat_or_3
x_terminat_or_3

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial