Wrox Programmer Forums
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 14th, 2010, 01:53 PM
Registered User
 
Join Date: Nov 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Question UDF not passing value

I'm stumped on this one. I've created a UDF to choose the correct tax rate and pass that vaue back to the calling procedure. I ran it through a watch and the "usetaxrate" is getting the correct rate but as soon as it hits "End Function" the value clears and doesn't get passed. Any ideas would be great as I have been working on this all day with different variations with the same result. Here's the code:

Private Sub ProductID_AfterUpdate()
Dim txcharged, txrate, txprice
Set txprice = [Forms]![copy of orders]![order details subform].Form![ExtendedPrice]
txrate = gettaxrate()
txcharged = txprice * txrate

[Forms]![copy of orders]![order details subform].Form![Tax] = txcharged
End Sub

Public Function gettaxrate() As Double
Dim usetaxrate
If [Forms]![copy of orders]![order details subform]![PSTExempt] = no And [Forms]![copy of orders]![order details subform]![GSTExempt] = no Then
usetaxrate = 0
ElseIf [Forms]![copy of orders]![order details subform]![PSTExempt] = no And [Forms]![copy of orders]![order details subform]![GSTExempt] = yes Then
usetaxrate = 0.05

ElseIf [Forms]![copy of orders]![order details subform]![PSTExempt] = yes And [Forms]![copy of orders]![order details subform]![GSTExempt] = no Then
usetaxrate = 0.08
Else: usetaxrate = 0.13
End If
End Function

** As a sidenote, the logic on the IF is backwards but that's another issue for another day :-)
if PST and GST exempt, no tax charged (usetaxrate=0)
if PST exempt only, GST is charged (usetaxrate=.05)
if GST exempt only, PST is charged (usetaxrate=.08)
if neither exempt, PST/GST is charged (usetaxrate=.13)

Thank you!
 
Old November 14th, 2010, 03:14 PM
Friend of Wrox
 
Join Date: Sep 2010
Posts: 245
Thanks: 5
Thanked 24 Times in 23 Posts
Default

There is nothign in your UDF tyhat set the vaule to pass back.

You need one more line of code.

Try this:
Code:
Private Sub ProductID_AfterUpdate()
Dim txcharged, txrate, txprice

Set txprice = [Forms]![copy of orders]![order details subform].Form![ExtendedPrice]

txrate = gettaxrate()

txcharged = txprice * txrate

[Forms]![copy of orders]![order details subform].Form![Tax] = txcharged

End Sub

Public Function gettaxrate() As Double
Dim usetaxrate

If [Forms]![copy of orders]![order details subform]![PSTExempt] = no And [Forms]![copy of orders]![order details subform]![GSTExempt] = no Then
   usetaxrate = 0

ElseIf [Forms]![copy of orders]![order details subform]![PSTExempt] = no And  [Forms]![copy of orders]![order details subform]![GSTExempt] = yes Then
    
  usetaxrate = 0.05

ElseIf [Forms]![copy of orders]![order details subform]![PSTExempt] = yes And [Forms]![copy of orders]![order details subform]![GSTExempt] = no Then

usetaxrate = 0.08

Else:

   usetaxrate = 0.13

End If 

' **** set the vaule to pass back to  the function name ***
gettaxrate = usetaxrate 

End Function
__________________
Boyd Trimmell aka HiTechCoach (.com)
Microsoft Access MVP Alumni 2010-2015
 
Old November 14th, 2010, 03:28 PM
Registered User
 
Join Date: Nov 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Smile

Thank you so much! As you can tell, I am new at Access and I haven't been doing too bad up to this point. I figured it had to be something simple! I was using the Access 2010 Programmer's Reference and that last line was missing from the examples.
 
Old November 14th, 2010, 04:34 PM
Friend of Wrox
 
Join Date: Sep 2010
Posts: 245
Thanks: 5
Thanked 24 Times in 23 Posts
Default

You're welcome.

Glad I could assist in your learning.
__________________
Boyd Trimmell aka HiTechCoach (.com)
Microsoft Access MVP Alumni 2010-2015





Similar Threads
Thread Thread Starter Forum Replies Last Post
UDF references DavidReese BOOK: Excel 2007 VBA Programmer's Reference ISBN: 978-0-470-04643-2 0 November 2nd, 2008 11:04 AM
trying to run an udf miguel.ossa SQL Server 2005 7 July 22nd, 2007 11:12 AM
how can i execute SQL in UDF vinod_mnr SQL Server 2000 1 March 18th, 2005 12:51 PM
UDF: allowed in one DB but not another??? ea SQL Server 2000 2 January 18th, 2005 03:14 PM
UDF and Stored Procedures hrishimusale SQL Server 2000 1 November 4th, 2003 12:07 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.