Results 1 to 8 of 8
  1. #1
    nello87to is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Aug 2016
    Posts
    14

    Help with calling a function

    Hi all,


    I am new about Access and VB, I have a problem with a calling to a function from a query.

    This is my query:

    SELECT
    "???" AS nome,
    "???" AS ente_componente,
    "???" AS stato_classificazione,
    EXP_APPROACH AS calcolo_rischio,
    calcola_tipo_strumento([forma_tecnica_proven];[prestiti_rotativi]) AS tipo_strumento,
    "???" AS data_xxx,
    "???" AS trib
    INTO Output_Tabella1
    FROM campi_per_tabella1;


    The problem is in the calling to the functioncalcola_tipo_strumento for a "sintax error".

    This is the function:

    Option Compare Database
    Option Explicit

    Public Function calcola_tipo_strumento(r111 As String, r222 As String) As String
    If r111 = "11" Or r111 = "53" Then
    calcola_tipo_strumento = "conto"
    ElseIf r111 = "58" Or r111 = "59" Or r111 = "60" Then
    calcola_tipo_strumento = "carta"
    ElseIf r111 = "99" Then
    If r222 = "1" Then
    calcola_tipo_strumento = "aaa"
    ElseIf r222 = "0" Then
    calcola_tipo_strumento = "bbb"
    End If
    End If
    End Function


    Can someone tell me what i can look to solve this error?
    Thanks a lot,
    Nello

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,527
    the function is just a field you create in the query, but you must add the parameters (fields)
    BUT you may not want to define the parameters as strings. It will fail if the field is NULL. So make them Variants.

    Code:
    Public Function calcola_tipo_strumento(r111 , r222 ) As String

    select calcola_tipo_strumento([field1], [field2]) as MyFieldName from table

  3. #3
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,444
    looks like your function is expecting strings which are actually numbers, so perhaps [forma_tecnica_proven] and/or [prestiti_rotativi] are numbers?

    if so, change r111 As String, r222 As String to r111 As long, r222 As long

    and change all instances of

    r111 = "11" or some other number to

    r111 = 11

    Alternatively try

    calcola_tipo_strumento([forma_tecnica_proven] & "";[prestiti_rotativi] & "") AS tipo_strumento,


    Also learn to use the code tags (#) to preserve formatting, makes it easier for everyone to read

    Code:
    Public Function calcola_tipo_strumento(r111 As String, r222 As String) As String
        If r111 = "11" Or r111 = "53" Then
               calcola_tipo_strumento = "conto"
        ElseIf r111 = "58" Or r111 = "59" Or r111 = "60" Then
               calcola_tipo_strumento = "carta"
        ElseIf r111 = "99" Then
               If r222 = "1" Then
                      calcola_tipo_strumento = "aaa"
               ElseIf r222 = "0" Then
                      calcola_tipo_strumento = "bbb"
               End If
        End If
     End Function

  4. #4
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Are the forma_tecnica_proven and prestiti_rotativi fields in the campi_per_tabella1 table?
    What is the data type of each?

    I am not sure of which version of Access you are using, but mine uses commas and not semi-colons as field delimiters (but I have seen some European versions that use semi-colons like you have in your code).
    So, if I change it to:
    Code:
    calcola_tipo_strumento([forma_tecnica_proven],[prestiti_rotativi]) AS tipo_strumento,
    your code works for me.

  5. #5
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,419
    Code:
    Public Function calcola_tipo_strumento(r111, r222) As String
    select case r111
        case "11", "53"
            calcola_tipo_strumento = "conto"
        case "58", "59", "60"
            calcola_tipo_strumento = "carta"
        case "99"
            select case r222
                case "1"
                    calcola_tipo_strumento = "aaa"
                case "0"
                    calcola_tipo_strumento = "bbb"
                case else
            end select
    end select
    end function
    Your function is much easier to understand if coded as above with Select Case rather than nested IF-Then-Else.
    Your query makes no sense. Explain what it is intended to do.

  6. #6
    nello87to is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Aug 2016
    Posts
    14
    thank you all for the attention.
    I have solved about the sintax error and i will put all these cerrection for have a better code.
    I have now this kind of error: function 'calcola_tipo_strumento' not defined in the expression. Do you know what are the causes of this error?
    I think the query doesn't recognize the function, it's possible?
    i have simplify query and code to identify what's wrong:

    function:
    Option Compare Database
    Option Explicit

    Public Function calcola_tipo_strumento(r111) As String
    If r111 = "11" Or r111 = "53" Then
    calcola_tipo_strumento = "Conto"
    End If
    End Function


    query:
    SELECT calcola_tipo_strumento([forma_tecnica_proven]) AS tipo_strumento FROM campi_per_tabella1;

    ps.
    i want extract with a query some columns from a table, and for one of these column i want calculate the value with a function.
    I think this is a stupid error, but i am a cobol programmer

  7. #7
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,444
    you haven't said where the code is located - it need to be a standard module (not a form or class module) and the module has to have a different name to the function.

    If this is your simplified code, you don't even need a function

    SELECT iif([forma_tecnica_proven] in ("11","53"),"Conto","") AS tipo_strumento FROM campi_per_tabella1;

    this assumes that [forma_tecnica_proven] is a field in your campi_per_tabella1 table and is a string, not a number


  8. #8
    nello87to is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Aug 2016
    Posts
    14
    Thank you a lot!
    this was my mistake: the module has to have a different name to the function!

    thank you all and i wish you a nice 15 august =)
    Nello

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Calling function from Excel
    By tennisbuck in forum Programming
    Replies: 6
    Last Post: 12-31-2013, 10:52 PM
  2. Calling a Private Function from Another Form
    By MintChipMadness in forum Programming
    Replies: 1
    Last Post: 01-07-2013, 12:08 PM
  3. Calling a Function From A Form.
    By ksmith in forum Access
    Replies: 2
    Last Post: 06-07-2012, 02:23 PM
  4. calling function
    By ManvinderKaur in forum Programming
    Replies: 3
    Last Post: 07-22-2010, 10:53 PM
  5. Calling a function and returning a value
    By 3dmgirl in forum Programming
    Replies: 0
    Last Post: 04-23-2007, 02:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums