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

help guys! im currently making a color guessing game program, for every correct asnwer gives you score. i already got how to randomize 3 colors, my main problem is that i dont know what is the correct code in order for the user to get score. every time you will run the program it gives me the error variable not defined. please help me!! please check my code above. THANKS!

Recommended Answers

All 6 Replies

What variable is it complaining about? Please post the exact text of the error message. Also, please use the "Code" tool to insert code that is properly formatted.

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

There it is. The error message it display is "Sub or Function not defined", this error appears to be in Command2_MouseDown line, where the scoring must take place.

When you get the message "variable not defined", the message identifies the variable that it is flagging. It also gives you the specific line that generated the error. I'm not going to play 20 questions. If you want help then please provide complete information. Having said that, I suspect your first error was on line 16 because you reference the array, Values, which is defined in Command1_Click but goes out of scope when you exit the sub. If you want to use Values in multiple subs/functions then you have to move it to have module scope, like "score".

I have another problem with your code. You have subs like Command1_Click and Form_Load but none of them have standard parameter lists or "Handles" qualifiers. What version of VB are you using?

I'm only using portable edition Visual basic 6.0. This time I tried to move Values in module scope like what you said, the error now is "Type mismatch" still in line 16. What I really want to do with line 16 is that if the value of Text1.Text is equivalent to the BackColor of Label1, which I declared as Values(0) = vbRed, Values(1) = vbBlue or Values(2) = vbGreen it should generate score or add score.

I'm not familiar with portable VB but the line

If Text1.Text = Label1.BackColor = Values(0) Then

in standard VB should be written

If a = b And b = c Then

and in my edition (VB 2010), you can't compare a string (Text1.Text) to a Color. That would explain the Type mismatch.

Thanks! I already solved my problem! Cheers man!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.