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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem referencing me.controls in function

Status
Not open for further replies.

scoobey

Technical User
Sep 18, 2001
32
GB
Hi!

I was given some help on a previous thread to cycle through some controls on a form to enable/disable them, which worked great. However, I now need to move this from the form procedure level to part of a function, but seem to be running into diffuculties. The code is below:

Dim ctl As Control
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acComboBox, acListBox, acTextBox, acCheckBox
If (ctl.Name <> "txtTst") Then
ctl.Enabled = Not ctl.Enabled
ctl.Locked = Not ctl.Locked
'toggling the property
End If
End Select
Next ctl

Basically, I don't think it likes the 'Me' part as it can't reference it. When I call the function from the form, I already send the form name by using: var_ReturnValue = bas_Edit(Me). I have tried using this in the function and replacing Me.Controls with FrmName.Controls but this doesn't work either.

Any ideas? I'm sure it's really simple!?
 
Is your function defined like this?

Function bas_Edit(FrmName as form)
...
End Function

If so, then post your function here.
 
Pass the form

[tt]public function somefunction(frm as form) as <somedatatype>
' do stuff
for each ctl in frm.controls
end function[/tt]

[tt]myvalue = somefunction(me)[/tt]

If you're not returning av value, consider making it a sub, to reduce the overhead

Roy-Vidar
 
OK, this is my function (I have taken out the rest of the function to make it easier to read):

Public Function EditValidation(FrmName As Form)

MsgBox "beginning of loop"
Dim ctl As Control
For Each ctl In FrmName.Controls
MsgBox ctl
Select Case ctl.ControlType
Case acComboBox, acListBox, acTextBox, acCheckBox
If (ctl.Name <> "txtTst") Then
ctl.Enabled = Not ctl.Enabled
ctl.Locked = Not ctl.Locked
'toggling the property
End If
End Select
Next ctl
MsgBox "end of loop"

End Function


I call the function using an On_Click event for the button:

var_ReturnVal = EditValidation(Me)

But it just doesn't work, even trying your suggesstions. For testing, the first message box is displayed but it never actually gets to the last message box. Help!???

 
Thanks guys - managed to fix it in the end. The msgbox ctl was causing the loop to fail (not too sure how that got in there). All sorted now! Cheers!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top