Results 1 to 8 of 8

Thread: if then else

  1. #1
    Join Date
    Jul 2013
    Posts
    4

    Question Unanswered: if then else

    I tried to find a post to this question, but didn't see anything. If you can point me in the right direction that would be good.

    It's been too long since I've really played in Access. I'm trying to create an IF THEN ELSE statement that pulls text into a report depending on the criteria.

    I want it so that if the course number begins with MATH that it returns the text from the if statement for that course. Or if it starts with IT, etc, etc.

    Code:
    Function DeptSchool()
    If Course = "MATH%" Then
    Text = "Text1"
    ElseIf Course = "IT%" Then
    Text = "Text2"
    End If
    End Function
    When I put =DeptSchool() in the text box in the report it doesn't recognize it. I'm guessing there is something silly and stupid I'm missing, but for the life of me my brain isn't finding it despite multiple searches today. I've also tried several different things on my own. All with no luck.

    If you can help me know what I'm missing that would be great!

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    What's Course, a variable, the name of a control ?
    Have a nice day!

  3. #3
    Join Date
    Nov 2010
    Posts
    84
    Try it now with this code...
    Function DeptSchool()
    If Course = "MATH%" Then
    DeptSchool = "Text1"
    ElseIf Course = "IT%" Then
    DeptSchool = "Text2"
    End If
    End Function

  4. #4
    Join Date
    Jul 2013
    Posts
    4

    Thumbs up thanks!

    Thanks, eremija! That is a lot better. Still needs a little help, but at least I'm not getting an error message.

  5. #5
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    where is this function defined?
    scope should be public by default but it cant help

    Code:
    PUBLIC Function DeptSchool()
    If Course = "MATH%" Then
    DeptSchool = "Text1"
    ElseIf Course = "IT%" Then
    DeptSchool = "Text2"
    End If
    End Function
    except its function which doesn't define a return type
    Code:
    PUBLIC Function DeptSchool() AS STRING
    If Course = "MATH%" Then
    DeptSchool = "Text1"
    ElseIf Course = "IT%" Then
    DeptSchool = "Text2"
    End If
    End Function
    I'm not convinced "MATH%" is right
    Code:
    PUBLIC Function DeptSchool() AS STRING
    dim UCOurse as string
    UCCOurse =UCASE(Course) 'forece course to be upper case
    If UCCourse = "MATH" Then
    DeptSchool = "Text1"
    ElseIf Course = "IT" Then
    DeptSchool = "Text2"
    ELSE
    DeptSchool = UCCourse 'so the function alwasy sets something, can remove once the function is working
    End If
    End Function
    I'd rather be riding on the Tiger 800 or the Norton

  6. #6
    Join Date
    Jul 2013
    Posts
    4
    I did go ahead and try that, but it didn't work for pulling the correct text. I'm pretty sure I need a wild card of some sort. Because MATH is really MATH 105 or MATH 220, etc, etc.

    I also tried "MATH*". Hmmm...I'll get it eventually. I'm way better off now. Thanks again for the help, all!
    Last edited by emerrube; 07-18-13 at 15:27.

  7. #7
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    are you getting something. its frustrating when you write a function that returns nothing.

    so you need to compare part of the course column then use the left function
    eg:-
    if left(UCCourse,4) = "MATH"
    I'd rather be riding on the Tiger 800 or the Norton

  8. #8
    Join Date
    Jul 2013
    Posts
    4
    that helped! it worked. thanks, healdem!

Posting Permissions

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