G
Guest
How do I pass the result from a user-defined function into my append query?
thanks
thanks
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Mike said:Public Function GetStatus(result)
Dim rec As Recordset
Dim db As Database
Set db = CurrentDb
Set rec = db.OpenRecordset("dbo_application")
Do Until rec.EOF
If rec![active_sw] = "C" Then
result = "CANCELLED"
rec.MoveNext
ElseIf rec![active_sw] = "W" Then
result = "WITHDREW"
rec.MoveNext
ElseIf rec![active_sw] = "A" And Not IsNull(rec![admit_dec_code]) Then
result = ""
rec.MoveNext
ElseIf rec![active_sw] = "A" And IsNull(rec![admit_dec_code]) And
rec![deferred_sw] = "A" Then
result = "ALTERNATE"
rec.MoveNext
ElseIf rec![active_sw] = "A" And IsNull(rec![admit_dec_code]) And
rec![deferred_sw] = "D" Then
result = "DEFERRED"
rec.MoveNext
ElseIf rec![active_sw] = "A" And IsNull(rec![admit_dec_code]) And
IsNull(rec![deferred_sw]) And Not IsNull(rec![referred_dept]) Then
result = "REFERRED"
rec.MoveNext
ElseIf rec![active_sw] = "A" And IsNull(rec![admit_dec_code]) And
IsNull(rec![deferred_sw]) And IsNull(rec![referred_dept]) And
rec![complete_sw] = "I" Then
result = "INCOMPLETE"
rec.MoveNext
Else
End If
Loop
rec.Close
Set db = Nothing
End Function
Query--->status: GetStatus([result])
kingston via AccessMonster.com said:Please post the function and the query if possible. Does the function
work
as expected? In the results pane in the VB window (bottom), what happens
when you enter: ?FunctionName(Input)
Public Function GetStatus(result)
Dim rec As Recordset
Dim db As Database
Set db = CurrentDb
Set rec = db.OpenRecordset("dbo_application")
Do Until rec.EOF
If rec![active_sw] = "C" Then
result = "CANCELLED"
rec.MoveNext
ElseIf rec![active_sw] = "W" Then
result = "WITHDREW"
rec.MoveNext
ElseIf rec![active_sw] = "A" And Not IsNull(rec![admit_dec_code]) Then
result = ""
rec.MoveNext
ElseIf rec![active_sw] = "A" And IsNull(rec![admit_dec_code]) And
rec![deferred_sw] = "A" Then
result = "ALTERNATE"
rec.MoveNext
ElseIf rec![active_sw] = "A" And IsNull(rec![admit_dec_code]) And
rec![deferred_sw] = "D" Then
result = "DEFERRED"
rec.MoveNext
ElseIf rec![active_sw] = "A" And IsNull(rec![admit_dec_code]) And
IsNull(rec![deferred_sw]) And Not IsNull(rec![referred_dept]) Then
result = "REFERRED"
rec.MoveNext
ElseIf rec![active_sw] = "A" And IsNull(rec![admit_dec_code]) And
IsNull(rec![deferred_sw]) And IsNull(rec![referred_dept]) And
rec![complete_sw] = "I" Then
result = "INCOMPLETE"
rec.MoveNext
Else
End If
Loop
rec.Close
Set db = Nothing
End Function
Query--->status: GetStatus([result])
[quoted text clipped - 8 lines]Please post the function and the query if possible. Does the function work
as expected? In the results pane in the VB window (bottom), what happens
kingston via AccessMonster.com said:Actually, functions work more like this:
Public Function GetStatus(Input) as Integer
If Input = "xyz" then
GetStatus = 123
Else
GetStatus = 456
End If
End Function
Then, when you GetStatus in the results pane, or a query, or a form:
GetStatus("abc") will give you 456
GetStatus("xyz") = 123
What you've got is a procedure that doesn't return anything (a subroutine
rather than a function). So, GetStatus(...) is not equal to anything, and
calling it won't return a result; it will do something but will not give a
value back to the call statement. What is the logic behind trying to run it
in a query? Are you trying to do something recursively or iteratively?
Public Function GetStatus(result)
Dim rec As Recordset
Dim db As Database
Set db = CurrentDb
Set rec = db.OpenRecordset("dbo_application")
Do Until rec.EOF
If rec![active_sw] = "C" Then
result = "CANCELLED"
rec.MoveNext
ElseIf rec![active_sw] = "W" Then
result = "WITHDREW"
rec.MoveNext
ElseIf rec![active_sw] = "A" And Not IsNull(rec![admit_dec_code]) Then
result = ""
rec.MoveNext
ElseIf rec![active_sw] = "A" And IsNull(rec![admit_dec_code]) And
rec![deferred_sw] = "A" Then
result = "ALTERNATE"
rec.MoveNext
ElseIf rec![active_sw] = "A" And IsNull(rec![admit_dec_code]) And
rec![deferred_sw] = "D" Then
result = "DEFERRED"
rec.MoveNext
ElseIf rec![active_sw] = "A" And IsNull(rec![admit_dec_code]) And
IsNull(rec![deferred_sw]) And Not IsNull(rec![referred_dept]) Then
result = "REFERRED"
rec.MoveNext
ElseIf rec![active_sw] = "A" And IsNull(rec![admit_dec_code]) And
IsNull(rec![deferred_sw]) And IsNull(rec![referred_dept]) And
rec![complete_sw] = "I" Then
result = "INCOMPLETE"
rec.MoveNext
Else
End If
Loop
rec.Close
Set db = Nothing
End Function
Query--->status: GetStatus([result])
[quoted text clipped - 8 lines]Please post the function and the query if possible. Does the function work
as expected? In the results pane in the VB window (bottom), what happensthanks
I'm trying to return a value back and append it to a table. I tried using an
IIf statement but it was to long and messy. I thought I could write a
functiont that would pass the result back to the query and append it to the
table but I don't want the same value for each record in my table.
[quoted text clipped - 70 lines]Actually, functions work more like this:
kingston via AccessMonster.com said:That sounds perfectly feasible. You won't necessarily get the same value for
each record in the table. What is the calculation you want to perform? What
is the value you want to set in the table? Actually, it seems like you were
on the right track with an embedded IIF statement in the query. Let's say
your table has 4 fields, and you want the 4th field to be the result of the
first two based on the state of the 3rd one. Then, you'd have an update
query where the 4th field would have an Update To: block that looks like
IIF([Field3]="Canceled",[Field1]-10,IIF([Field3]="Active",[Field1]*[Field2],-
1)
So the default value is -1. If [Field3]="Active" then [Field4]=[Field1]*
[Field2] (won't yield the same value for each record unless [Field1] and
[Field2] coincidentally result in the same product). Last, if [Field3]
="Canceled" [Field4]=[Field1]-10 (again, not the same value for each record
unless [Field1] is the same for every record). If the IIF statement gets too
long, paste it into a text editor and construct it correctly first. Then
paste it into the query. HTH
I'm trying to return a value back and append it to a table. I tried using an
IIf statement but it was to long and messy. I thought I could write a
functiont that would pass the result back to the query and append it to the
table but I don't want the same value for each record in my table.
[quoted text clipped - 70 lines]Actually, functions work more like this:thanks
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.