Results 1 to 12 of 12

Thread: [RESOLVED] Basic Problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Location
    London
    Posts
    90

    Resolved [RESOLVED] Basic Problem

    Can ayone see whats wrong with this; I keep getting "variable not defined" with the variable Allboards high-lighted, yet It seems to me I have defined it!!!


    VB Code:
    1. Public Function calcBoards()
    2. Dim Allboards As Single
    3. Dim Brd1 As Single, Brd2 As Single, Brd3 As Single, Brd4 As Single, Brd5 As Single, Brd6 As Single
    4. Brd1 = Val(Text4.Text)
    5. Brd2 = Val(Text7.Text)
    6. Brd3 = Val(Text8.Text)
    7. Brd4 = Val(Text9.Text)
    8. Brd5 = Val(Text10.Text)
    9. Brd6 = Val(Text11.Text)
    10. Allboards = Brd1 + Brd2 + Brd3 + Brd4 + Brd5 + Brd6
    11. calcBoards = Allboards
    12. End Function

    Any quick help please

  2. #2
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Basic Problem

    which line errors?? that function works fine for me with no error and option explicit on, try using

    VB Code:
    1. Public Function calcBoards() [B]as Single[/B]
    Chris

  3. #3
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,425

    Re: Basic Problem

    Worked for me

    3 things tho:
    1. A Function should return something, in this case a Single. So- Public Function calcBoards() As Single (currently its a Variant as it isnt defined)

    2. If one of those TextBox's arnt pressent, of on a different Form - it will give that Error.

    If the TB's are on another Form then do like: Brd1 = Val(Form2.Text4.Text)

    3. Use Option Explict (as mentioned)
    Handy References:
    MSDN Library
    ADO Tutorial Excel Tutorial MZTools (VB6)

    • Please pull down the Thread Tools menu and click the 'Mark Thread Resolved' button, or alternately edit your original post and add "Resolved" or place a in the subject when your question(s) have been answered.
    • Please use code tags [highlight=vb] your code goes here between the tags [/highlight] when posting code.

  4. #4
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,425

    Re: Basic Problem

    Quote Originally Posted by the182guy
    which line errors?? that function works fine for me with no error and option explicit on, try using

    VB Code:
    1. Public Function calcBoards() [B]as Single[/B]
    That in its self wont cause an Error. If undefined it will be trated as a Variant Data Type.
    Handy References:
    MSDN Library
    ADO Tutorial Excel Tutorial MZTools (VB6)

    • Please pull down the Thread Tools menu and click the 'Mark Thread Resolved' button, or alternately edit your original post and add "Resolved" or place a in the subject when your question(s) have been answered.
    • Please use code tags [highlight=vb] your code goes here between the tags [/highlight] when posting code.

  5. #5
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135

    Re: Basic Problem

    I don't see what's causing that error but suggest to use something like the following instead so you can pass all necessary arguments before:
    VB Code:
    1. Public Function CalcTotals(Brd1 As Single, Brd2 As Single, Brd3 As Single, _
    2.                            Brd4 As Single, Brd5 As Single, Brd6 As Single)
    3. '============================================================================
    4. Dim sngResult As Single
    5.  
    6.     sngResult = Brd1 + Brd2 + Brd3 + Brd4 + Brd5 + Brd6
    7.     CalcTotals = sngResult
    8.  
    9. End Function

  6. #6
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Basic Problem

    Quote Originally Posted by Bruce Fox
    That in its self wont cause an Error. If undefined it will be trated as a Variant Data Type.
    yes exactly so its better practice to define the type instead of just leaving variant
    Chris

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,175

    Re: Basic Problem

    I don't see your problem either but you could improve the function as follows.

    VB Code:
    1. Public Function calcBoards() As Single
    2.  
    3. calcBoards = Val(Text4.Text) _
    4.                + Val(Text7.Text) _
    5.                + Val(Text8.Text) _
    6.                + Val(Text9.Text) _
    7.                + Val(Text10.Text) _
    8.                + Val(Text11.Text)
    9.  
    10. End Function

  8. #8
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Basic Problem

    there is no problem with the function i tried it myself, the problem is one of the textboxes is either missing, not named correctly, or on another form, that is the only thing that could cause the error just as Fox said
    Chris

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Location
    London
    Posts
    90

    Re: Basic Problem

    Thanks for your replies. I've tried much of what is suggested. The specific error comes up when I carry out the click event on Command1. Works fine for the calcArea function that I wrote (same basic layout). I'm at a bit of a loss with it as it seems to be saying that the variable "Allboards" is not defined when it clearly is. I checked all the correct textboxes etc and all is on the same form.....


    VB Code:
    1. Private Sub Command1_Click()
    2. Label20.Caption = calcArea
    3. Label21.Caption = Allboards
    4.  
    5. End Sub

    Come on you geniuses or is that genii?


  10. #10

  11. #11
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,425

    Re: Basic Problem

    Quote Originally Posted by desmondno1
    variable Allboards high-lighted
    Oppps missed that

    Nothing to do with TextBox's etc....

    As Marty stated your problem is with that Variable having scope to only the Function.
    On option would be to assign the return of that Function: calcBoards

    So:
    VB Code:
    1. Private Sub Command1_Click()
    2.     Label20.Caption = calcArea
    3.     Label21.Caption = [b]calcBoards[/b]
    4. End Sub
    Handy References:
    MSDN Library
    ADO Tutorial Excel Tutorial MZTools (VB6)

    • Please pull down the Thread Tools menu and click the 'Mark Thread Resolved' button, or alternately edit your original post and add "Resolved" or place a in the subject when your question(s) have been answered.
    • Please use code tags [highlight=vb] your code goes here between the tags [/highlight] when posting code.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Location
    London
    Posts
    90

    Re: Basic Problem

    Doooh, and I thought that was what I was doing. It appears to me as a newbie that a common element in programming is not being able to see the wood for the trees. Maybe that a line for general chit chat.

    Thanks for your help folks. Also am gratefull for the advice on the better programming mr Rhino. Cheers all

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