Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Apply Sub Procedure to Multiple Controls

Status
Not open for further replies.

MasterLu

MIS
Jun 11, 2003
16
0
0
US
Hi

I have a question regarding how to apply the same sub procedure such as the following example to multiple controls on the same form. I want to apply this procedure to Copy2, Copy3, Copy4... Copy10. Is there a way to pass the Copy# as a variable? So for each Copy#, before updating I want it to run the same procedure below. Is this possible to do without separately coding an individual BeforeUpdate procedure for every single Copy#?

Any advice is greatly appreciated!!!
-----------------------------------
Private Sub Copy1_BeforeUpdate(Cancel As Integer)

If Me!Copy1 Like "*[[]LINK]*" Then
If Len(Me!Copy1) < 1020 Then
Exit Sub
Else: MsgBox "You are limited to 1024 characters in the copy of each block. You currently have " & Len(Me!Copy1) & ". Please remove any excess characters.", vbExclamation, "Character Limit"
Me.Undo
End If

Else: Response = MsgBox("You must type [LINK] where Link Text should appear." & Chr(13) & "Do you want to add a link?", vbYesNo, "[LINK] Error")
If Response = vbYes Then
Me.Undo
End If
End If

End Sub
---------------------------------
-Lu
 
Change your procedure to a function, using the control as an argument (control data type).

Then from the Properties dialog box, of each control, put the name of your function(including the argument), on the BeforeUpdate Event of each control.

Public Function LengthCheck(ctlCopy As Control)

If ctlCopy.Value Like "*[[]LINK]*" Then
If Len(ctlCopy.Value) < 1020 Then
Exit Sub
Else: MsgBox "You are limited to 1024 characters in the copy of each block. You currently have " & Len(ctlCopy.Value) & ". Please remove any excess characters.", vbExclamation, "Character Limit"
Me.Undo
End If

Else: Response = MsgBox("You must type [LINK] where Link Text should appear." & Chr(13) & "Do you want to add a link?", vbYesNo, "[LINK] Error")
If Response = vbYes Then
Me.Undo
End If
End If

End Function

On the BeforeUpdate Event (Right in properties dialog box)

=LengthCheck([Copy1])

=LengthCheck([Copy2])
...
 
Wow!

Thanks Zion7 for the quick response. I will try that out and let you know how it works for me.

Thanks!
-Lu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top