Welcome to SPN

Register and Join the most happening forum of Sikh community & intellectuals from around the world.

Sign Up Now!

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

Discussion in 'Information Technology' started by Gary, Oct 27, 2005.

  1. Gary

    Gary
    Expand Collapse
    Guest

    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.
     
  2. Loading...


  3. Michael J. Strickland

    Michael J. Strickland
    Expand Collapse
    Guest

    "Gary" <garyusenet@myway.com> wrote in message
    news:1130338944.456862.102550@g49g2000cwa.googlegroups.com...
    >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)


    --
    ---------------------------------------------------------------
    Michael J. Strickland
    Quality Services qualityser@att.net
    703-560-7380
    ---------------------------------------------------------------
     
  4. Gary

    Gary
    Expand Collapse
    Guest

    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
     
  5. Van T. Dinh

    Van T. Dinh
    Expand Collapse
    Guest

    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."

    --
    HTH
    Van T. Dinh
    MVP (Access)



    "Gary" <garyusenet@myway.com> wrote in message
    news:1130338944.456862.102550@g49g2000cwa.googlegroups.com...
    >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.
    >
     
  6. xRoachx

    xRoachx
    Expand Collapse
    Guest

    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.

    "Gary" wrote:

    > 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.
    >
    >
     
  7. Douglas J Steele

    Douglas J Steele
    Expand Collapse
    Guest

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

    --
    Doug Steele, Microsoft Access MVP
    http://I.Am/DougSteele
    (no e-mails, please!)


    "Gary" <garyusenet@myway.com> wrote in message
    news:1130341874.984677.124620@g43g2000cwa.googlegroups.com...
    > 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
    >
     
  8. Michael J. Strickland

    Michael J. Strickland
    Expand Collapse
    Guest

    "Gary" <garyusenet@myway.com> wrote in message
    news:1130341874.984677.124620@g43g2000cwa.googlegroups.com...
    > 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


    --
    ---------------------------------------------------------------
    Michael J. Strickland
    Quality Services qualityser@att.net
    703-560-7380
    ---------------------------------------------------------------
     

Share This Page