Connecting Tech Pros Worldwide Forums | Help | Site Map

Is there a faster way

Access
Guest
 
Posts: n/a
#1: Jan 22 '06
I am looking for the most efficient method to check for a entity
membership in a group. Is there a better method than this? I am
working with a native Access database. At some point I'll probably end
up splitting the data from the front end.

Public Function isGroupMember(lngEntityID As Long, lngGroupID As Long)
As Boolean
On Error GoTo err_isGroupMember
Dim rs As New ADODB.Recordset
Dim strSQL As String

strSQL = "SELECT tblGroupMembership.GroupID,
tblGroupMembership.EntityID FROM tblGroupMembership WHERE
(((tblGroupMembership.GroupID)=" & lngGroupID & ") AND
((tblGroupMembership.EntityID)=" & lngEntityID & "));"
rs.Open strSQL, CurrentProject.Connection, adOpenForwardOnly,
adLockReadOnly

If rs.EOF Then
isGroupMember = False
Else
isGroupMember = True
End If
rs.Close
Set rs = Nothing

err_isGroupMember:
If Err <> 0 Then
isGroupMember = False
End If
End Function


Allen Browne
Guest
 
Posts: n/a
#2: Jan 22 '06

re: Is there a faster way


How about:
Not IsNull(DLookup("GroupID", "tblGroupMembership", _
"(GroupID = " & lngGroupID & ") AND (EntityID = " & lngEntityID & ")"))

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Access" <alderran666@gmail.com> wrote in message
news:1137934094.701751.313460@f14g2000cwb.googlegr oups.com...[color=blue]
>I am looking for the most efficient method to check for a entity
> membership in a group. Is there a better method than this? I am
> working with a native Access database. At some point I'll probably end
> up splitting the data from the front end.
>
> Public Function isGroupMember(lngEntityID As Long, lngGroupID As Long)
> As Boolean
> On Error GoTo err_isGroupMember
> Dim rs As New ADODB.Recordset
> Dim strSQL As String
>
> strSQL = "SELECT tblGroupMembership.GroupID,
> tblGroupMembership.EntityID FROM tblGroupMembership WHERE
> (((tblGroupMembership.GroupID)=" & lngGroupID & ") AND
> ((tblGroupMembership.EntityID)=" & lngEntityID & "));"
> rs.Open strSQL, CurrentProject.Connection, adOpenForwardOnly,
> adLockReadOnly
>
> If rs.EOF Then
> isGroupMember = False
> Else
> isGroupMember = True
> End If
> rs.Close
> Set rs = Nothing
>
> err_isGroupMember:
> If Err <> 0 Then
> isGroupMember = False
> End If
> End Function[/color]


Jamey
Guest
 
Posts: n/a
#3: Jan 22 '06

re: Is there a faster way


DCount(...as in Allen's example...) > 0
and
Nz(DLookup(...as in Allen's example...)) <> ""
work as well.

i usually use the DCount variety of this test, and do so quite often.

Allen,

you see any advantages, disadvantages to either approach?

Lyle Fairfield
Guest
 
Posts: n/a
#4: Jan 22 '06

re: Is there a faster way


Not CurrentProject.Connection.Execute("SELECT * FROM
[4060148Transactions] WHERE CreditAccountID = 19 AND DebitAccountID =
18").BOF

Allen Browne
Guest
 
Posts: n/a
#5: Jan 23 '06

re: Is there a faster way


DCount() is slower: Access can't just return the first value; it has to
continue through the entire table to get the total count.

The Nz() is superfluous, because DLookup() is wrongly programmed by
Microsoft. It is incapable of distinguishing between a Null and a
zero-length string (ZLS), so if returns Null even if it should return a ZLS.
So, in this case, checking for a ZLS adds nothing.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Jamey" <cantankeris@yahoo.com> wrote in message
news:1137951413.118561.53390@f14g2000cwb.googlegro ups.com...[color=blue]
> DCount(...as in Allen's example...) > 0
> and
> Nz(DLookup(...as in Allen's example...)) <> ""
> work as well.
>
> i usually use the DCount variety of this test, and do so quite often.
>
> Allen,
>
> you see any advantages, disadvantages to either approach?
>[/color]


Terry Kreft
Guest
 
Posts: n/a
#6: Jan 23 '06

re: Is there a faster way


Public Function isGroupMember( _
lngEntityID As Long, _
lngGroupID As Long _
) As Boolean
Dim rs As New ADODB.Recordset
Dim strSQL As String

On Error GoTo err_isGroupMember

strSQL = "SELECT Count(*) " _
& "FROM tblGroupMembership " _
& "WHERE tblGroupMembership.GroupID)=" & lngGroupID _
& " AND tblGroupMembership.EntityID)=" & lngEntityID

call rs.Open(strSQL, CurrentProject.Connection, _
adOpenForwardOnly, adLockReadOnly

isGroupMember = cbool(rs(0))
rs.Close
Set rs = Nothing
Exit Function
err_isGroupMember:
If Err <> 0 Then
isGroupMember = False
End If
End Function

--

Terry Kreft


"Access" <alderran666@gmail.com> wrote in message
news:1137934094.701751.313460@f14g2000cwb.googlegr oups.com...[color=blue]
> I am looking for the most efficient method to check for a entity
> membership in a group. Is there a better method than this? I am
> working with a native Access database. At some point I'll probably end
> up splitting the data from the front end.
>
> Public Function isGroupMember(lngEntityID As Long, lngGroupID As Long)
> As Boolean
> On Error GoTo err_isGroupMember
> Dim rs As New ADODB.Recordset
> Dim strSQL As String
>
> strSQL = "SELECT tblGroupMembership.GroupID,
> tblGroupMembership.EntityID FROM tblGroupMembership WHERE
> (((tblGroupMembership.GroupID)=" & lngGroupID & ") AND
> ((tblGroupMembership.EntityID)=" & lngEntityID & "));"
> rs.Open strSQL, CurrentProject.Connection, adOpenForwardOnly,
> adLockReadOnly
>
> If rs.EOF Then
> isGroupMember = False
> Else
> isGroupMember = True
> End If
> rs.Close
> Set rs = Nothing
>
> err_isGroupMember:
> If Err <> 0 Then
> isGroupMember = False
> End If
> End Function
>[/color]


david epsom dot com dot au
Guest
 
Posts: n/a
#7: Jan 23 '06

re: Is there a faster way



"Lyle Fairfield" <lylefairfield@aim.com> wrote in message
news:1137952986.525823.52670@g47g2000cwa.googlegro ups.com...[color=blue]
> Not CurrentProject.Connection.Execute("SELECT * FROM
> [4060148Transactions] WHERE CreditAccountID = 19 AND DebitAccountID =
> 18").BOF
>[/color]

Surprised. Would have expected ...("SELECT Null FROM ...

Would that work as well?


(david)


Lyle Fairfield
Guest
 
Posts: n/a
#8: Jan 24 '06

re: Is there a faster way


I think so.
I suppose with a large table one might want to use:
"SELECT TOP 1 0 FROM [4060148Transactions] WHERE ...
(That's not ten; it's the top 1 of the "zeroeth" field.)
I expect there are many ways of dealing with this. If the table had a
million records and the code were to be run a million times a day I'd
probably use an index. As it is, this is takes between 50% and 70% of
the time the DLoopUp thingme takes/

Closed Thread