Hi,

I have 2 variables defined in a code module as public which are used in the same 3 sub procedures. While the first variable is ok, when I debug, the second variables comes up "variable not defined"! Can anyone help? PS I'm a researcher using VB to design experiments and not a programmer!

Thanks

Post the code?

Also, I don't recall how much of a difference it makes, but you might consider changing (if it's in a module) public to global.

so:

global varname as whatever

Post the code?

Also, I don't recall how much of a difference it makes, but you might consider changing (if it's in a module) public to global.

so:

global varname as whatever

Many thanks - I'll try global and post the code if it doesn't work!

Thanks again

Alright, let us know how it goes.

Sorry, it didn't work so here are the relevant parts of the code. I have included the global variabal declarations plus the three sub procedures: BuildExperiment; BuildExperimentTrial and Reset. The problem appears to lie in Reset where it accepts all the variables expect for colour1, 2....12.

You will realise from this that I am not a programmer but a researcher trying to write an experiment!!!

Again, your help is greatly appreciated.

CODE:

Option Explicit

'Array holding experiment details and results
Global ExperimentOrder(288, 20) As String

Global ExperimentMode As String ' Practice or Experiment
Global VisFieldVar As Integer
Global CurrentTrial As Integer
Global FileName As String

'Actual values
Const TotalETrials As Integer = 288 'total no of experimental trials
Const ValidETrial As Integer = 144 'no of trials for each valid condition (288/2)
Const EColourTrial As Integer = 24 'no of trials for each colour set (288/12)
Const EPositionTrial As Integer = 48 'no of trials in each position of visual field (288/6)
Const TotalPTriaGlobalGloballs As Integer = 12 'total no of practice trials
Const ValidPTrial As Integer = 6 'totalptrials / 2 conditions
Const PColourTrial As Integer = 1 'no of trials for each colour set (12/12)
Const PPositionTrial As Integer = 2 'no of trials in each position of visual field (12/6)


'Keep counts of different aspects already assigned

Global NearImmediate1OTSGC As Integer
Global NearImmediate1OXSGC As Integer
Global NearImmediate1UTGCS As Integer
Global NearImmediate1UXGCS As Integer
Global NearImmediate1GTCSO As Integer
Global NearImmediate1GXCSO As Integer

Global NearImmediate2OSTGC As Integer
Global NearImmediate2OSXGC As Integer
Global NearImmediate2UGTCS As Integer
Global NearImmediate2UGXCS As Integer
Global NearImmediate2GCTSO As Integer
Global NearImmediate2GCXSO As Integer

Global Colourl As Integer
Global Colour2 As Integer
Global Colour3 As Integer
Global Colour4 As Integer
Global Colour5 As Integer
Global Colour7 As Integer
Global Colour8 As Integer
Global Colour9 As Integer
Global Colour10 As Integer
Global Colour11 As Integer
Global Colour12 As Integer

Global AboveL As Integer
Global AboveM As Integer
Global AboveR As Integer
Global BelowL As Integer
Global BelowM As Integer
Global BelowR As Integer

Sub BuildExperiment()

Dim CountVar As Integer
Dim NearImmediate1Count As Integer
Dim NearImmediate2Count As Integer
Dim TrialType As Integer

CountVar = 0
NearImmediate1Count = 0
NearImmediate2Count = 0

ResetExperiment

Do Until False
TrialType = GetRandomNumber(2)

Select Case TrialType
Case 1
If NearImmediate1Count < ValidETrial Then
NearImmediate1Count = NearImmediate1Count + 1

CountVar = CountVar + 1

BuildExperimentTrial CountVar, "N_I1", ValidETrial, "C1", EColourTrial, EPositionTrial

End If
Case 2
If NearImmediate2Count < ValidETrial Then
NearImmediate2Count = NearImmediate2Count + 1

CountVar = CountVar + 1

BuildExperimentTrial CountVar, "N_I2", ValidETrial, "C2", EColourTrial, EPositionTrial
End If

Case Else
Beep
MsgBox "Experiment Invalid trialtype = " & TrialType
End Select

If NearImmediate1Count = ValidETrial _
And NearImmediate2Count = ValidETrial Then
Exit Do
Confidence.Visible = False
FrmEnd.Visible = True
Else
Confidence.Visible = False
Fixation.Visible = True
End If
Loop

End Sub

Sub BuildExperimentTrial(ArrayVar As Integer, ConditionVar As String, ValidNumberVar As Integer, ColourVar As String, ColourNumberVar As Integer, PositionNumberVar As Integer)
'Build individual trial

Dim LStringVar As Integer
Dim CStringVar As Integer
Dim VisFieldVar As Integer
Dim CountVar As Integer

Do Until False
LStringVar = GetRandomNumber(6)

Select Case LStringVar
Case 1
Select Case ConditionVar
Case "N_I1"
If NearImmediate1OTSGC < ValidNumberVar Then
NearImmediate1OTSGC = NearImmediate1OTSGC + 1

ExperimentOrder(ArrayVar, 1) = ExperimentMode
ExperimentOrder(ArrayVar, 2) = ArrayVar
ExperimentOrder(ArrayVar, 3) = "I1"
ExperimentOrder(ArrayVar, 4) = "N"
ExperimentOrder(ArrayVar, 5) = "O "
ExperimentOrder(ArrayVar, 6) = "T "
ExperimentOrder(ArrayVar, 7) = "S "
ExperimentOrder(ArrayVar, 8) = "G "
ExperimentOrder(ArrayVar, 9) = "C "

Exit Do
End If
Case "N_I2"
If NearImmediate2OSTGC < ValidNumberVar Then
NearImmediate2OSTGC = NearImmediate2OSTGC + 1

ExperimentOrder(ArrayVar, 1) = ExperimentMode
ExperimentOrder(ArrayVar, 2) = ArrayVar
ExperimentOrder(ArrayVar, 3) = "I2"
ExperimentOrder(ArrayVar, 4) = "N"
ExperimentOrder(ArrayVar, 5) = "O "
ExperimentOrder(ArrayVar, 6) = "S "
ExperimentOrder(ArrayVar, 7) = "T "
ExperimentOrder(ArrayVar, 8) = "G "
ExperimentOrder(ArrayVar, 9) = "C "

Exit Do
End If
End Select

Case 2
Select Case ConditionVar
Case "N_I1"
If NearImmediate1OXSGC < ValidNumberVar Then
NearImmediate1OXSGC = NearImmediate1OXSGC + 1

ExperimentOrder(ArrayVar, 1) = ExperimentMode
ExperimentOrder(ArrayVar, 2) = ArrayVar
ExperimentOrder(ArrayVar, 3) = "I1"
ExperimentOrder(ArrayVar, 4) = "N"
ExperimentOrder(ArrayVar, 5) = "O "
ExperimentOrder(ArrayVar, 6) = "X "
ExperimentOrder(ArrayVar, 7) = "S "
ExperimentOrder(ArrayVar, 8) = "G "
ExperimentOrder(ArrayVar, 9) = "C "

Exit Do
End If

Case "N_I2"
If NearImmediate2OSXGC < ValidNumberVar Then
NearImmediate2OSXGC = NearImmediate2OSXGC + 1

ExperimentOrder(ArrayVar, 1) = ExperimentMode
ExperimentOrder(ArrayVar, 2) = ArrayVar
ExperimentOrder(ArrayVar, 3) = "I2"
ExperimentOrder(ArrayVar, 4) = "N"
ExperimentOrder(ArrayVar, 5) = "O "
ExperimentOrder(ArrayVar, 6) = "S "
ExperimentOrder(ArrayVar, 7) = "X "
ExperimentOrder(ArrayVar, 8) = "G "
ExperimentOrder(ArrayVar, 9) = "C "

Exit Do
End If
End Select
Case 3
Select Case ConditionVar
Case "N_I1"
If NearImmediate1UTGCS < ValidNumberVar Then
NearImmediate1UTGCS = NearImmediate1UTGCS + 1

ExperimentOrder(ArrayVar, 1) = ExperimentMode
ExperimentOrder(ArrayVar, 2) = ArrayVar
ExperimentOrder(ArrayVar, 3) = "I1"
ExperimentOrder(ArrayVar, 4) = "N"
ExperimentOrder(ArrayVar, 5) = "U "
ExperimentOrder(ArrayVar, 6) = "T "
ExperimentOrder(ArrayVar, 7) = "G "
ExperimentOrder(ArrayVar, 8) = "C "
ExperimentOrder(ArrayVar, 9) = "S "

Exit Do
End If

Case "N_I2"
If NearImmediate2UGTCS < ValidNumberVar Then
NearImmediate2UGTCS = NearImmediate2UGTCS + 1

ExperimentOrder(ArrayVar, 1) = ExperimentMode
ExperimentOrder(ArrayVar, 2) = ArrayVar
ExperimentOrder(ArrayVar, 3) = "I2"
ExperimentOrder(ArrayVar, 4) = "N"
ExperimentOrder(ArrayVar, 5) = "U "
ExperimentOrder(ArrayVar, 6) = "G "
ExperimentOrder(ArrayVar, 7) = "T "
ExperimentOrder(ArrayVar, 8) = "C "
ExperimentOrder(ArrayVar, 9) = "S "

Exit Do
End If
End Select

Case 4
Select Case ConditionVar
Case "N_I1"
If NearImmediate1UTGCS < ValidNumberVar Then
NearImmediate1UTGCS = NearImmediate1UTGCS + 1

ExperimentOrder(ArrayVar, 1) = ExperimentMode
ExperimentOrder(ArrayVar, 2) = ArrayVar
ExperimentOrder(ArrayVar, 3) = "I1"
ExperimentOrder(ArrayVar, 4) = "N"
ExperimentOrder(ArrayVar, 5) = "U "
ExperimentOrder(ArrayVar, 6) = "X "
ExperimentOrder(ArrayVar, 7) = "G "
ExperimentOrder(ArrayVar, 8) = "C "
ExperimentOrder(ArrayVar, 9) = "S "

Exit Do
End If

Case "N_I2"
If NearImmediate2UGXCS < ValidNumberVar Then
NearImmediate2UGXCS = NearImmediate2UGXCS + 1

ExperimentOrder(ArrayVar, 1) = ExperimentMode
ExperimentOrder(ArrayVar, 2) = ArrayVar
ExperimentOrder(ArrayVar, 3) = "I2"
ExperimentOrder(ArrayVar, 4) = "N"
ExperimentOrder(ArrayVar, 5) = "U "
ExperimentOrder(ArrayVar, 6) = "G "
ExperimentOrder(ArrayVar, 7) = "X "
ExperimentOrder(ArrayVar, 8) = "C "
ExperimentOrder(ArrayVar, 9) = "S "

Exit Do
End If
End Select

Case 5
Select Case ConditionVar
Case "N_I1"
If NearImmediate1GTCSO < ValidNumberVar Then
NearImmediate1GTCSO = NearImmediate1GTCSO + 1

ExperimentOrder(ArrayVar, 1) = ExperimentMode
ExperimentOrder(ArrayVar, 2) = ArrayVar
ExperimentOrder(ArrayVar, 3) = "I1"
ExperimentOrder(ArrayVar, 4) = "N"
ExperimentOrder(ArrayVar, 5) = "G "
ExperimentOrder(ArrayVar, 6) = "T "
ExperimentOrder(ArrayVar, 7) = "C "
ExperimentOrder(ArrayVar, 8) = "S "
ExperimentOrder(ArrayVar, 9) = "O "

Exit Do
End If
Case "N_I2"
If NearImmediate2GCTSO < ValidNumberVar Then
NearImmediate2GCTSO = NearImmediate2GCTSO + 1

ExperimentOrder(ArrayVar, 1) = ExperimentMode
ExperimentOrder(ArrayVar, 2) = ArrayVar
ExperimentOrder(ArrayVar, 3) = "I2"
ExperimentOrder(ArrayVar, 4) = "N"
ExperimentOrder(ArrayVar, 5) = "G "
ExperimentOrder(ArrayVar, 6) = "C "
ExperimentOrder(ArrayVar, 7) = "T "
ExperimentOrder(ArrayVar, 8) = "S "
ExperimentOrder(ArrayVar, 9) = "O "

Exit Do
End If
End Select

Case 6
Select Case ConditionVar
Case "N_I1"
If NearImmediate1GXCSO < ValidNumberVar Then
NearImmediate1GXCSO = NearImmediate1GXCSO + 1

ExperimentOrder(ArrayVar, 1) = ExperimentMode
ExperimentOrder(ArrayVar, 2) = ArrayVar
ExperimentOrder(ArrayVar, 3) = "I1"
ExperimentOrder(ArrayVar, 4) = "N"
ExperimentOrder(ArrayVar, 5) = "G "
ExperimentOrder(ArrayVar, 6) = "X "
ExperimentOrder(ArrayVar, 7) = "C "
ExperimentOrder(ArrayVar, 8) = "S "
ExperimentOrder(ArrayVar, 9) = "O "

Exit Do
End If

Case "N_I2"
If NearImmediate2GCXSO < ValidNumberVar Then
NearImmediate2GCXSO = NearImmediate2GCXSO + 1

ExperimentOrder(ArrayVar, 1) = ExperimentMode
ExperimentOrder(ArrayVar, 2) = ArrayVar
ExperimentOrder(ArrayVar, 3) = "I2"
ExperimentOrder(ArrayVar, 4) = "N"
ExperimentOrder(ArrayVar, 5) = "G "
ExperimentOrder(ArrayVar, 6) = "C "
ExperimentOrder(ArrayVar, 7) = "X "
ExperimentOrder(ArrayVar, 8) = "S "
ExperimentOrder(ArrayVar, 9) = "O "

Exit Do
End If

End Select

Case Else
Beep
MsgBox "BuildExperimentTrial Invalid LStringVar " & LStringVar, vbExclamation
End
End Select
Loop

Do Until False

CStringVar = GetRandomNumber(6)

Select Case CStringVar
Case 1
Select Case ColourVar
Case "C1"
If Colour1 < ColourNumberVar Then
Colour1 = Colour1 + 1
ExperimentOrder(ArrayVar, 11) = "C1"
ExperimentOrder(ArrayVar, 12) = "PINK"
ExperimentOrder(ArrayVar, 13) = "GREEN"
ExperimentOrder(ArrayVar, 14) = "RED"
ExperimentOrder(ArrayVar, 15) = "AQUA"
ExperimentOrder(ArrayVar, 16) = "PURPLE"
Exit Do
End If
Case "C2"
If Colour7 < ColourNumberVar Then
Colour7 = Colour7 + 1
ExperimentOrder(ArrayVar, 11) = "C2"
ExperimentOrder(ArrayVar, 12) = "PURPLE"
ExperimentOrder(ArrayVar, 13) = "PINK"
ExperimentOrder(ArrayVar, 14) = "GREEN"
ExperimentOrder(ArrayVar, 15) = "RED"
ExperimentOrder(ArrayVar, 16) = "AQUA"
Exit Do
End If
End Select

Case 2
Select Case ColourVar
Case "C1"
If Colour2 < ColourNumberVar Then
Colour2 = Colour2 + 1
ExperimentOrder(ArrayVar, 11) = "C1"
ExperimentOrder(ArrayVar, 12) = "TURQUOISE"
ExperimentOrder(ArrayVar, 13) = "RED"
ExperimentOrder(ArrayVar, 14) = "GREEN"
ExperimentOrder(ArrayVar, 15) = "MAUVE"
ExperimentOrder(ArrayVar, 16) = "BROWN"
Exit Do
End If
Case "C2"
If Colour8 < ColourNumberVar Then
Colour8 = Colour8 + 1
ExperimentOrder(ArrayVar, 11) = "C2"
ExperimentOrder(ArrayVar, 12) = "BROWN"
ExperimentOrder(ArrayVar, 13) = "TURQUOISE"
ExperimentOrder(ArrayVar, 14) = "RED"
ExperimentOrder(ArrayVar, 15) = "GREEN"
ExperimentOrder(ArrayVar, 16) = "MAUVE"
Exit Do
End If
End Select

Case 3
Select Case ColourVar
Case "C1"
If Colour3 < ColourNumberVar Then
Colour3 = Colour3 + 1
ExperimentOrder(ArrayVar, 11) = "C1"
ExperimentOrder(ArrayVar, 12) = "TURQUOISE"
ExperimentOrder(ArrayVar, 13) = "RED"
ExperimentOrder(ArrayVar, 14) = "BLUE"
ExperimentOrder(ArrayVar, 15) = "ORANGE"
ExperimentOrder(ArrayVar, 16) = "MAUVE"
Exit Do
End If
Case "C2"
If Colour9 < ColourNumberVar Then
Colour9 = Colour9 + 1
ExperimentOrder(ArrayVar, 11) = "C2"
ExperimentOrder(ArrayVar, 12) = "MAUVE"
ExperimentOrder(ArrayVar, 13) = "TURQUOISE"
ExperimentOrder(ArrayVar, 14) = "RED"
ExperimentOrder(ArrayVar, 15) = "BLUE"
ExperimentOrder(ArrayVar, 16) = "ORANGE"
Exit Do
End If
End Select

Case 4
Select Case ColourVar
Case "C1"
If Colour4 < ColourNumberVar Then
Colour4 = Colour4 + 1
ExperimentOrder(ArrayVar, 11) = "C1"
ExperimentOrder(ArrayVar, 12) = "LIME"
ExperimentOrder(ArrayVar, 13) = "BLUE"
ExperimentOrder(ArrayVar, 14) = "RED"
ExperimentOrder(ArrayVar, 15) = "AQUA"
ExperimentOrder(ArrayVar, 16) = "ORANGE"
Exit Do
End If
Case "C2"
If Colour10 < ColourNumberVar Then
Colour10 = Colour10 + 1
ExperimentOrder(ArrayVar, 11) = "C2"
ExperimentOrder(ArrayVar, 12) = "ORANGE"
ExperimentOrder(ArrayVar, 13) = "LIME"
ExperimentOrder(ArrayVar, 14) = "BLUE"
ExperimentOrder(ArrayVar, 15) = "RED"
ExperimentOrder(ArrayVar, 16) = "AQUA"
Exit Do
End If
End Select

Case 5
Select Case ColourVar
Case "C1"
If Colour5 < ColourNumberVar Then
Colour5 = Colour5 + 1
ExperimentOrder(ArrayVar, 11) = "C1"
ExperimentOrder(ArrayVar, 12) = "PINK"
ExperimentOrder(ArrayVar, 13) = "GREEN"
ExperimentOrder(ArrayVar, 14) = "BLUE"
ExperimentOrder(ArrayVar, 15) = "BROWN"
ExperimentOrder(ArrayVar, 16) = "MAUVE"
Exit Do
End If
Case "C2"
If Colour11 < ColourNumberVar Then
Colour11 = Colour11 + 1
ExperimentOrder(ArrayVar, 11) = "C2"
ExperimentOrder(ArrayVar, 12) = "MAUVE"
ExperimentOrder(ArrayVar, 13) = "PINK"
ExperimentOrder(ArrayVar, 14) = "GREEN"
ExperimentOrder(ArrayVar, 15) = "BLUE"
ExperimentOrder(ArrayVar, 16) = "BROWN"
Exit Do
End If
End Select
Case 6
Select Case ColourVar
Case "C1"
If Colour6 < ColourNumberVar Then
Colour6 = Colour6 + 1
ExperimentOrder(ArrayVar, 11) = "C1"
ExperimentOrder(ArrayVar, 12) = "LIME"
ExperimentOrder(ArrayVar, 13) = "BLUE"
ExperimentOrder(ArrayVar, 14) = "GREEN"
ExperimentOrder(ArrayVar, 15) = "MAUVE"
ExperimentOrder(ArrayVar, 16) = "ORANGE"
Exit Do
End If
Case "C2"
If Colour12 < ColourNumberVar Then
Colour12 = Colour12 + 1
ExperimentOrder(ArrayVar, 11) = "C2"
ExperimentOrder(ArrayVar, 12) = "ORANGE"
ExperimentOrder(ArrayVar, 13) = "LIME"
ExperimentOrder(ArrayVar, 14) = "BLUE"
ExperimentOrder(ArrayVar, 15) = "GREEN"
ExperimentOrder(ArrayVar, 16) = "ORANGE"
Exit Do
End If
End Select
Case Else
Beep
MsgBox " BuildExperimentTrial Invalid CStringVar " & CStringVar, vbExclamation
End
End Select
Loop

Do Until False
VisFieldVar = GetRandomNumber(6)

Select Case VisFieldVar
Case 1
If AboveL < PositionNumberVar Then
AboveL = AboveL + 1
ExperimentOrder(ArrayVar, 20) = "AL"
End If

Case 2
If AboveM < PositionNumberVar Then
AboveM = AboveM + 1
ExperimentOrder(ArrayVar, 20) = "AM"
End If

Case 3
If AboveR < PositionNumberVar Then
AboveR = AboveR + 1
ExperimentOrder(ArrayVar, 20) = "AR"
End If

Case 4
If BelowL < PositionNumberVar Then
BelowL = BelowL + 1
ExperimentOrder(ArrayVar, 20) = "BL"
End If

Case 5
If BelowM < PositionNumberVar Then
BelowM = BelowM + 1
ExperimentOrder(ArrayVar, 20) = "BM"
End If

Case 6
If BelowR < PositionNumber Then
BelowR = BelowR + 1
ExperimentOrder(ArrayVar, 20) = "BR"
End If

Case Else
Beep
MsgBox "DoTrial Invalid position " & VisFieldVar
End
End Select
Loop

End Sub

Sub ResetExperiment()
'Reset counts

NearImmediate1OTSGC = 0
NearImmediate1OXSGC = 0
NearImmediate1UTGCS = 0
NearImmediate1UXGCS = 0
NearImmediate1GTCSO = 0
NearImmediate1GXCSO = 0

NearImmediate2OSTGC = 0
NearImmediate2OSXGC = 0
NearImmediate2UGTCS = 0
NearImmediate2UGXCS = 0
NearImmediate2GCTSO = 0
NearImmediate2GCXSO = 0

Colour1 = 0
Colour2 = 0
Colour3 = 0
Colour4 = 0
Colour5 = 0
Colour6 = 0
Colour7 = 0
Colour8 = 0
Colour9 = 0
Colour10 = 0
Colour11 = 0
Colour12 = 0

AboveL = 0
AboveM = 0
AboveR = 0
BelowL = 0
BelowM = 0
BelowR = 0

CurrentTrial = 0

End Sub

ok, I'm assuming this is VB6, if you want to send me the code in a zip file, I'll see what I can do with it.... I'm not seeing why there is a problem, since colourx through colourx is globally defined (I'm assuming they are globally defined in a code module). Anyway, send me the project and the files in a zip file, and I'll sift through it, and try to debug it for you.

ok, I'm assuming this is VB6, if you want to send me the code in a zip file, I'll see what I can do with it.... I'm not seeing why there is a problem, since colourx through colourx is globally defined (I'm assuming they are globally defined in a code module). Anyway, send me the project and the files in a zip file, and I'll sift through it, and try to debug it for you.

Hi,

Thanks for this - I have attached the whole project for you and yes, it's VB6.

I think I've managed to attach the file - let me know if I messed it up!

Attachments

The last do of buildexperimenttrial sub requires your attention, and is commented. The others that I could find have been fixed and commented.... let me know how it turns out....

By The Way, The only changes I made where in the module.

Attachments

The last do of buildexperimenttrial sub requires your attention, and is commented. The others that I could find have been fixed and commented.... let me know how it turns out....

By The Way, The only changes I made where in the module.

Thanks for all that. I will make VERY sure in future that I check and recheck my variables! However, as you are being so helpful I wonder if you would help me with timing. I have written a sub procedure to do this as doing it on each form only works for one trial but this doesn't work at all. None of the textbooks seem to address the problem of timing forms each time they show.

Any ideas? I have attached a new zip of my coding which I have changed plus a flow chart of the experimental procedure (order of forms).

Thanks again in anticipation for your help.

This article has been dead for over six months. Start a new discussion instead.