Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    HS_1 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2016
    Posts
    109

    Angry Function doesn't work

    Wonder if somebody can help me, I am beginner in programming and trying to execute function I located in Module which doesn't work. Basically it should grab some data from table "tb_pipeallow", which matches field "p_size" ="size" in table "TB_Main_Length", multiply quantity by allowance and display the sum on a form in a filed "txtLen" by a click of the button.

    I can't figure it out what I am doing wrong, thanks in advance HS_1

    ' Procedure Name: CabLen
    Public Function cablen(Dp As Single, lp As Single, nf As Integer, Nvf1 As Integer, Nvf3 As Integer) As Single


    Dp = nz(Size, 0)
    lp = nz(P_lgth, 0)
    nf = nz(Fl_Qty, 0)
    Nvf1 = nz(Vlv_F150_Qty, 0)
    Nvf3 = nz(Vlv_F300_Qty, 0)
    Debug.Print Dp, lp, nf, Nvf1, Nvf3

    Dim Ap As Single
    Dim Af As Single
    Dim Avf1 As Single
    Dim Avf3 As Single
    Dim strAllow As String
    Dim RsAllow As Recordset

    Ap = nz(RsAllow.Fields("p_allow"), 0)
    Af = nz(RsAllow.Fields("Fl"), 0)
    Avf1 = nz(RsAllow.Fields("Vlv_F150"), 0)
    Avf3 = nz(RsAllow.Fields("Vlv_F300"), 0)
    strAllow = "select p_allow, Vlv_F150, Vlv_F300, fl, from tb_pipeallow where abs(p_size-" & Dp & ")<1e-5"

    Set RsAllow = db.OpenRecordset(strAllow)
    If RsAllow.EOF Then
    MsgBox ("No data selected from tb_pipeallow")
    Exit Function
    End If


    RsAllow.Close


    Set RsAllow = Nothing
    Debug.Print Ap, Af, Avf1, Avf3

    cablen = (Ap * lp + Af * nf + Avf1 * Nvf1 + Avf3 * Nvf3)

    Exit Function
    exitsub:
    MsgBox Err.Description
    End Function




    Private Sub btnCalc_Click()
    Dim cablen As Integer
    Me.txtHLen = cablen


    End Sub

  2. #2
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You have
    Code:
    Private Sub btnCalc_Click()
        Dim cablen As Integer   '<<<<--- Not needed/ Shouldn't be there
        Me.txtHLen = cablen      '<<<<--- This is the name of a function, but you are NOT entering the required arguments!
    End Sub

    Provided these are the names of controls on a form: "Size, P_lgth, Fl_Qty, Vlv_F150_Qty, Vlv_F300_Qty", The button click would be
    Code:
    Private Sub btnCalc_Click()
        Me.txtHLen = cablen(Nz(Size, 0), Nz(P_lgth, 0), Nz(Fl_Qty, 0), Nz(Vlv_F150_Qty, 0), Nz(Vlv_F300_Qty, 0))
    End Sub
    Be aware that "Size" is a reserved word in Access and shouldn't be used as an object name.
    It also looks like you have repeating fields : "Vlv_F150_Qty, Vlv_F300_Qty".



    The function (rearranged/rewritten) would be something like:
    Code:
    Public Function cablen(Dp As Single, lp As Single, nf As Integer, Nvf1 As Integer, Nvf3 As Integer) As Single
        On Error GoTo exitsub
    
        Dim Ap As Single
        Dim Af As Single
        Dim Avf1 As Single
        Dim Avf3 As Single
        Dim strAllow As String
        Dim RsAllow As DAO.Recordset
    
        Debug.Print Dp, lp, nf, Nvf1, Nvf3
    
        'set default return value
        cablen = 0
    
        strAllow = "select p_allow, Vlv_F150, Vlv_F300, fl, from tb_pipeallow where abs(p_size-" & Dp & ")<1e-5" '<< I'm not sure about this SQL - think it will error
        Debug.Print strAllow
    
        Set RsAllow = db.OpenRecordset(strAllow)
        If RsAllow.BOF And RsAllow.EOF Then
            MsgBox ("No data selected from tb_pipeallow")
        Else
            Ap = Nz(RsAllow.Fields("p_allow"), 0)
            Af = Nz(RsAllow.Fields("Fl"), 0)
            Avf1 = Nz(RsAllow.Fields("Vlv_F150"), 0)
            Avf3 = Nz(RsAllow.Fields("Vlv_F300"), 0)
            cablen = (Ap * lp + Af * nf + Avf1 * Nvf1 + Avf3 * Nvf3)
        End If
    
        RsAllow.Close
        Set RsAllow = Nothing
        Debug.Print Ap, Af, Avf1, Avf3
    
        Exit Function
    exitsub:
        MsgBox Err.Description
    End Function
    Alert!! Code is untested!!

  3. #3
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,420
    << I'm not sure about this SQL - think it will error
    1e-5 is scientific notation - as a value it is 0.00001

  4. #4
    HS_1 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2016
    Posts
    109
    ssanfu, thank you for your help, I have made all the changes as you suggested but I am getting an error "Object required" when I click on the button and calculated value on a form defaults to "0"
    I have no idea how to correct it.


    Quote Originally Posted by HS_1 View Post
    Wonder if somebody can help me, I am beginner in programming and trying to execute function I located in Module which doesn't work. Basically it should grab some data from table "tb_pipeallow", which matches field "p_size" ="size" in table "TB_Main_Length", multiply quantity by allowance and display the sum on a form in a filed "txtLen" by a click of the button.

    I can't figure it out what I am doing wrong, thanks in advance HS_1

    ' Procedure Name: CabLen
    Public Function cablen(Dp As Single, lp As Single, nf As Integer, Nvf1 As Integer, Nvf3 As Integer) As Single


    Dp = nz(Size, 0)
    lp = nz(P_lgth, 0)
    nf = nz(Fl_Qty, 0)
    Nvf1 = nz(Vlv_F150_Qty, 0)
    Nvf3 = nz(Vlv_F300_Qty, 0)
    Debug.Print Dp, lp, nf, Nvf1, Nvf3

    Dim Ap As Single
    Dim Af As Single
    Dim Avf1 As Single
    Dim Avf3 As Single
    Dim strAllow As String
    Dim RsAllow As Recordset

    Ap = nz(RsAllow.Fields("p_allow"), 0)
    Af = nz(RsAllow.Fields("Fl"), 0)
    Avf1 = nz(RsAllow.Fields("Vlv_F150"), 0)
    Avf3 = nz(RsAllow.Fields("Vlv_F300"), 0)
    strAllow = "select p_allow, Vlv_F150, Vlv_F300, fl, from tb_pipeallow where abs(p_size-" & Dp & ")<1e-5"

    Set RsAllow = db.OpenRecordset(strAllow)
    If RsAllow.EOF Then
    MsgBox ("No data selected from tb_pipeallow")
    Exit Function
    End If


    RsAllow.Close
    Set RsAllow = Nothing
    Debug.Print Ap, Af, Avf1, Avf3

    cablen = (Ap * lp + Af * nf + Avf1 * Nvf1 + Avf3 * Nvf3)

    Exit Function
    exitsub:
    MsgBox Err.Description
    End Function




    Private Sub btnCalc_Click()
    Dim cablen As Integer
    Me.txtHLen = cablen


    End Sub

  5. #5
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,975
    Suggest you post the code you are using now
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  6. #6
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,798
    I think he did, and it's a mish-mash of the 2 functions. Error is probably due to trying to access the recordset before it has been declared (discernable in the code). Even if that wasn't the case, it's fundamentally flawed to pass variables to a function, then re-assign them right off the bat:
    Public Function cablen(Dp As Single, lp As Single,...
    then
    Dp = nz(Size, 0)
    lp = nz(P_lgth, 0)
    ...
    Last edited by Micron; 05-07-2018 at 12:09 PM. Reason: spelin and gramur
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,975
    If that's the case, the OP has ignored many/all of ssanfu's suggestions
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  8. #8
    HS_1 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2016
    Posts
    109

    code I am using now

    Quote Originally Posted by ridders52 View Post
    Suggest you post the code you are using now

    Ridders52
    Below is the code I am using now. To be completely honest this function was written by somebody else and I am trying to modify it to work for my needs with very limited vba knowledge. I understand how the constant values are extracted from tb_pipeallow table and passed to function but I have problem understanding how the variable quantity and length from tb_main_length are related and red by the function.

    First table and column names: (stores quanity and length for each line_no)
    tb_Main_Length
    Line_No, L_Size, M_lgth, Vlv_F150_Qty, Vlv_F300_Qty, Fl_Qty, HLngth

    Second table
    Tb_PipeAllow
    p_allow, Vlv_F150, Vlv_F300, fl
    There is relationship: one to many between p_all (Tb_PipeAllow) and L_size (tb_Main_Length)


    Public Function cablen(Dp As Single, lp As Single, nf As Integer, Nvf1 As Integer, Nvf3 As Integer) As Single
    On Error GoTo exitsub

    Dim Ap As Single
    Dim Af As Single
    Dim Avf1 As Single
    Dim Avf3 As Single
    Dim strAllow As String
    Dim RsAllow As DAO.Recordset

    Debug.Print Dp, lp, nf, Nvf1, Nvf3

    'set default return value
    cablen = 0

    strAllow = "select p_allow, Vlv_F150, Vlv_F300, fl, from tb_pipeallow where abs(p_size-" & Dp & ")<1e-5" '<< I'm not sure about this SQL - think it will error
    Debug.Print strAllow

    Set RsAllow = db.OpenRecordset(strAllow)
    If RsAllow.BOF And RsAllow.EOF Then
    MsgBox ("No data selected from tb_pipeallow")
    Else
    Ap = Nz(RsAllow.Fields("p_allow"), 0)
    Af = Nz(RsAllow.Fields("Fl"), 0)
    Avf1 = Nz(RsAllow.Fields("Vlv_F150"), 0)
    Avf3 = Nz(RsAllow.Fields("Vlv_F300"), 0)
    cablen = (Ap * lp + Af * nf + Avf1 * Nvf1 + Avf3 * Nvf3)
    End If

    RsAllow.Close
    Set RsAllow = Nothing
    Debug.Print Ap, Af, Avf1, Avf3

    Exit Function
    exitsub:
    MsgBox Err.Description
    End Function

    Private Sub btnCalc_Click()
    Me.txtHLen = cablen(Nz(L_size, 0), Nz(M_lgth, 0), Nz(Fl_Qty, 0), Nz(Vlv_F150_Qty, 0), Nz(Vlv_F300_Qty, 0))
    End Sub

  9. #9
    HS_1 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2016
    Posts
    109
    Sorry I have made few typing mistakes marked in red when writing the response,
    here is the "correct one".

    Ridders52
    Below is the code I am using now. To be completely honest this function was written by somebody else and I am trying to modify it to work for my needs with very limited vba knowledge. I understand how the constant values are extracted from tb_pipeallow table and passed to function but I have problem understanding how the variable quantity and length from tb_main_length are related and red by the function.

    First table and column names: (stores quanity and length for each line_no)
    tb_Main_Length
    Line_No, L_Size, M_lgth, Vlv_F150_Qty, Vlv_F300_Qty, Fl_Qty, HLngth

    Second table
    Tb_PipeAllow
    P_size, p_allow, Vlv_F150, Vlv_F300, fl
    There is relationship: one to many between p_size (Tb_PipeAllow) and L_size (tb_Main_Length)

    Public Function cablen(Dp As Single, lp As Single, nf As Integer, Nvf1 As Integer, Nvf3 As Integer) As Single
    On Error GoTo exitsub

    Dim Ap As Single
    Dim Af As Single
    Dim Avf1 As Single
    Dim Avf3 As Single
    Dim strAllow As String
    Dim RsAllow As DAO.Recordset

    Debug.Print Dp, lp, nf, Nvf1, Nvf3

    'set default return value
    cablen = 0

    strAllow = "select p_allow, Vlv_F150, Vlv_F300, fl, from tb_pipeallow where abs(p_size-" & Dp & ")<1e-5" '<< I'm not sure about this SQL - think it will error
    Debug.Print strAllow

    Set RsAllow = db.OpenRecordset(strAllow)
    If RsAllow.BOF And RsAllow.EOF Then
    MsgBox ("No data selected from tb_pipeallow")
    Else
    Ap = Nz(RsAllow.Fields("p_allow"), 0)
    Af = Nz(RsAllow.Fields("Fl"), 0)
    Avf1 = Nz(RsAllow.Fields("Vlv_F150"), 0)
    Avf3 = Nz(RsAllow.Fields("Vlv_F300"), 0)
    cablen = (Ap * lp + Af * nf + Avf1 * Nvf1 + Avf3 * Nvf3)
    End If

    RsAllow.Close
    Set RsAllow = Nothing
    Debug.Print Ap, Af, Avf1, Avf3

    Exit Function
    exitsub:
    MsgBox Err.Description
    End Function

    Private Sub btnCalc_Click()
    Me. HLngth = cablen(Nz(L_size, 0), Nz(M_lgth, 0), Nz(Fl_Qty, 0), Nz(Vlv_F150_Qty, 0), Nz(Vlv_F300_Qty, 0))
    End Sub

    Quote Originally Posted by HS_1 View Post
    Ridders52
    Below is the code I am using now. To be completely honest this function was written by somebody else and I am trying to modify it to work for my needs with very limited vba knowledge. I understand how the constant values are extracted from tb_pipeallow table and passed to function but I have problem understanding how the variable quantity and length from tb_main_length are related and red by the function.

    First table and column names: (stores quanity and length for each line_no)
    tb_Main_Length
    Line_No, L_Size, M_lgth, Vlv_F150_Qty, Vlv_F300_Qty, Fl_Qty, HLngth

    Second table
    Tb_PipeAllow
    p_allow, Vlv_F150, Vlv_F300, fl
    There is relationship: one to many between p_all (Tb_PipeAllow) and L_size (tb_Main_Length)


    Public Function cablen(Dp As Single, lp As Single, nf As Integer, Nvf1 As Integer, Nvf3 As Integer) As Single
    On Error GoTo exitsub

    Dim Ap As Single
    Dim Af As Single
    Dim Avf1 As Single
    Dim Avf3 As Single
    Dim strAllow As String
    Dim RsAllow As DAO.Recordset

    Debug.Print Dp, lp, nf, Nvf1, Nvf3

    'set default return value
    cablen = 0

    strAllow = "select p_allow, Vlv_F150, Vlv_F300, fl, from tb_pipeallow where abs(p_size-" & Dp & ")<1e-5" '<< I'm not sure about this SQL - think it will error
    Debug.Print strAllow

    Set RsAllow = db.OpenRecordset(strAllow)
    If RsAllow.BOF And RsAllow.EOF Then
    MsgBox ("No data selected from tb_pipeallow")
    Else
    Ap = Nz(RsAllow.Fields("p_allow"), 0)
    Af = Nz(RsAllow.Fields("Fl"), 0)
    Avf1 = Nz(RsAllow.Fields("Vlv_F150"), 0)
    Avf3 = Nz(RsAllow.Fields("Vlv_F300"), 0)
    cablen = (Ap * lp + Af * nf + Avf1 * Nvf1 + Avf3 * Nvf3)
    End If

    RsAllow.Close
    Set RsAllow = Nothing
    Debug.Print Ap, Af, Avf1, Avf3

    Exit Function
    exitsub:
    MsgBox Err.Description
    End Function

    Private Sub btnCalc_Click()
    Me.txtHLen = cablen(Nz(L_size, 0), Nz(M_lgth, 0), Nz(Fl_Qty, 0), Nz(Vlv_F150_Qty, 0), Nz(Vlv_F300_Qty, 0))
    End Sub

  10. #10
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Whoops!!

    I just noticed an error in the SQL line.

    There should NOT be a comma between fl and FROM.
    Code:
    strAllow = "SELECT p_allow, Vlv_F150, Vlv_F300, fl, FROM tb_pipeallow WHERE abs(p_size-" & Dp & ") < 0.00001"



    There is relationship: one to many between p_size (Tb_PipeAllow) and L_size (tb_Main_Length)
    So "p_size" is the PK field in "Tb_PipeAllow"? (Each value in "p_allow" is unique?)



    You could post your dB to make it easier to troubleshoot. Just need the form, tables/queries involved and some examples of what you expect to get...

  11. #11
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,798
    HS_1: If you want to make your posts easier for us to read:
    - don't quote every bit of every prior reply
    - use code tags and proper indentation in and around your code.
    Both of these things will make your posts less arduous to read.

  12. #12
    HS_1 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2016
    Posts
    109

    dbase

    Ssanfu, here is the database for the code.


    Quote Originally Posted by ssanfu View Post
    Whoops!!

    I just noticed an error in the SQL line.

    There should NOT be a comma between fl and FROM.
    Code:
    strAllow = "SELECT p_allow, Vlv_F150, Vlv_F300, fl, FROM tb_pipeallow WHERE abs(p_size-" & Dp & ") < 0.00001"




    So "p_size" is the PK field in "Tb_PipeAllow"? (Each value in "p_allow" is unique?)



    You could post your dB to make it easier to troubleshoot. Just need the form, tables/queries involved and some examples of what you expect to get...
    Attached Files Attached Files

  13. #13
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,798
    Well, so much for not quoting everything.
    Ciao.

  14. #14
    HS_1 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2016
    Posts
    109
    What do you mean by not quoting everything? I don’t have lots of interaction on this forum so I don’t know what’s expected from me when asking for help
    Quote Originally Posted by Micron View Post
    Well, so much for not quoting everything.
    Ciao.

  15. #15
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by HS_1 View Post
    What do you mean by not quoting everything?
    See how I cut down your quote?

    Look at Micron's post # 13. Nothing quoted. It is not necessary to quote the entire post - look at your Post #9.

    When you post code, click the '#' in the reply header... put the code between the code tags.


    So I looked at your dB - corrected a few things. You were getting an error because you were using "L_Size" instead of "txtL_Size".
    I added the "cablelen" calculation to the query. Now, anytime you change one of the values, the traced length is automatically calculated.


    See if this is what you want.....
    Attached Files Attached Files

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 01-21-2018, 04:06 AM
  2. Replies: 12
    Last Post: 05-02-2017, 07:39 PM
  3. Replies: 3
    Last Post: 09-19-2014, 07:58 AM
  4. Replies: 3
    Last Post: 01-07-2014, 09:57 AM
  5. Replies: 2
    Last Post: 01-03-2011, 05:17 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