2016-07-28 77 views
0

而不是硬編碼MS Access VBA中的分數和等級,我怎麼能將值放在一個表中,並從表中訪問它們?這樣,當分數範圍發生變化時,我不必回到vba代碼並更新它。我只是更新表格中的值,代碼只會提取分數範圍,並在該字段上分配正確的分數。請幫助...更改IF其他條件值從表

Private Sub MarkScored_Exit(Cancel As Integer) 
Dim mScore As Integer 

mScore = Nz(Forms![frmAssessmentDetails]![subfrmAcademicAssessment]![MarkScored]) 

If mScore >= 0 And mScore <= 10 Then 
    Forms![frmAssessmentDetails]![subfrmAcademicAssessment]![cboGradeCode] = "VLA" 
ElseIf mScore > 10 And mScore <= 15 Then 
    Forms![frmAssessmentDetails]![subfrmAcademicAssessment]![cboGradeCode] = "LA" 
ElseIf mScore > 15 And mScore <= 20 Then 
    Forms![frmAssessmentDetails]![subfrmAcademicAssessment]![cboGradeCode] = "S" 
ElseIf mScore > 20 And mScore <= 25 Then 
    Forms![frmAssessmentDetails]![subfrmAcademicAssessment]![cboGradeCode] = "HA" 
ElseIf mScore > 25 And mScore <= 30 Then 
    Forms![frmAssessmentDetails]![subfrmAcademicAssessment]![cboGradeCode] = "VHA" 
Else 
    MsgBox "Please enter valid mark for Score between 0 and 30", vbOKCancel, "Invalid Number" 

End If 

末次

+0

的1列我刪除了SQL Server的標籤,因爲這似乎是一個VBA訪問的問題。 –

回答

2

我建議建立在數據庫中的新表,像

|ID| gradeCode | fromScore | toScore| 
0 VLA   0   10 
1 LA   11   15 
2 S   16   20 

然後在代碼

Select gradeCode from <gradeCodeTableName> where mScore between fromScore and toScore 

查詢它的東西編輯:正如古斯塔夫建議,更好的方式來做到這一點而不浪費資源是創建像

|ID| gradeCode | fromScore | 
0 VLA   0   
1 LA   11   
2 S   16   

和查詢它像

Select Top 1 gradeCode From <gradeCodeTableName> Where mScore >= fromScore Order By fromScore Desc 

您將節省數據

+0

您只需要_fromScore_,因爲toScore是多餘的:'Select Select Top 1 gradeCode From where mScore> = fromScore Order By fromScore Desc' – Gustav

+0

嗯,沒有想到這樣,謝謝你提高我的答案! @Gustav –

+0

謝謝Hynek和Gustav ..你的答案爲我工作..很欣賞..我是一個新手,這是增加了我的學習..謝謝你多爲你的幫助.. – User9123