passing controls as parameters

  • Thread starter Thread starter jakub.denkiewicz
  • Start date Start date
J

jakub.denkiewicz

Hi i have a subform (City) within a form, and want to create a function
that selects a record on the subform using a combobox. The reason that
i created this function is that this search is used on many diffrent
subforms; therefore, i wanted to put it into a function. However, i
keep getting strange errors everytime i run this. Is it possiblet o
pass a SubForm and Combobox as parameters into a function? What am i
missing here

*****THIS DOES NOT WORK******
Function search(formBeingSearched as SubForm,comboboxBeingSearched as
Combobox)

Dim strSearch As String

strSearch = "[idString] =" & Me![comboboxBeingSearched].Column(1)

Me.formBeingSearched.Form.RecordsetClone.FindFirst (strSearch)
Me.formBeingSearched.Form.Bookmark =
Me.formBeingSearched.Form.RecordsetClone.Bookmark

End Function


Private Sub comboboxCitySearch_AfterUpdate()
Call search(subformCity, comboboxCity)
End Sub


***** THIS WORKS
Private Sub comboboxCitiesSearch_AfterUpdate()
'Find record corresponding to City Selection
Dim strSearch As String
strSearch = "[idString] =" & Me![comboboxCity].Column(1)

Me.subformCity.Form.RecordsetClone.FindFirst (strSearch)
Me.subformCity.Form.Bookmark =
Me.subformCity.Form.RecordsetClone.Bookmark
End Sub


Any help would be greatly appreciated,

Thanks.
 
Hi Jakub,
Function search(formBeingSearched as SubForm,comboboxBeingSearched as
Combobox)

Dim strSearch As String

strSearch = "[idString] =" & Me![comboboxBeingSearched].Column(1)
---------------------------------^
Here's your problem!

Type comboboxBeingSearched.Column(1)

"Me" doesn't exist once you're in a function...

Same with the rest...
Me.formBeingSearched.Form.RecordsetClone.FindFirst (strSearch)
Me.formBeingSearched.Form.Bookmark =
Me.formBeingSearched.Form.RecordsetClone.Bookmark
---^

just fromBeingSearched.Form.... should work fine

End Function

Let me know if this works!

Ben.


Hi i have a subform (City) within a form, and want to create a function
that selects a record on the subform using a combobox. The reason that
i created this function is that this search is used on many diffrent
subforms; therefore, i wanted to put it into a function. However, i
keep getting strange errors everytime i run this. Is it possiblet o
pass a SubForm and Combobox as parameters into a function? What am i
missing here

*****THIS DOES NOT WORK******
Function search(formBeingSearched as SubForm,comboboxBeingSearched as
Combobox)

Dim strSearch As String

strSearch = "[idString] =" & Me![comboboxBeingSearched].Column(1)

Me.formBeingSearched.Form.RecordsetClone.FindFirst (strSearch)
Me.formBeingSearched.Form.Bookmark =
Me.formBeingSearched.Form.RecordsetClone.Bookmark

End Function


Private Sub comboboxCitySearch_AfterUpdate()
Call search(subformCity, comboboxCity)
End Sub


***** THIS WORKS
Private Sub comboboxCitiesSearch_AfterUpdate()
'Find record corresponding to City Selection
Dim strSearch As String
strSearch = "[idString] =" & Me![comboboxCity].Column(1)

Me.subformCity.Form.RecordsetClone.FindFirst (strSearch)
Me.subformCity.Form.Bookmark =
Me.subformCity.Form.RecordsetClone.Bookmark
End Sub


Any help would be greatly appreciated,

Thanks.
 
Here is a function I use that is similar to what you want. Maybe it will
give you some ideas:

Public Function ComboLookup(ByRef frm As Form, ByRef ctl As Control, _
ByRef strFieldName As String)
Dim rst As Recordset

On Error GoTo ComboLookup_Error

Set rst = frm.RecordsetClone
rst.FindFirst strFieldName & " = '" & ctl & "'"
If Not rst.NoMatch Then
ctl = ctl.OldValue
frm.Bookmark = rst.Bookmark
frm.Repaint
End If
Set rst = Nothing

ComboLookup_Exit:

On Error Resume Next
Exit Function

ComboLookup_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure ComboLookup of Module modFormOperations"
GoTo ComboLookup_Exit

End Function
 
Works like a charm, can i ask you a couple questions:

1) What is the diffrence between a function and a sub?
2) I come from mostly a java background so i dont know if you can
answer this but i was under the impression that "Me" is the same as
"this" in Java.

Thanks
 
Hi Jakub,

1) Generally, the difference is that a function returns a value, and a sub
doesn't. VB is a bit of a messy mixture of an old-fashioned imperative
language with bolt on concepts from lovely, object oriented, modern
languages like java.

2) Yup - pretty much. Your problem was a matter of scope. I take it your
function was defined in an external module? Think of access forms and
modules like separate classes in java. You wouldn't expect to use "this" to
reference a public variable in another class - same in Access. In the code
associated with the form, you can use me to your hearts content. However,
in Modules, "Me", if anything, will refer to the module. In your original
code "Me" - or at least the version you though you were using was out of
scope, and therefore inaccessible to the Module containing your Function.

Hope that makes sense! And glad it worked!

B.
 
So if was it possible for me to declare it as a sub instead of a
function to be able to use Me? or are all the subs pre exisiting
properites of the form.
 
A function can return a value, but a sub does not. In the early days of the
BASIC language, there was no such thing as a function. Everything was line
number oriented and all data and variables were always visible, so functions
were not necessary. Then there was the Gosub command:
1050 GoSub 2510
meant you are now on line 1050, the next line of code to execute is line
2510. The code would jump to line 2510 and continue executing until it hit a
Return statement. It would then go back to the next line number after 1050
and continue executing from that point.

I don't know Java (except I drink too much of it), but Me. is just
shorthand for the current form, report, or class.

Me.txtClientName is the same as
Forms!frmClient!txtClientName

It refers to the current object. Now, in light of that, if you call a
function outside the form module, it will not understand Me. because it is
not the current object.
 
Check out Klatuu's answer - very well put!

You could declare this as a sub certainly - or you can use a function. But
if you want the code to be called from different forms, you'll need to put
it in a module and therefore can't use "me". "me" can only be used in code
that is part of the form's module.

:->

Ben.
 
Thanks a lot guys, i understand now :)
Ben said:
Check out Klatuu's answer - very well put!

You could declare this as a sub certainly - or you can use a function. But
if you want the code to be called from different forms, you'll need to put
it in a module and therefore can't use "me". "me" can only be used in code
that is part of the form's module.

:->

Ben.
 
Back
Top