Thanks again, Dale. I modified the PARAMETERS declaration, but Access 97
won't accept the character length. I made that small modification to the
function.
At this point, Debug kicks in with "Too few parameters. Expected: 1" and
highlights the line:
Set rs = CurrentDb.OpenRecordset(strSQL, , dbFailOnError)
The Help for the OpenRecordset method says:
"For Connection and Database objects:
Set recordset = object.OpenRecordset (source, type, options, lockedits)"
I added an extra "," but this gave the error "Invalid argument". I tried
moving the "," but this only gave a syntax error.
Another argument seems to be required. What should it be?
--
Richard
Search the web and raise money for charity at
www.everyclick.com
:
When you declare the parameters, you are declaring the values you plan on
entering when the query is run. Since you are planning on typing values into
input boxes for the component and the parent parts, those are the parameters
you should be declaring. In this case, the Parameters line should look
something like:
PARAMETERS [Enter Component Part Number:] Text (15),
[Enter Parent Part Number:] Text (15) ;
I would set the # of characters (I used 15) to the same number of characters
defined for the fields (Component, and Parent) in your table.
Also, I just noticed that I failed to change the declaration of the return
value of fnProgenitor from long to string, and the default value from 0 to
"". You will need to do that too, or the function will not return a string
value.
Function fnProgenitor(Tablename as string, ID as String) as String
fnProgenitor = ""
--
HTH
Dale
Don''t forget to rate the post if it was helpful!
email address is invalid
Please reply to newsgroup only.
:
Thanks, Dale. I understood everything you posted, and re-pasted the function.
Iadded Parameter type definitions for Fsbill.COMPONENT and
fnProgenitor("Fsbill", Fsbill.PARENT) but I have a syntax error on the
PARAMETERS line of the SQL:
PARAMETERS Fsbill.COMPONENT Text, [Expr1:
fnProgenitor("Fsbill",[Fsbill]].[[PARENT])] Text;
The second parameter looks strange, but what do I know?
Thanks very much for sticking with me this far. It's a real education for me.
--
Richard
Search the web and raise money for charity at
www.everyclick.com
:
Actually, I'm on Eastern Daylight Time(it is currently 9:50 AM).
I've got a couple of points.
1. You don't need to use quotes inside of your parameters.
Change: ["Enter Component Part Number:"]
to: [Enter Component Part Number:]
When I said you might need to declare their data types, I meant that you
should define the parameters. You do this by right clicking in the upper
portion of the query grid (but not on a table), and selecting the Parameters
option from the shortcut menu. This will display the Parameters dialog box,
where you can enter your parameters (I usually cut and paste from my query to
make sure there is an exact match), and select their data type. This helps
Access/Jet correctly interpret the data type of the parameter being passed.
2. When you changed the datatype declaration on lngMyID to string, I would
have changed the name of the variable as well, to strMyID. This would make
your code easier to read and to interpret. Additionally, since the data type
is a string, you have to make sure you wrap the variable (strMyID) in quotes
in the SQL string.
Let me rewrite fnProgenitor as I think it should probably read for your
application:
Function fnProgenitor(TableName as string, ID As String) As Long
Dim strSQL As String
Dim rs As DAO.Recordset
Dim strMyID As String
strMyID = ID
fnProgenitor = 0
Do
strSQL = "SELECT Component, Parent FROM [" & TableName & "] " _
& "WHERE ID = " & chr$(34) & strMyID & chr$(34)
Set rs = CurrentDb.OpenRecordset(strSQL, , dbFailOnError)
If IsNull(rs("Parent")) Then
fnProgenitor = rs("Component")
Exit Do
Else
strMyID = rs("Parent")
End If
rs.Close
Set rs = Nothing
Loop
End Function
You had changed TableName in the function to "FsBill", but did not need to
do that, since you TableName is a variable, and you are now passing that
value to the function.
--
HTH
Dale
Don''t forget to rate the post if it was helpful!
email address is invalid
Please reply to newsgroup only.
:
My browser says your time is 6:21 AM PST. What dedication! I wouldn't have
had a drink by then either! It's 14:30 GMT here.
We're moving on now. I've cleared another couple of minor errors and here's
the SQL I'm working with:
SELECT Fsbill.COMPONENT, Fsbill.COMP_DESC, Fsbill.PARENT, Fsbill.PARNT_DESC
FROM Fsbill
WHERE Fsbill.COMPONENT = ["Enter Component Part Number:"]
AND fnProgenitor("Fsbill", Fsbill.PARENT) = ["Enter Parent Part Number:"];
And here's the code. I modified the arguments and lngMyID to type String:
Function fnProgenitor(Fsbill As String, ID As String) As Long
Dim strSQL As String
Dim rs As DAO.Recordset
Dim varParentID As Variant
Dim lngMyID As String
varParentID = Null
lngMyID = ID
fnProgenitor = 0
Do
strSQL = "SELECT ID, Parent_ID FROM [" & Fsbill & "] " _
& "WHERE ID = " & lngMyID
Set rs = CurrentDb.OpenRecordset(strSQL, , dbFailOnError)
If IsNull(rs("Parent_ID")) Then
fnProgenitor = rs("ID")
Exit Do
Else
lngMyID = rs("Parent_ID")
End If
rs.Close
Set rs = Nothing
Loop
End Function
At this stage I'm getting the following run-time error:
Syntax error (missing operator) in query expression 'ID = 0-021875GA'.
The value for ID is the first Parent in Fsbill.
If I click End on this message box, the following is shown:
This expression is typed incorrectly, or it is too complex to be evaluated ...
Clicking Debug highlights the "Set rs = Current..." line in the code.
--
Richard
Search the web and raise money for charity at
www.everyclick.com
:
Sorry, you can tell it is still early and I haven't had my cup of coffee yet.
Yes, it does require two, in the version I sent. This version includes a
TableName (string) as the first argument in the function, so you can use it
with other heirarchical datasets. So you need to modify the criteria to:
fnProgenitor("Fsbill", [Fsbill].[COMPONENT]) = [Enter Parent Part Number];
Are [Component] and [Parent] strings, or long integer values? You may also
need to declare the datatype of the values of your two parameters.
Have you modified fnProgenitor to use the correct field names for your table
structure? I always use ID and Parent_ID as the fields that define the
parent-child relationships, so you will need to modify fnProgenitor( ) with
the correct fields names.
--
HTH
Dale
Don''t forget to rate the post if it was helpful!
email address is invalid
Please reply to newsgroup only.
:
I'm using the first query from your post, and I've addapted it accordingly.
But when I try to run it, and error is displayed, saying that the number of
arguments is wrong in the WHERE clause:
Fsbill.COMPONENT = [Enter Component Part #]
AND fnProgenitor([Fsbill.COMPONENT]) = [Enter Parent Part Number];
Does fnProgenitor expect two arguments?
--
Richard
Search the web and raise money for charity at
www.everyclick.com
:
Sorry, forgot to paste that in.
Public Function fnProgenitor(TableName as String, ID As Long) As Long
Dim strSQL As String
Dim rs As DAO.Recordset
Dim varParentID As Variant
Dim lngMyID As Long