can i create a function to save repeating lines of code?

G

Gary

i use this code: -

Me.txbdate.Locked = "true"
Me.txbtime.Locked = "true"
Me.txbcompany.Locked = "true"
Me.txblead.Locked = "true"
Me.txbtelephone.Locked = "true"
Me.txbsource.Locked = "true"
Me.txbsalesman.Locked = "true"

sometimes i want to unlock all of the above, and sometimes i want to
lock them all - to save typing it all out... can i create a function?
that will let me type something like

controls(lock) to lock them all
or
controls(unlock) to unlock them all

i've never made my own function before and wouldn't know where to
start? am i thinking the right thing here? or am i just confused?

if i'm right. please give me step by step instructions on how to create
the function, thanks,

gary.
 
M

Michael J. Strickland

Gary said:
i use this code: -

Me.txbdate.Locked = "true"
Me.txbtime.Locked = "true"
Me.txbcompany.Locked = "true"
Me.txblead.Locked = "true"
Me.txbtelephone.Locked = "true"
Me.txbsource.Locked = "true"
Me.txbsalesman.Locked = "true"

sometimes i want to unlock all of the above, and sometimes i want to
lock them all - to save typing it all out... can i create a function?
that will let me type something like

controls(lock) to lock them all
or
controls(unlock) to unlock them all

i've never made my own function before and wouldn't know where to
start? am i thinking the right thing here? or am i just confused?

if i'm right. please give me step by step instructions on how to create
the function, thanks,

gary.


Insert the following subroutine in your form:

Private Sub LockControls(blnLock As Boolean)

Me.txbdate.Locked = blnLock
Me.txbtime.Locked = blnLock
Me.txbcompany.Locked = blnLock
Me.txblead.Locked = blnLock
Me.txbtelephone.Locked = blnLock
Me.txbsource.Locked = blnLock
Me.txbsalesman.Locked = blnLock

End Sub

To lock the controls, use:

Call LockControls(True)

To unlock the controls, use:

Call LockControls(False)


--
 
G

Gary

Thank's exactly what i wanted.

i also need to change background colours of controls. i've modified
your code slightly to account for the different property type. can you
tell me if what ive done is right.

Private Sub ColourControls(strcolour As String)

Me.txbdate.BackColor = strcolour
Me.txbtime.BackColor = strcolour
Me.txbcompany.BackColor = strcolour
Me.txblead.BackColor = strcolour
Me.txbtelephone.BackColor = strcolour
Me.txbsource.BackColor = strcolour
Me.txbsalesman.BackColor = strcolour
End Sub
 
V

Van T. Dinh

Check Access VB Help topic "Writing a Sub Procedure".

Quick excerpt from the topic:

"A Sub procedure is a series of Visual Basic statements enclosed by the Sub
and End Sub statements that performs actions but doesn't return a value."
 
X

xRoachx

Hey Gary, There are a couple of ways to accomplish what you want. If these
text boxes are the only ones on the form and you want to lock them all at
once, you can do this by using a loop. If you have other textboxes on the
form, you can create an array and then loop through the array.

Loop without array (uses a command button):

Private Sub CommandButton_Click()

Dim ctl As Access.Control 'Variable for the controls

For Each ctl In Me.Controls ' Loop through each control on the form

With ctl
If .ControlType = acTextBox Then 'Only lock text boxes
.Locked = True
End If
End With
Next ctl

End Sub

With an array:

Private Sub CommandButton_Click()

Dim arrctl(1) As Access.Control

Set arrctl(0) = Text0
Set arrctl(1) = Text2

For i = 0 To UBound(arrctl())
With arrctl(i)
If .ControlType = acTextBox Then
.Locked = True
End If
End With
Next i

End Sub

A couple of points regarding the array: you need to set the size of the
array in the dim statement; you have 7 texboxes listed so this would be Dim
arrctl(6) as access.control. Also, you would need to set each array value
equal to the name of your text boxes, e.g., instead of Set arrctl(0) = Text0,
you would have Set arrctl(0) = Me.txbdate. This would have to be done for
each textbox.

To unlock the boxes, create a separate function and set the lock = false.
 
D

Douglas J Steele

The BackColor property is a Long Integer, not a String. Other than that,
you've got the right idea.
 
M

Michael J. Strickland

Gary said:
Thank's exactly what i wanted.

i also need to change background colours of controls. i've modified
your code slightly to account for the different property type. can you
tell me if what ive done is right.

Private Sub ColourControls(strcolour As String)

Me.txbdate.BackColor = strcolour
Me.txbtime.BackColor = strcolour
Me.txbcompany.BackColor = strcolour
Me.txblead.BackColor = strcolour
Me.txbtelephone.BackColor = strcolour
Me.txbsource.BackColor = strcolour
Me.txbsalesman.BackColor = strcolour
End Sub


The BackColor needs to be a long, not a string. If you check the
help, I think there are some named constants usable for some of the more
common, basic colors (e.g. vbBlue).

Try:

Private Sub ControlsBackColor(lngColor As Long)

Me.txbdate.BackColor = lngColor
Me.txbtime.BackColor = lngColor
Me.txbcompany.BackColor = lngColor
Me.txblead.BackColor = lngColor
Me.txbtelephone.BackColor = lngColor
Me.txbsource.BackColor = lngColor
Me.txbsalesman.BackColor = lngColor

End Sub


--
 
Top