Solved

to make a fucnation return mutliple values

Posted on 2004-09-20
16
218 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
  • 6
  • 3
  • 2
  • +4
16 Comments
 
LVL 16

Expert Comment

by:jimbobmcgee
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 8

Expert Comment

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

Expert Comment

by:x_terminat_or_3
Comment Utility
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
Comment Utility
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
Comment Utility

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Author Comment

by:vairavamuthu
Comment Utility
hi Experts team,

 iam working on it and will let  u know immediately

regards
vairavamuthu
0
 
LVL 1

Expert Comment

by:Programmer_Jeff
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 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

11 Experts available now in Live!

Get 1:1 Help Now