pass paramter to function

S

Souris

I would like to have a function to take care of the controls from calling
function. I am not sure what kind variable I have to pass.

For example,

function ValidateMyControl( MyControl as Combobox)
Mycontrol.setfocus
If MyControl.text ="" then
MsgBox("Please enter the value", vbOKOnly)
end if
end function

Above function only works for combobox, I need have one function for every
controls.

Are there any ways to have one function to take care all the controls?
Or I have to have one function per type controls?

Your information is great appreciated,
 
M

Marshall Barton

Souris said:
I would like to have a function to take care of the controls from calling
function. I am not sure what kind variable I have to pass.

For example,

function ValidateMyControl( MyControl as Combobox)
Mycontrol.setfocus
If MyControl.text ="" then
MsgBox("Please enter the value", vbOKOnly)
end if
end function

Above function only works for combobox, I need have one function for every
controls.

Are there any ways to have one function to take care all the controls?
Or I have to have one function per type controls?


You can declare the argument As Control, but then you have
to deal with controls that don't have a property used in the
function. E,g, the Text property only applies to text and
combo boxes (and it is almost always the wrong property
anyway).
 
K

Klatuu

In VBA, the Text property is only valid when the control has the focus. It
is different in VB where the Text property is more like the Value property in
VBA. And, as you stated, almost never necessary with one exception. If you
need to know the current value of a control while the control has the focus,
you need to use the Text property. The Value property is not populated until
the focus leaves the control.
 
S

Souris

Thanks for the message,

I pass the control name like cmbMyControl to the function, but the function
receive the value or text of the control which is not the control.

Can you please help me pass the control to the function?

Thanks again,
 
K

Klatuu

To pass a control, put the reference to the control in the function:

RetVal = MyFunction(Me.txtSSN)

Then in the Function dim it as a control

MyFunction(ctl As Control)

Another way is to pass the name of the control and use it in referencing the
form's controls collection
RetVal = MyFunction(Me.txtSSN.Name)

MyFunction(strCtl As String)

If Me.Controls(strCtl).Tag = "X" Then
 
S

Souris

I use first method, but I got object required.
I move the mouse on the control name I passed, but I got the value of the
control.
 
S

Souris

This is my code

Call ValidateActivityComponents(Me.cmbEmployeeName, "Employee")

Private Sub ValidateActivityComponents(MyComponent As Control, MyText As
String)
If (MyComponent Is ComboBox) Or (MyComponent Is Text) Then
MyComponent.SetFocus
If MyComponent.Text = "" Then
Response = MsgBox(MyText & " has not entered, Please enter " &
MyText & " to continue ", _
vbOKOnly )
Exit Sub
End If
ElseIf MyComponent Is CalendarOCX.Calendar Then
If DateDiff("d", CalendarOCX.Calendar.Value, Now) > 0 Then
Response = MsgBox("The date must not be before today date, Please
enter " & MyText & " to continue ", _
vbOKOnly)
Exit Sub
End If
End If
End Sub


I got "object required" message,
When the procedure was called,
 
S

Souris

If (MyComponent Is ComboBox) Or (MyComponent Is Text) Then

I got object required from above code,
By the way, the verison of VBA is version 6.3.
 
S

Souris

This is my code

Call ValidateActivityComponents(Me.cmbEmployeeName, "Employee")

Private Sub ValidateActivityComponents(MyComponent As Control, MyText As
String)
If (MyComponent Is ComboBox) Or (MyComponent Is Text) Then
MyComponent.SetFocus
If MyComponent.Text = "" Then
Response = MsgBox(MyText & " has not entered, Please enter " &
MyText & " to continue ", _
vbOKOnly )
Exit Sub
End If
ElseIf MyComponent Is CalendarOCX.Calendar Then
If DateDiff("d", CalendarOCX.Calendar.Value, Now) > 0 Then
Response = MsgBox("The date must not be before today date, Please
enter " & MyText & " to continue ", _
vbOKOnly)
Exit Sub
End If
End If
End Sub


I got "object required" message,
When the procedure was called,
 
K

Klatuu

There are a couple of problems.

This is incorrect, unless you have your own constants or variables to
identify control types:

MyComponent Is ComboBox
Shoud be:

If MyComponent.Control type = acComboBox then

However, I think the problem is here:

ElseIf MyComponent Is CalendarOCX.Calendar Then

You can't directly reference a control like that. Actually it would be

ElseIf MyComponent.ControlType = acCustomControl Then
 
S

Souris

Thanks millions,
Merry Christmas,




Klatuu said:
There are a couple of problems.

This is incorrect, unless you have your own constants or variables to
identify control types:

MyComponent Is ComboBox
Shoud be:

If MyComponent.Control type = acComboBox then

However, I think the problem is here:

ElseIf MyComponent Is CalendarOCX.Calendar Then

You can't directly reference a control like that. Actually it would be

ElseIf MyComponent.ControlType = acCustomControl Then
 
S

Souris

Thanks for the message,

it works now.

I tried to access and validate the date value of my CalendarOCX.Calendar
control using following code.

If DateDiff("d", MyComponent.Value, Now) > 0 Then

I got "The setting you entered isn't valid for this property"

It seems that activeX control does not let me access value property.

Can you please help me to access the activeX control value property?

Thanks again
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top