Sorry, I am a beginner at Visual Basic, and have had to create this project as part of a school task. It keeps coming up with "argument not optional" on the calls tp checkcards and determinewinnings. I don't knkow what this means really, and I don't know how to fix it. Can you help??

Here is the code:

Option Explicit

Private Sub cmdDraw_Click()
'subroutine to "draw" the cards

Call GenerateCards      'calling the GenerateCards subroutine
Call CheckCards         'calling the CheckCards subroutine
Call DetermineWinnings  'calling the DetermineWinnings subroutine

End Sub

Private Sub cmdStart_Click()
'subroutine to activate the program when start is clicked

cmdDraw.Enabled = True              'enabling the "Draw" command button

Dim intwinnings As Integer          'declaring the intwinnings variable as an integer
intwinnings = 0                     'setting intwinnings to 0
lblwinnings.Caption = intwinnings   'making the lblwinnings caption 0

Randomize                           'Telling the program to generate random numbers

End Sub

Sub GenerateCards()
'subroutine to generate the random numbers for the variables

Dim intcard1, intcard2, intcard3, intcard4 As Integer 'Declaring the variables as integers

intcard1 = Int(Rnd * 4) + 1     'making the intcard1 variable equal to a random number between 1 & 2
intcard2 = Int(Rnd * 4) + 1     'making the intcard2 variable equal to a random number between 1 & 2
intcard3 = Int(Rnd * 4) + 1     'making the intcard3 variable equal to a random number between 1 & 2
intcard4 = Int(Rnd * 4) + 1     'making the intcard4 variable equal to a random number between 1 & 2

End Sub

Sub CheckCards(intcard1, intcard2, intcard3, intcard4 As Integer)
'subroutine to display image according to the values of the random numbers generated in the GenerateCards subroutine

If intcard1 = 1 Then                                    'Checking if the intcard1 variable is a 1 and if it is, generating a diamond card
    imgcard1.Picture = imgdiamond.Picture
Else
    If intcard1 = 2 Then                                'Checking if the intcard1 variable is a 2 and if it is, generating a hearts card
        imgcard1.Picture = imgheart.Picture
    Else
        If intcard1 = 3 Then
            imgcard1.Picture = imgclub.Picture          'Checking if the intcard1 variable is a 3 and if it is, generating a clubs card
        Else
            If intcard1 = 4 Then
                imgcard1.Picture = imgspade.Picture     'Checking if the intcard1 variable is a 4 and if it is, generating a spades card
            End If
        End If
    End If
End If


If intcard2 = 1 Then                                     'Checking if the intcard2 variable is a 1 and if it is, generating a diamonds card
    imgcard2.Picture = imgdiamond.Picture
Else
    If intcard2 = 2 Then                                 'Checking if the intcard2 variable is a 2 and if it is, generating a hearts card
        imgcard2.Picture = imgheart.Picture
    Else
        If intcard2 = 3 Then                             'Checking if the intcard2 variable is a 3 and if it is, generating a clubs card
            imgcard2.Picture = imgclub.Picture
        Else
            If intcard2 = 4 Then                         'Checking if the intcard2 variable is a 4 and if it is, generating a spades card
                imgcard2.Picture = imgspade.Picture
            End If
        End If
    End If
End If


If intcard3 = 1 Then                                     'Checking if the intcard3 variable is a 1 and if it is, generating a diamonds card
    imgcard3.Picture = imgdiamond.Picture
Else
    If intcard3 = 2 Then                                 'Checking if the intcard3 variable is a 2 and if it is, generating a hearts card
        imgcard3.Picture = imgheart.Picture
    Else
        If intcard3 = 3 Then                             'Checking if the intcard3 variable is a 3 and if it is, generating a clubs card
            imgcard3.Picture = imgclub.Picture
        Else
            If intcard3 = 4 Then                         'Checking if the intcard3 variable is a 4 and if it is, generating a spades card
                imgcard3.Picture = imgspade.Picture
            End If
        End If
    End If
End If


If intcard4 = 1 Then                                     'Checking if the intcard4 variable is a 1 and if it is, generating a diamonds card
    imgcard4.Picture = imgdiamond.Picture
Else
    If intcard4 = 2 Then                                 'Checking if the intcard4 variable is a 2 and if it is, generating a hearts card
        imgcard4.Picture = imgheart.Picture
    Else
        If intcard4 = 3 Then                             'Checking if the intcard4 variable is a 3 and if it is, generating a clubs card
            imgcard4.Picture = imgclub.Picture
        Else
            If intcard4 = 4 Then                         'Checking if the intcard4 variable is a 4 and if it is, generating a spades card
                imgcard4.Picture = imgspade.Picture
            End If
        End If
    End If
End If

End Sub

Sub DetermineWinnings(intcard1, intcard2, intcard3, intcard4, intwinnings As Integer)
'subroutine to calculate, and keep tally of the user's winnings

If intcard1 = intcard2 = intcard3 = intcard4 Then   'Checking the cards for four of the same
    intwinnings = intwinnings + 5       'stating that if a set of four is found, the winner receives $5
    lblwinnings.Caption = intwinnings   'Updating the onscreen total
Else
    If intcard1 = intcard2 = intcard3 Or intcard2 = intcard3 = intcard4 Or intcard1 = intcard2 = intcard4 Or intcard1 = intcard3 = intcard4 Then    'checking the cards for triplets
        intwinnings = intwinnings + 3       'stating that if a triplet is found, the winner receives $3
        lblwinnings.Caption = intwinnings   'updating the onscreen total
    Else
        If intcard1 = intcard2 Then             'checking the cards for pairs
            intwinnings = intwinnings + 1       'stating the if a double is found, the winner receives $1 for each pair
            lblwinnings.Caption = intwinnings   'updating the onscreen total
        End If
        
        
        If intcard2 = intcard3 Then           'checking the cards for pairs
            intwinnings = intwinnings + 1       'stating the if a double is found, the winner receives $1 for each pair
            lblwinnings.Caption = intwinnings   'updating the onscreen total
        End If
        
        
        If intcard3 = intcard4 Then             'checking the cards for pairs
            intwinnings = intwinnings + 1       'stating the if a double is found, the winner receives $1 for each pair
            lblwinnings.Caption = intwinnings   'updating the onscreen total
        End If
        
        
        If intcard1 = intcard3 Then             'checking the cards for pairs
            intwinnings = intwinnings + 1       'stating the if a double is found, the winner receives $1 for each pair
            lblwinnings.Caption = intwinnings   'updating the onscreen total
        End If
            
            
            
        If intcard1 = intcard4 Then         'checking the cards for pairs
            intwinnings = intwinnings + 1       'stating the if a double is found, the winner receives $1 for each pair
            lblwinnings.Caption = intwinnings   'updating the onscreen total
        End If
            
            
        If intcard2 = intcard4 Then           'checking the cards for pairs
            intwinnings = intwinnings + 1       'stating the if a double is found, the winner receives $1 for each pair
            lblwinnings.Caption = intwinnings   'updating the onscreen total
        End If
    End If
End If
        

End Sub

Recommended Answers

All 8 Replies

Sorry, I am a beginner at Visual Basic, and have had to create this project as part of a school task. It keeps coming up with "argument not optional" on the calls tp checkcards and determinewinnings. I don't knkow what this means really, and I don't know how to fix it. Can you help??

Here is the code:

Option Explicit

Private Sub cmdDraw_Click()
'subroutine to "draw" the cards

Call GenerateCards      'calling the GenerateCards subroutine
Call CheckCards         'calling the CheckCards subroutine
Call DetermineWinnings  'calling the DetermineWinnings subroutine

End Sub

Private Sub cmdStart_Click()
'subroutine to activate the program when start is clicked

cmdDraw.Enabled = True              'enabling the "Draw" command button

Dim intwinnings As Integer          'declaring the intwinnings variable as an integer
intwinnings = 0                     'setting intwinnings to 0
lblwinnings.Caption = intwinnings   'making the lblwinnings caption 0

Randomize                           'Telling the program to generate random numbers

End Sub

Sub GenerateCards()
'subroutine to generate the random numbers for the variables

Dim intcard1, intcard2, intcard3, intcard4 As Integer 'Declaring the variables as integers

intcard1 = Int(Rnd * 4) + 1     'making the intcard1 variable equal to a random number between 1 & 2
intcard2 = Int(Rnd * 4) + 1     'making the intcard2 variable equal to a random number between 1 & 2
intcard3 = Int(Rnd * 4) + 1     'making the intcard3 variable equal to a random number between 1 & 2
intcard4 = Int(Rnd * 4) + 1     'making the intcard4 variable equal to a random number between 1 & 2

End Sub

Sub CheckCards(intcard1, intcard2, intcard3, intcard4 As Integer)
'subroutine to display image according to the values of the random numbers generated in the GenerateCards subroutine

If intcard1 = 1 Then                                    'Checking if the intcard1 variable is a 1 and if it is, generating a diamond card
    imgcard1.Picture = imgdiamond.Picture
Else
    If intcard1 = 2 Then                                'Checking if the intcard1 variable is a 2 and if it is, generating a hearts card
        imgcard1.Picture = imgheart.Picture
    Else
        If intcard1 = 3 Then
            imgcard1.Picture = imgclub.Picture          'Checking if the intcard1 variable is a 3 and if it is, generating a clubs card
        Else
            If intcard1 = 4 Then
                imgcard1.Picture = imgspade.Picture     'Checking if the intcard1 variable is a 4 and if it is, generating a spades card
            End If
        End If
    End If
End If


If intcard2 = 1 Then                                     'Checking if the intcard2 variable is a 1 and if it is, generating a diamonds card
    imgcard2.Picture = imgdiamond.Picture
Else
    If intcard2 = 2 Then                                 'Checking if the intcard2 variable is a 2 and if it is, generating a hearts card
        imgcard2.Picture = imgheart.Picture
    Else
        If intcard2 = 3 Then                             'Checking if the intcard2 variable is a 3 and if it is, generating a clubs card
            imgcard2.Picture = imgclub.Picture
        Else
            If intcard2 = 4 Then                         'Checking if the intcard2 variable is a 4 and if it is, generating a spades card
                imgcard2.Picture = imgspade.Picture
            End If
        End If
    End If
End If


If intcard3 = 1 Then                                     'Checking if the intcard3 variable is a 1 and if it is, generating a diamonds card
    imgcard3.Picture = imgdiamond.Picture
Else
    If intcard3 = 2 Then                                 'Checking if the intcard3 variable is a 2 and if it is, generating a hearts card
        imgcard3.Picture = imgheart.Picture
    Else
        If intcard3 = 3 Then                             'Checking if the intcard3 variable is a 3 and if it is, generating a clubs card
            imgcard3.Picture = imgclub.Picture
        Else
            If intcard3 = 4 Then                         'Checking if the intcard3 variable is a 4 and if it is, generating a spades card
                imgcard3.Picture = imgspade.Picture
            End If
        End If
    End If
End If


If intcard4 = 1 Then                                     'Checking if the intcard4 variable is a 1 and if it is, generating a diamonds card
    imgcard4.Picture = imgdiamond.Picture
Else
    If intcard4 = 2 Then                                 'Checking if the intcard4 variable is a 2 and if it is, generating a hearts card
        imgcard4.Picture = imgheart.Picture
    Else
        If intcard4 = 3 Then                             'Checking if the intcard4 variable is a 3 and if it is, generating a clubs card
            imgcard4.Picture = imgclub.Picture
        Else
            If intcard4 = 4 Then                         'Checking if the intcard4 variable is a 4 and if it is, generating a spades card
                imgcard4.Picture = imgspade.Picture
            End If
        End If
    End If
End If

End Sub

Sub DetermineWinnings(intcard1, intcard2, intcard3, intcard4, intwinnings As Integer)
'subroutine to calculate, and keep tally of the user's winnings

If intcard1 = intcard2 = intcard3 = intcard4 Then   'Checking the cards for four of the same
    intwinnings = intwinnings + 5       'stating that if a set of four is found, the winner receives $5
    lblwinnings.Caption = intwinnings   'Updating the onscreen total
Else
    If intcard1 = intcard2 = intcard3 Or intcard2 = intcard3 = intcard4 Or intcard1 = intcard2 = intcard4 Or intcard1 = intcard3 = intcard4 Then    'checking the cards for triplets
        intwinnings = intwinnings + 3       'stating that if a triplet is found, the winner receives $3
        lblwinnings.Caption = intwinnings   'updating the onscreen total
    Else
        If intcard1 = intcard2 Then             'checking the cards for pairs
            intwinnings = intwinnings + 1       'stating the if a double is found, the winner receives $1 for each pair
            lblwinnings.Caption = intwinnings   'updating the onscreen total
        End If
        
        
        If intcard2 = intcard3 Then           'checking the cards for pairs
            intwinnings = intwinnings + 1       'stating the if a double is found, the winner receives $1 for each pair
            lblwinnings.Caption = intwinnings   'updating the onscreen total
        End If
        
        
        If intcard3 = intcard4 Then             'checking the cards for pairs
            intwinnings = intwinnings + 1       'stating the if a double is found, the winner receives $1 for each pair
            lblwinnings.Caption = intwinnings   'updating the onscreen total
        End If
        
        
        If intcard1 = intcard3 Then             'checking the cards for pairs
            intwinnings = intwinnings + 1       'stating the if a double is found, the winner receives $1 for each pair
            lblwinnings.Caption = intwinnings   'updating the onscreen total
        End If
            
            
            
        If intcard1 = intcard4 Then         'checking the cards for pairs
            intwinnings = intwinnings + 1       'stating the if a double is found, the winner receives $1 for each pair
            lblwinnings.Caption = intwinnings   'updating the onscreen total
        End If
            
            
        If intcard2 = intcard4 Then           'checking the cards for pairs
            intwinnings = intwinnings + 1       'stating the if a double is found, the winner receives $1 for each pair
            lblwinnings.Caption = intwinnings   'updating the onscreen total
        End If
    End If
End If
        

End Sub

*******************************************************

where are trying to compare the values of:

If intcard1 = intcard2 = intcard3 = intcard4 ???

if you want to compare the four values try doing this:


if intcard1 = intcard2 and intcard1 = intcard3 and intcard1 = intcard4 then that will be for 4 of the same...(hope i am right)


now for the 3 of the same:

if intcard1 = intcard2 and intcard1 = intcard3 or intcard1 = intcard3 and intcard1 = intcard4 then


hope that one works for you...good luck.

'argument not optional' basically just means 'you can't do it like this'.

For all these comparisons, you could use other methods such as Select Case, or loops within loops, to make the code easier to read, but of course that comes with experience.

One thing I think I should point out is, when you declare several variables on the same line, you need to state the variable type for each one. If the VarType is not stated, it will default to the Variant type.
e.g.

Dim intCard1, intCard2, intCard3, intCard4 As Integer .....incorrect

Dim intCard1 As Integer, intCard2 As Integer, intCard3 As Integer, intCard4 As Integer......Correct

if you are getting this where you say, it means that checkcards wants a variable sent to it:

Call CheckCards   int1,int2, int3 ,int4 'calling the CheckCards subroutine
Call DetermineWinnings   int1,int2, int3 ,int4 'calling the DetermineWinnings subroutine

you may want to create a global variable for the cards so you won't have to worry about passing parameters to subs.

Global intcard1 as integer
global  intcard2 as integer
global intcard3 as integer
global intcard4 As Integer

or you may want to declare the variables as integer in the sub

Sub CheckCards(intcard1 As Integer, intcard2 As Integer, intcard3 As Integer, intcard4 As Integer)

Yes, if these variables are not used in any other forms it would be simpler to declare them with the Dim statement directly after 'Option Explicit', instead of inside the sub.
As far as I know, if you declare a variable inside a sub, it is not recognised anywhere else, even if you use Global or Public. Someone please correct me if I'm wrong !

*Nods*

jwshepherd is hitting real close to home with that. You should take his advice.

A global variable has to be defined in a code module. A local variable gets dimensioned within the sub, and it's "scope" only extends within the sub. A Public Variable gets declared within the declarations section of the form, and it's scope is all procedures in the form. So, if you need a temporary variable, that only needs to be used in one procedure... use dim in that procedure. If you need a variable that the form can use... say, a variable that an OK and Cancel button can both have access to, then you would need a public/form-wide variable. If you want one that the entire project can use (spanning multiple forms) then you need a global variable (declared in a module).

Yes Comatose is correct.

If int1 is declared in a sub, it's value can only be used in that sub. ONce the program leaves that sub, the value is erased.

I forgot to tell you to add a module to your project to add the global variables.

Good luck

You can add all kinds of great stuff in code modules too. You can have functions and subs accessible by the entire program, along with variables, constants, type declarations, and API calls. As soon as I start a new project, I add a code module (it's actually a standard module I built so that VB can use certain subs and functions that come in other language [such as push and pop]) right off the bat, so that I know I at least have one...

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.