Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select Case Help 1

Status
Not open for further replies.

Davidwazza

Programmer
Oct 31, 2003
16
AU
Hi, I am a bit of a beginner in VBA, so...

I am writing a database based on students grade. The user inputs their percentage, and the grade is automatically calculated. It prints the grade in an "unbound textbox". How would I go about setting this up? I have the following thus far.

Select Case [intARI]
Case If >= 90
debug.print "HD"
Case 80 <= [intARI] < 90
debug.print &quot;D&quot;
Case 65 <= [intARI] < 80
debug.print &quot;C&quot;
Case 50 <= [intARI] < 65
debug.print &quot;P&quot;
Case Else
debug.print &quot;F&quot;
End Select

How do I link this VBA code to the textbox (named &quot;ARIGrade&quot;), and also is the debug.print correct? Doesnt appear to be doing anything? Like, as an example, I tried putting this in an onClick, and put a MsgBox prompt also, and this worked fine, but the textbox was still empty.

Thanking in advance, David.
 
Your syntax won't work in vb, try this:

Code:
  Select Case [intARI]
   Case Is >= 90
    Me![txtGrade] = &quot;HD&quot;
   Case 80 To 89
    Me![txtGrade] = &quot;D&quot;
   Case 65 To 79
    Me![txtGrade] = &quot;C&quot;
   Case 50 To 64
    Me![txtGrade] = &quot;P&quot;
   Case Else
    Me![txtGrade] = &quot;F&quot;
  End Select

Assumes your textbox is named [tt]txtGrade[/tt]

VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
VBSlammer is correct. Debug.print just displays the result in the &quot;Immediate window&quot; (under view in VBA Editor), so using his code will work!



_________________
Paladine

&quot;Doing what others find difficult is talent.....
 
Thanks for that

Just one quick thing - the cases have been changed from say, for a credit, 65 - 79. What happens if the user puts in 79.3 ? Would this work as a case? Round down?

Thanks again.
 
If you use integer division you'll get rounding down for values of .5 and less, and rounding up for values .5+ and more.
Code:
  Select Case ([intARI] \ 1)    '<-- integer division
   Case Is >= 90
    Me![txtGrade] = &quot;HD&quot;
   Case 80 To 89
    Me![txtGrade] = &quot;D&quot;
   Case 65 To 79
    Me![txtGrade] = &quot;C&quot;
   Case 50 To 64
    Me![txtGrade] = &quot;P&quot;
   Case Else
    Me![txtGrade] = &quot;F&quot;
  End Select



VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
And so hence last thing!

How do I link this code up to my form?

So far, Ive got it in a &quot;current&quot; form function - but it obviously doesnt automatically update once you change it - just only when you enter the form? How would I go about doing this?

So far;

Private Sub Form_Current()

Select Case ([intARI] \ 1) '<-- integer division
Case Is >= 90
Me![txtGrade] = &quot;HD&quot;
Case 80 To 89
Me![txtGrade] = &quot;D&quot;
Case 65 To 79
Me![txtGrade] = &quot;C&quot;
Case 50 To 64
Me![txtGrade] = &quot;P&quot;
Case Else
Me![txtGrade] = &quot;F&quot;
End Select

End Sub

What function then???
 
I'm not sure what you're doing with this value once you calculate it, but I normally make a separate routine for stuff like this so it can be called from multiple places in code:

Code:
Private Sub Form_Current()
  Call UpdateGrade
End Sub

Private Sub txtPoints_AfterUpdate()
  Call UpdateGrade
End Sub

Private Sub UpdateGrade()
    Select Case ([txtPoints] \ 1)
   Case Is >= 90
    Me![txtLetterGrade] = &quot;HD&quot;
   Case 80 To 89
    Me![txtLetterGrade] = &quot;D&quot;
   Case 65 To 79
    Me![txtLetterGrade] = &quot;C&quot;
   Case 50 To 64
    Me![txtLetterGrade] = &quot;P&quot;
   Case Else
    Me![txtLetterGrade] = &quot;F&quot;
  End Select
End Sub


VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top