Form Controls

usermj

Registered User.
Local time
Today, 14:25
Joined
Dec 12, 2004
Messages
39
Hi guys,

please give me some directions.

Recently I designed a form with several controls including both TextBox and Combox. Before I submit data by clicking the button, I use VBA to validate the values of those controls (textbox and combox). If the value is not valid, the system will eject a msgbox and automatically set the focus on that component. In order to tidy the code, I use a GoTo statement, which refers to a group of code to display the message due to the value of parameters

But it seems VBA doesn't recongize the control I set through the parameter.

The code as the following:

private function validation () as boolean

Dim ErrorMessage as string
Dim ErrorComponent as String

......
ErrorMessage="Please select the shop Name"
ErrorComponent="ShopName" // ShopNameis a combox

GoTo ExitFunction
.....

......
ErrorMessage="Please set the start date"
ErrorComponent="StartDate" // StartDate is a Textbox
GoTo ExitFunction
.....



ExitFunction:
msgResult = MsgBox(ErrorMessage, vbOKOnly, "Error Message")
Me(ErrorComponent).SetFocus
validation = False
Exit Function


=============

As I tested for seveal time, I'm sure there is something wrong with
"
msgResult = MsgBox(ErrorMessage, vbOKOnly, "Error Message")
Me(ErrorComponent).SetFocus
"

but how can I fix it?

Many Thanks
 
what does it do? how are you calling the function?

Peter
 
Alternative code for validation

Hello usermj
While designing the form if a value is compulsory you can assign the error message as a tag property for the control .Then try the following code

Dim ctrl As Control


For Each ctrl In Me.Controls
If IsNull(ctrl) And Not IsNull(ctrl.tag) Then
ctrl.SetFocus
MsgBox Screen.ActiveControl.Tag
Exit Sub
End If

Next
Hope this helps
vengsiva
 
Last edited:
Why are you going to the Exit Function? The Exit Function words are reserved words to exit a function. See below for a normal Function structure.

Function FunctionName ()
End Function

If you need to exit a function you simply use the reserved words Exit Function (See below):
Function FunctionName()
Your Code goes here
Exit Function
Some More Code Here if you like
Exit Function
End Function
 
usermj;

Could you provide more information about what does happen and what you expected to happen? For example, any error messages that come up. This might help in diagnosing your problem.

usermj said:
Before I submit data by clicking the button, I use VBA to validate the values of those controls
Have you considered providing feedback as soon as the cursor leaves a control? Currently it appears that the user could be suddenly flooded with a series of error messages, which is not a good way to provide feedback. If you carry out a check as the cursor leaves each control that you need to check you can provide feedback immediately and the user will eventually become more familiar with what is required for each field by associating the different error messages with each control more easily. Alternatively, have you considered using the build in validation? I know that it can be a pig to use but will at least provide automatic checking and potentially save you some coding?

If you wish to stick to checking all of the controls at the end, have you considered using an 'error message' text box and feeding the error messages into it? i.e. by adding to the text box contents. If I was using going to provide checking at the end of data entry I would also change the colour of the lable text for each control, to highlight where the errors are (don't forget to reset on a new record).

I'm not sure if the omitted code would show this, but do you have lables to go to each section of code for each control and a selection routine which determines which portion of code to run? Currently it looks like your code will get to the first check then exit, so it won't check any of the other controls.

HTH

Tim
 

Users who are viewing this thread

Back
Top Bottom