2 Replies - 1507 Views - Last Post: 01 April 2011 - 03:41 PM Rate Topic: -----

#1 GrantLyk  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 28
  • Joined: 25-March 11

Compile Error ?

Posted 01 April 2011 - 02:24 AM

Hello, Im having abit of trouble with a certain section of code, because i am trying to Parameter pass however it keeps coming up with a "Compile Error: Only user-defined types defined in public object modules can be coerced to or from a variant or passed to late-bound" Heres the section of code the error appears in;

Type Students
    forename() As String ' Forename as an array going to ten
    course() As Integer ' course as an array going to ten
    exam() As Integer ' exam going as an array going to ten
End Type



Sub main()
Dim allStudents() As Students
Dim total As Integer ' the total of course & exam added
Dim grade As String ' the grades are stored here
Dim buildstring As String ' The final display variable
Dim x As Integer ' x is the number of students

    x = InputBox("How many Students are in your class?")
ReDim allStudents(x) As Students

 For i = 1 To x
    Call enter(allStudents) ' call the enter procedure
    Call Calculate ' call the calculate procedure
    buildstring = buildstring & "Name; " & forename(i) & vbTab & "." & vbCr & "Your course mark; " & course(i) & vbTab & "." & vbCr & "Your exam mark is " & exam(i) & vbTab & "." & vbCr ' use concatonation and the buildstring variable to join all your arrays together
 Next i
    Call display ' display the results
    Call findStudent ' call the find procedure to search for a student

End Sub


The problem seems to highlight the "Call enter(allStudents) Secrtion" heres the whole code,
Type Students
    forename() As String ' Forename as an array going to ten
    course() As Integer ' course as an array going to ten
    exam() As Integer ' exam going as an array going to ten
End Type



Sub main()
Dim allStudents() As Students
Dim total As Integer ' the total of course & exam added
Dim grade As String ' the grades are stored here
Dim buildstring As String ' The final display variable
Dim x As Integer ' x is the number of students

    x = InputBox("How many Students are in your class?")
ReDim allStudents(x) As Students

 For i = 1 To x
    Call enter(allStudents) ' call the enter procedure
    Call Calculate ' call the calculate procedure
    buildstring = buildstring & "Name; " & forename(i) & vbTab & "." & vbCr & "Your course mark; " & course(i) & vbTab & "." & vbCr & "Your exam mark is " & exam(i) & vbTab & "." & vbCr ' use concatonation and the buildstring variable to join all your arrays together
 Next i
    Call display ' display the results
    Call findStudent ' call the find procedure to search for a student

End Sub

Sub enter(Students)
    
    ReDim allStudents(x) As Students
    ReDim forename(x) As String ' Bring in the array forename, x being the number of students inputed as a text input
    ReDim course(x) As Integer ' Bring in the array course, x being the number of students inputed as a numberical input
    ReDim exam(x) As Integer ' Bring in the array exam, x being the number of students inputed as a numberical input
   
    
   
        forename(i) = InputBox("What is your name?") ' take in the name
        course(i) = InputBox("What mark did you gain in your coursework?") ' take in the coursework mark
        exam(i) = InputBox("What mark did you gain in your exam?") ' take in the exam mark
        

End Sub
Sub Calculate()

total(i) = course(i) + exam(i) ' add course & exam to give you total
     
    
    If total(i) >= 70 Then ' If total is greater than or equal to 70 set grade as an A
        grade(i) = "A" ' set there grade as an A
    Else
        If total(i) >= 60 And total(i) <= 69 Then ' check the range of marks to see if its between 60 and 69
            grade(i) = "B" ' Set grade to B
        Else
            If total(i) >= 50 And total(i) <= 59 Then ' check the range of marks to see if its between 50 & 59
                grade(i) = "C" ' set grade to C
            Else
                grade(i) = "Fail" ' everything else set the grade to fail
            End If
        End If
    End If
    
   buildstring = buildstring & "Your grade is " & grade(i) & vbTab & "." & vbCr
    
    
End Sub
Sub display()
  MsgBox (buildstring) ' display the final result
End Sub
Sub findStudent()
Dim willYouSearch As String
Dim pupilSearch As String
Dim i As Integer

willYouSearch = InputBox("Do you wish to search for a Student? Enter Yes to Search & No to not Search")

    If willYouSearch = "Yes" Then
        pupilSearch = InputBox("Enter the Name of the pupil you are searching for")
        For i = 0 To (x - 1)
            If pupilSearch = forename(i) Then
                MsgBox (forename(i) & vbCr & course(i) & vbCr & exam(i) & vbCr & total(i) & vbCr & grade(i))
                Exit Sub
            End If
        Next i
    End If
End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Compile Error ?

#2 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 386
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Compile Error ?

Posted 01 April 2011 - 10:12 AM

Students is a data type and a function or a sub expects an object or a variable as a parameter.
Change your routine signature to
Sub enter(ByRef someStudents as Students)


ByRef is used to send a reference to you variable so you can change it. In enter sub you use someStudent variable and not allStudents. You cannot use allStudents because its scope is inside Main function and all other functions don't have access to it. This is why parameters were required. Another approach would be to change enter to a function that will return an array of Students
function enter() as Students()
dim someVar as Students()
'make your job with the array

enter = someVar
end function


And your main will be like
'something here
dim allStudents as Students
'something here
allStudents = enter()


This post has been edited by AdamSpeight2008: 02 April 2011 - 12:19 AM

Was This Post Helpful? 0
  • +
  • -

#3 BobRodes  Icon User is offline

  • Lovable Curmudgeon
  • member icon

Reputation: 600
  • View blog
  • Posts: 3,069
  • Joined: 19-May 09

Re: Compile Error ?

Posted 01 April 2011 - 03:41 PM

You would get better errors in a situation like this if you had the statement "Option Explicit" at the top of your code. This is important, and I'll explain why. But first, go to Tools/Options in VB and open the Editor tab. You'll see a checkbox called "Require Variable Declaration" there. Check it and hit Ok. This will automatically start all your code with the Option Explicit statement.

Now, what this is saying is that if you try to use a variable that you haven't declared you'll get an error. And you really want that! Most of the time (should be always) if you don't declare a variable it's by accident.

I was going to say that this is a case in point, but it isn't actually. Instead of erasing the above, I'll just point out that it's good general advice and everyone should do it.

However, your line 29 is declaring Students as a Variant, because that's the default variable type. So, what you are actually doing is attempting to "coerce" your Students type into a variant on line 20, which you can't do unless you define it as public in a standard module. Even then, it's not a good idea. Take Ionut's advice. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1