Page 1 of 2 12 LastLast
Results 1 to 15 of 27

Thread: Call Code

  1. #1
    Join Date
    Sep 2003
    Location
    Washington, DC
    Posts
    116

    Unanswered: Call Code

    Help!! Here's the code:

    Private Sub AnswerID_Click()
    Dim AnswerID As TAns
    Call EnterAnswer(TAns)

    End Sub

    And the error is:

    User-defined type not defined.

    What does that mean?

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    Private Sub AnswerID_Click()
    Dim AnswerID As TAns
    Call EnterAnswer(TAns)
    you have expressly defined answerID as type TAns, which is not a VB variable type. So the compiler is politely saying it doesn't know what type TAns is.

    The code is valid, you could have defined a type TAns and refer to it here

    So to resolve either define what type TAns, using ther type construct or chaneg the type decalration to something such as long, integer or whatever type will handle the designed range of values....

  3. #3
    Join Date
    Sep 2003
    Location
    Washington, DC
    Posts
    116
    TAns is already defined as an integer in the private function that this sub calls (EnterAnswer).

    Private Function EnterAnswer(TAns As Integer)

    Any other ideas?

  4. #4
    Join Date
    Feb 2005
    Location
    England.
    Posts
    232
    TAns needs to be declared in your on_click event, else it doesn't exist at this stage.

    You could, if you wished, publicly declare TAns as an integer, but would suggest doing it on the sub you need.

    Hope it helps.

  5. #5
    Join Date
    Sep 2003
    Location
    Washington, DC
    Posts
    116
    It would, but I've never done a public sub before. I tried this, but I'm still getting an error in my on click event. I know this is probably wrong, but I gave it a shot!!

    Public Sub TAns()
    Dim TAns As Integer
    End Sub

  6. #6
    Join Date
    Feb 2005
    Location
    England.
    Posts
    232
    Sorry I think I may have been confusing there try this,

    Public TAns As Integer

    Put this at the top of the code, beneath

    Option Compare Database

    Try that and see what happens.

  7. #7
    Join Date
    Sep 2003
    Location
    Washington, DC
    Posts
    116
    I still get that user-defined type not defined error.

  8. #8
    Join Date
    Sep 2003
    Location
    Washington, DC
    Posts
    116
    Here's a copy of the whole code.

    Option Compare Database
    Public TAns As Integer


    Private Function EnterAnswer(TAns As Integer)
    Dim myCount As Integer

    myCount = DCount("REmployeeID", "tbResults", "((REmployeeID = " & [qzEmployeeID] & ") AND (RSessionID = " & [qzSessionID] & ") AND (RQuestionID = " & [qzQuestionID] & "))")

    If myCount > 0 Then
    DoCmd.SetWarnings False
    If MsgBox("You have already answered this question. Do you want to change your answer?" _
    , vbYesNo + vbQuestion, "Your Title") = vbYes Then
    DoCmd.RunSQL "UPDATE tbResults " & _
    "SET RAnswerID = " & TAns & _
    " WHERE REmployeeID = " & [qzEmployeeID] & " AND RSessionID = " & [qzSessionID] & " AND RQuestionID = " & [QuestionID] & ";"
    DoCmd.SetWarnings True
    End If
    Else
    DoCmd.SetWarnings False
    DoCmd.RunSQL "INSERT INTO tbResults (REmployeeID, RSessionID, RQuestionID, RAnswerID ) " & _
    "SELECT " & [qzEmployeeID] & " AS Expr1, " & [qzSessionID] & " AS Expr2, " & [QuestionID] & " AS Exp3, " & TAns & " AS Exp4 ;"
    DoCmd.SetWarnings True
    End If

    End Function


    Private Sub AnswerText_Click()
    Dim AnswerID As TAns
    Call EnterAnswer(TAns)

    End Sub

  9. #9
    Join Date
    Jan 2004
    Location
    Aberdeen, Scotland
    Posts
    1,067
    a few questions
    are you running the function to return a value i.e. into a variable if so you should be calling it like this
    Code:
    dim x as integer
    x = 1
    AnswerID = EnterAnswer(x)
    this will return whatever EnterAnswer calcculates to

    if you are using a sub you should call your sub either by
    Call EnterAnswer(x)
    or
    EnterAnswer x

    where x is a predefined integer as your function is looking for an integer

    Dave

  10. #10
    Join Date
    Sep 2003
    Location
    Washington, DC
    Posts
    116
    The problem with doing it that way is that the AnswerID varies. Basically this db proctors multiple tests. I had to set it up to accomodate multiple questions per exam, and multiple answers per question. AnswerID is the PK for tbAnswers (AnswerID, AQuestionID, AnsLetter (A, B, C or D), AnsText). There are four AnswerID's per AQuestionID. I'd like to be able to enter the AnswerID instead of a value. Is this possible?

  11. #11
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    I fear you are getting mixed up with sub routines and functions
    a function can return a value (or multiple values if the return type is a composite structure such as a userdefined type)


    private function GetAnswerID(<incoming parameter>) as <outgoing type>
    compared to
    private sub GetAnswerID(<incoming parameter>)

    in both cases unless you use the byvalue predicate on the incoming parameter changes to the <incoming parameter(s)> within the function will be returned to the calling program.

    If you want TAns to be visible through out your project then you need to defien the type (TNas) as a private type within the form or report, OR a public type within a code module (so that the type is visible to all forms & reports).

    So in your original posting you needed to define TAns in the global declarations section of thast form, OR change the datatype to soemthing like integer, AND make sure that the return from the function/sub EnterAns usies the same datatype

    A suggestion:
    Code:
    Private Sub AnswerID_Click()
    Dim AnswerID As TAns
    Call EnterAnswer(TAns)
    End Sub
    becomes
    Code:
    Private Sub AnswerID_Click()
    Dim TAns As Integer
    Call EnterAnswer(TAns)
    .....some other code
    
    private sub EnterAnswer(intAnswerID as integer)
    ....some more code
    end sub
    End Sub

  12. #12
    Join Date
    Jan 2004
    Location
    Aberdeen, Scotland
    Posts
    1,067
    just a thought you should define what tans is before calling your sub
    as it doesnt look like its actually passing a value accross as im sure tans is to take on a value and your sql will not work as expected and youll need to look at your table and loop through the answerid's

  13. #13
    Join Date
    Sep 2003
    Location
    Washington, DC
    Posts
    116
    Alright, I'm a bit lost here. Maybe I'll just revert back to declaring a value and figure out how to make everything else work based on that.

  14. #14
    Join Date
    Jan 2004
    Location
    Aberdeen, Scotland
    Posts
    1,067
    Have a look here
    replacing the sql to fill the recordeset with something like

    SELECT answerid FROM tablename

    now instead of this
    Code:
    If Not rs Is Nothing Then
            lngCount = rs.Fields(0)
        End If
        
        Debug.Print lngCount
    put something like
    Code:
    With rs
            .MoveFirst
            Do While Not .EOF
                tans = rs.Fields("AnswerId")
                Call EnterAnswer(tans)
                .MoveNext
            Loop
        end with
    and have a play from there (make sure you reference ADO before you start)

  15. #15
    Join Date
    Sep 2003
    Location
    Washington, DC
    Posts
    116
    Here's the history on how difficult this has been to put together (or rather how inexperienced I am):

    http://www.dbforums.com/t1060620.html
    http://www.dbforums.com/t1068424.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •