Multiple Form Instances & Me

J

Jack Leach

Could someone please verify...

If I pass a form to a public function that has an argument of type Form
(byRef), using the Me keyword, there should be no chance that vba may get
confused if there happens to be two instances of that form opened, correct?

I'm trying to globalize some standard form operations.

The function:

Public Function GoToFormRecord(ByRef frm As Form, _
ByVal fld As String, _
ByVal txt As String _
) As Boolean

....
....
Set rs = frm.RecordsetClone
strCriteria = "[" & fld & "] = """ & txt & """"
rs.FindFirst strCriteria
If Not rs.NoMatch Then
frm.Bookmark = rs.Bookmark
Ret = True
Else
Ret = False
End If
....
....
End Function

and the caller:

Private Sub hbtnGo_Click()
If Not GoToRecord(Me, "fldCompanyCode", Me.hctlGo.Text) Then
MsgBox "Record Not Found"
End If
End Sub



I'm pretty sure Me passes the hwnd, and hoping that ByRef will take care of
any issues I might have with it even if it doesn't, but was hoping someone
can confirm this.

Thanks,


--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
 
A

Allen Browne

Using Me like that should give you the correct instance of the form.

(I wouldn't want to guarantee that for reports, as there are cases where
Access applies the Filter to the wrong instance rather than Me.)
 
S

Stuart McCall

Jack Leach said:
Could someone please verify...

If I pass a form to a public function that has an argument of type Form
(byRef), using the Me keyword, there should be no chance that vba may get
confused if there happens to be two instances of that form opened,
correct?

I'm trying to globalize some standard form operations.

The function:

Public Function GoToFormRecord(ByRef frm As Form, _
ByVal fld As String, _
ByVal txt As String _
) As Boolean

...
...
Set rs = frm.RecordsetClone
strCriteria = "[" & fld & "] = """ & txt & """"
rs.FindFirst strCriteria
If Not rs.NoMatch Then
frm.Bookmark = rs.Bookmark
Ret = True
Else
Ret = False
End If
...
...
End Function

and the caller:

Private Sub hbtnGo_Click()
If Not GoToRecord(Me, "fldCompanyCode", Me.hctlGo.Text) Then
MsgBox "Record Not Found"
End If
End Sub



I'm pretty sure Me passes the hwnd, and hoping that ByRef will take care
of
any issues I might have with it even if it doesn't, but was hoping someone
can confirm this.

Thanks,


--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)

This may just be a 'posting typo', but your function is called
'GoToFormRecord', but in the 'hbtnGo_Click' procedure, you're calling it
with 'GotoRecord', which is a built-in Access function.
 
J

Jack Leach

Sorry, that was a posting typo...

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)



Stuart McCall said:
Jack Leach said:
Could someone please verify...

If I pass a form to a public function that has an argument of type Form
(byRef), using the Me keyword, there should be no chance that vba may get
confused if there happens to be two instances of that form opened,
correct?

I'm trying to globalize some standard form operations.

The function:

Public Function GoToFormRecord(ByRef frm As Form, _
ByVal fld As String, _
ByVal txt As String _
) As Boolean

...
...
Set rs = frm.RecordsetClone
strCriteria = "[" & fld & "] = """ & txt & """"
rs.FindFirst strCriteria
If Not rs.NoMatch Then
frm.Bookmark = rs.Bookmark
Ret = True
Else
Ret = False
End If
...
...
End Function

and the caller:

Private Sub hbtnGo_Click()
If Not GoToRecord(Me, "fldCompanyCode", Me.hctlGo.Text) Then
MsgBox "Record Not Found"
End If
End Sub



I'm pretty sure Me passes the hwnd, and hoping that ByRef will take care
of
any issues I might have with it even if it doesn't, but was hoping someone
can confirm this.

Thanks,


--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)

This may just be a 'posting typo', but your function is called
'GoToFormRecord', but in the 'hbtnGo_Click' procedure, you're calling it
with 'GotoRecord', which is a built-in Access function.
 
J

Jack Leach

Thanks Allen.

I haven't done much of this "globalizing" for reports yet (not sure if I
will need to), but I'm thinking maybe something along these lines (use a
passed hWnd value for forms or reports, loop the collection of open objects
and match them by hWnd):


Sub MatchForm(lhWnd As Long)
Dim frmloop As Form
Dim frm As Form
For Each frmloop in Forms
If frmloop.hWnd = lhWnd Then
Set frm = frmloop
Exit For
End If
Next

'do stuff with frm
...
...
End Sub


I would assume this could be used for reports as well as forms, and using
the hWnd property *should* guarantee that I've always got the correct
instance...

Any thoughts? I've never really played around with this before.

Thanks,
--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)



Allen Browne said:
Using Me like that should give you the correct instance of the form.

(I wouldn't want to guarantee that for reports, as there are cases where
Access applies the Filter to the wrong instance rather than Me.)

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.


Jack Leach said:
Could someone please verify...

If I pass a form to a public function that has an argument of type Form
(byRef), using the Me keyword, there should be no chance that vba may get
confused if there happens to be two instances of that form opened,
correct?

I'm trying to globalize some standard form operations.

The function:

Public Function GoToFormRecord(ByRef frm As Form, _
ByVal fld As String, _
ByVal txt As String _
) As Boolean

...
...
Set rs = frm.RecordsetClone
strCriteria = "[" & fld & "] = """ & txt & """"
rs.FindFirst strCriteria
If Not rs.NoMatch Then
frm.Bookmark = rs.Bookmark
Ret = True
Else
Ret = False
End If
...
...
End Function

and the caller:

Private Sub hbtnGo_Click()
If Not GoToRecord(Me, "fldCompanyCode", Me.hctlGo.Text) Then
MsgBox "Record Not Found"
End If
End Sub



I'm pretty sure Me passes the hwnd, and hoping that ByRef will take care
of
any issues I might have with it even if it doesn't, but was hoping someone
can confirm this.

Thanks,


--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
 
V

vanderghast

There is no need here to be concerned to passing the OBJECT by ref or by
value: your subroutine does not modify it anyhow. Passing an OBJECT by
reference just pass a reference to the reference (kind of pointer) of your
initial object: it can be seen as an additional level of indirection; in the
same spirit, note that a ByVal of an OBJECT is not as a COPY of the object,
with all its data, but a copy of the reference (pointer) to your object.

The "Me" keyword refer to the independant set of data of your object.
Indeed, if you have two forms from the same class, they both SHARE the same
CODE, but maintain INDEPENDANT data block, and that is what ME refers to:
the independant data block. As long as the property is ACCESSBILE, there
should be no problem to your subroutine GoToFormRecord(), the right data
block will be accessed... if is it accessible: a PRIVATE property of an
object is NOT accessible in code written in other unrelated class (or
module), but is always accessible in code written in the same class, as
example.


So, ByRef is not required, and VBA won't be confused, but may not let you
reach private properties, though, if the code is in a different
module/class.



Vanderghast, Access MVP
 
A

Allen Browne

As vanderghast said, just passing Me will be enough.

But the hWnd is a way to distinguish between different instances of a form.
I've suggested using the hWnd to manage your collection of instances:
http://allenbrowne.com/ser-35.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.


Jack Leach said:
Sorry, that was a posting typo...

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)



Stuart McCall said:
Jack Leach said:
Could someone please verify...

If I pass a form to a public function that has an argument of type Form
(byRef), using the Me keyword, there should be no chance that vba may
get
confused if there happens to be two instances of that form opened,
correct?

I'm trying to globalize some standard form operations.

The function:

Public Function GoToFormRecord(ByRef frm As Form, _
ByVal fld As String, _
ByVal txt As String _
) As Boolean

...
...
Set rs = frm.RecordsetClone
strCriteria = "[" & fld & "] = """ & txt & """"
rs.FindFirst strCriteria
If Not rs.NoMatch Then
frm.Bookmark = rs.Bookmark
Ret = True
Else
Ret = False
End If
...
...
End Function

and the caller:

Private Sub hbtnGo_Click()
If Not GoToRecord(Me, "fldCompanyCode", Me.hctlGo.Text) Then
MsgBox "Record Not Found"
End If
End Sub



I'm pretty sure Me passes the hwnd, and hoping that ByRef will take
care
of
any issues I might have with it even if it doesn't, but was hoping
someone
can confirm this.

Thanks,


--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)

This may just be a 'posting typo', but your function is called
'GoToFormRecord', but in the 'hbtnGo_Click' procedure, you're calling it
with 'GotoRecord', which is a built-in Access function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top