conn.Execute "insert into studentclass(学号,课程号,成绩)" + _
 "values('" + combostudentid.Text + _
 "','" + txtclassid.Text + _
 "','" + txtscore.Text + _
 "')"
这是一条插入的SQL语句,现在要改变成:当选修某一课程(号)的学生超过20人时,就不能再选修了(也就不是插入了),应该如何改?谢谢
if (select count(distinct 学号) from studentclass where 课程号= 'txtclassid.Text' )<=20 then 上面的语句
else msgbox "error!"
这么可以吗?总发现在where语句那里出问题,那些引号啊真烦人!只剩14分了,呵呵!

解决方案 »

  1.   

    sql语句怎么能和VB的判断语句放在一起,改成:
    rs.open "select count(distinct 学号) from studentclass where 课程号='" & txtclassid.Text & "'",conADOif rs(0)>20 then
      ......
    else
      msgbox "error"
    end if
      

  2.   

    rs.open "select count(学号) from studentclass where 课程号='" & txtclassid.Text & "' group by 学号",conADOif rs(0)>20 then
      ......
    else
      msgbox "error"
    end if
      

  3.   

    //不会是因为分少,没人帮忙吧
    当然不是!你在插入之前首先检查一下
        Dim rsTemp As ADODB.Recordset
        Dim intCount As Integer
        Dim strSQL As String
        
        strSQL = "select Count(课程号) from studentclass" _
                & " where 课程号='" & txtclassid.Text & "'"
        Set rsTemp = New ADODB.Recordset
        rsTemp.Open strSQL, conn, adOpenStatic, adLockOptimistic
        intCount = rsTemp(0)
        rsTemp.Close
        If intCount >= 20 Then
            MsgBox "该课程的选修人数已达到最大,不能再选修!", vbInformation, "提示"
            Exit Sub
        End If    '如果程序运行到这里,说明没有超过20个人,就可以添加了。
        conn.Execute "insert into studentclass(学号,课程号,成绩)" + _
     "values('" + combostudentid.Text + _
     "','" + txtclassid.Text + _
     "','" + txtscore.Text + _
     "')"
      

  4.   

    和和,偶上面搞错了,查询语句应该是:
        strSQL = "select Count(学号) from studentclass" _
                & " where 课程号='" & txtclassid.Text & "'"
      

  5.   

    还有,连接字符串最好用&,而不是+.
    个人意见!
      

  6.   

    提问:用+和用&有什么区别?