Results 1 to 5 of 5

Thread: RecordSet in Access

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Posts
    4

    RecordSet in Access

    Code:
    Function Postage_Charge(Postage As String, Country As String) As Currency
    
        Set RecordD = New ADODB.Recordset
        RecordD.Open "SELECT Postage, Country FROM tblPostal", CurrentProject.Connection
    
        'Return Postal charges plus shipping'
        Postage_Charge = RecordD.Fields(0).Value + RecordD.Fields(1).Value
    
    End Function
    The problem I have is I want to pass into RecordD.Open the value of Postage, which may be "First" for example and not the string "Postage" as it thinks now. Eg:

    Code:
    Something = Postage_Charge("First", "UK");
    (both First and UK exist in a table)

    I have tried $Postage with no luck. Anyone have any suggestions?

    Thanks

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    At the moment, the value of "postage" which you've got inside of the argument to the Open method is being treated as part of a string as it's enclosed (along with the rest of the SQL command) in double quotes.

    Try changing the code to this & the postage value which makes up the SQL command should equal whatever value you pass into the function/ the value of the postage variable instead:
    VB Code:
    1. Function Postage_Charge(Postage As String, Country As String) As Currency
    2.  
    3.     Set RecordD = New ADODB.Recordset
    4.     RecordD.Open "SELECT [b]" & Postage & ", " & Country  & _
    5.     "[/b] FROM tblPostal", CurrentProject.Connection
    6.  
    7.     'Return Postal charges plus shipping'
    8.     Postage_Charge = RecordD.Fields(0).Value + RecordD.Fields(1).Value
    9.  
    10. End Function

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Posts
    4
    Thanks alot. I will try this and let you know how it went.

  4. #4
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    Don't you need to filter the amounts?

    IE with a where clause?
    Building on Alex Reads post:
    VB Code:
    1. Function Postage_Charge(Postage As String, Country As String) As Currency
    2.     dim strSql as string
    3.  
    4.     strsql="SELECT Postage, Country "
    5.     strsql=strsql & "FROM tblPostal "
    6.     strsql=strsql & "WHERE [postage]='" & Postage & "' and [country]='" & Country  & "'"
    7.  
    8.     Set RecordD = New ADODB.Recordset
    9.     RecordD.Open strsql,CurrentProject.Connection,3,3,1 'static, optimistic, adcmdtext
    10.  
    11.     'Return Postal charges plus shipping'
    12.     if recordd.eof then
    13. '---- no records returned
    14.     else
    15. '---- check your fields...?
    16.        Postage_Charge = RecordD.Fields(0).Value + RecordD.Fields
    17. (1).Value
    18.     end if
    19.  
    20. '---- close recordset and clean up
    21.     recordd.close
    22.     set recordd = nothing
    23. End Function

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Posts
    4
    Thanks, I have used both of the ideas. Cheers!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width