Solved

to make a fucnation return mutliple values

Posted on 2004-09-20
16
224 Views
Last Modified: 2010-05-02
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

0
Comment
Question by:vairavamuthu
[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
  • 3
  • 2
  • +4
16 Comments
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12102562
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
 
LVL 8

Expert Comment

by:mladenovicz
ID: 12102629
You can set focus with setFocus method

Private Function SafeSetFocus(Ctl As Object)
    On Error Resume Next
    Ctl.SetFocus
End Function
0
 
LVL 2

Expert Comment

by:x_terminat_or_3
ID: 12102634
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 8

Expert Comment

by:mladenovicz
ID: 12102645
When you find empty field you should exit for to prevent multiple setFocus calls
0
 
LVL 2

Expert Comment

by:x_terminat_or_3
ID: 12102672
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
 
LVL 12

Expert Comment

by:fulscher
ID: 12103428
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
 
LVL 4

Expert Comment

by:ChenChen
ID: 12107429

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
 
LVL 1

Author Comment

by:vairavamuthu
ID: 12112022
hi Experts team,

 iam working on it and will let  u know immediately

regards
vairavamuthu
0
 
LVL 1

Expert Comment

by:Programmer_Jeff
ID: 12153742
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
 
LVL 2

Expert Comment

by:x_terminat_or_3
ID: 12153913
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
 
LVL 1

Author Comment

by:vairavamuthu
ID: 12159870
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
 
LVL 2

Expert Comment

by:x_terminat_or_3
ID: 12159938
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
 
LVL 2

Expert Comment

by:x_terminat_or_3
ID: 12159953
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
 
LVL 1

Author Comment

by:vairavamuthu
ID: 12187671
Hi team,
         Iam trying out this and will say the result as soon as possible

regards
vairavamuthu
0
 
LVL 2

Accepted Solution

by:
x_terminat_or_3 earned 50 total points
ID: 12559698
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

Featured Post

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.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

688 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