Results 1 to 10 of 10

Thread: variable not defined

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2012
    Posts
    30

    Post variable not defined

    Please help me with my program guys!
    Im currently creating a color guessing program, for every correct answer will give score. The codes of my program are below. Im having a hard time in correcting my code. It has an error of variable not defined please help me!


    Option Explicit
    Dim score As Integer
    Private Sub Command1_Click()
    Dim Values(3) As ColorConstants
    Values(0) = vbRed
    Values(1) = vbBlue
    Values(2) = vbGreen
    Dim RandomNumber As ColorConstants
    RandomNumber = Values(Rnd * 2)
    Label1.BackColor = RandomNumber
    Randomize
    End Sub

    Private Sub Command2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Text1.Text = Label1.BackColor = Values(0) Then
    score = score + 1
    Label3.Caption = score
    End If
    If Text1.Text = Label1.BackColor = Values(1) Then
    score = score + 1
    Label3.Caption = score
    If Text1.Text = Label1.BackColor = Values(2) Then
    score = score + 1
    Label3.Caption = score
    End Sub

    Private Sub Form_Load()
    Label2.Caption = "the color is:"
    Text1.Text = ""
    Label4.Caption = "score:"
    Label1.Caption = ""
    Command2.Caption = "submit"
    End Sub

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: variable not defined

    You've got at least 3 problems

    1. The Variable Values is defined in Command1_Click and you're trying to access it in Command2_Click. To do this you must define Values at the Form level (ie in the Declarations Section of the Form.

    2. You're missing 2 'End If' statements in Command2_Click

    3. This statement: If Text1.Text = Label1.BackColor = Values(0) Then will result in a Type Mismatch Error. It's not clear what you're trying to do

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2012
    Posts
    30

    Re: variable not defined

    Then how would I do with this statement If Text1.Text = Label1.BackColor = Values(0)? What i'm trying to do is that if the value in Text1.text is equivalent to the BackColor of Label1 for example vbRed, vbBlue or vbGreen then it should add score. I'm really having hard time with this thing.

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: variable not defined

    You would not use more than 1 = on a line what you have there will effectively check to see if the text is true or false and that is not what you want.

    The question is what do you want them to enter in the text box? Do you want them to enter a number from 1 to 3, The actual numeric value of the color or the word for the color. Depending on this factor the code would be different in each case.

    For example if you want them to enter the word Red for the color red then you would do

    Code:
    If Text1.Text="Red" and Label1.BackColor=vbRed Then
        score=score+1
    End If
    If the first part of your code you have another mistake. You call rnd() before you call randomize. You need to call Randomize first and make sure that you only call it once then the rnd() function will work properly.

    In other words the call to Randomize should be moved to the Form_Load event and removed from where you have it.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2012
    Posts
    30

    Cool Re: variable not defined

    Quote Originally Posted by DataMiser View Post
    You would not use more than 1 = on a line what you have there will effectively check to see if the text is true or false and that is not what you want.

    The question is what do you want them to enter in the text box? Do you want them to enter a number from 1 to 3, The actual numeric value of the color or the word for the color. Depending on this factor the code would be different in each case.

    For example if you want them to enter the word Red for the color red then you would do

    Code:
    If Text1.Text="Red" and Label1.BackColor=vbRed Then
        score=score+1
    End If
    If the first part of your code you have another mistake. You call rnd() before you call randomize. You need to call Randomize first and make sure that you only call it once then the rnd() function will work properly.

    In other words the call to Randomize should be moved to the Form_Load event and removed from where you have it.

    Thank you very much! I already finished creating my program thanks man!

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Aug 2012
    Posts
    30

    Re: variable not defined

    Um can I ask one last thing, how would I code a statement for example the user will enter the color in text box i.e. yellow, it must be the same if the user enters Yellow. How would I do that?

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: variable not defined

    You can either use the Ucase$() or LCase$() functions to convert the case in your test or if you want a global solution you can add a line to the very top of your form code under the declarations section

    Code:
    Option Compare Text
    adding this line will make all text checks non case sensitive

    Otherwise you could do
    Code:
    If Ucase$(Text1.Text)="RED" and Label1.BackColor=vbRed Then
        score=score+1
    End If
    or you could do
    Code:
    If LCase$(Text1.Text)="red" and Label1.BackColor=vbRed Then
        score=score+1
    End If

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Aug 2012
    Posts
    30

    Re: variable not defined

    Thank you again! I already fixed my program.

  9. #9
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: variable not defined

    Also.....
    Code:
    If StrConv(Text1.Text, vbProperCase) = "Red" And Label1.BackColor = vbRed Then

  10. #10
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Re: variable not defined

    Yep doggle is right .you are missing two end if as well .
    Code:
    Option Explicit
    Dim score As Integer
    Private Sub Command1_Click()
    Dim Values(3) As ColorConstants
    Values(0) = vbRed
    Values(1) = vbBlue
    Values(2) = vbGreen
    Dim RandomNumber As ColorConstants
    RandomNumber = Values(Rnd * 2)
    Label1.BackColor = RandomNumber
    Randomize
    End Sub
    
    Private Sub Command2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Text1.Text = Label1.BackColor = Values(0) Then
    score = score + 1
    Label3.Caption = score
    End If
    If Text1.Text = Label1.BackColor = Values(1) Then
    score = score + 1
    Label3.Caption = score
    End if      'End if Needs to be here 
    If Text1.Text = Label1.BackColor = Values(2) Then
    score = score + 1
    Label3.Caption = score
    End if             'Endif needs to be here
    End Sub
    
    Private Sub Form_Load()
    Label2.Caption = "the color is:"
    Text1.Text = ""
    Label4.Caption = "score:"
    Label1.Caption = ""
    Command2.Caption = "submit"
    End Sub
    Last edited by firoz.raj; Aug 29th, 2012 at 04:41 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
  •  



Click Here to Expand Forum to Full Width