这是我写的,导出的数据在EXCEL中显示的时候~出现了类型不匹配的情况,比如体育教师会出现高数的 课程,小弟 崩溃中,求 各位了 ,能不能帮我把 错误的 代码 指出来。谢了~在线等答案。。
Option Explicit
Dim i As Integer
Dim j As Integer
Dim C As StringDim rSubject As New ADODB.Recordset     '课程信息的记录集
Dim rClassTime As New ADODB.Recordset   '上课时间的记录集
Dim rTeacher As New ADODB.Recordset     '教师信息的记录集
Dim rAddress As New ADODB.Recordset     '地址信息的记录集'相对应的四个SQL查询语句
Dim SQL1 As String
Dim SQL2 As String
Dim SQL3 As String
Dim SQL4 As String'以下注释掉的代码是SQL Server中的交驻表查询代码
'select class,weekid,
'    max(CASE [id] WHEN 1 then subject end) as one,
'    max(CASE [id] WHEN 2 then subject end) as two,
'    max(CASE [id] WHEN 3 then subject end) as three,
'    max(CASE [id] WHEN 4 then subject end) as four,
'    max(CASE [id] WHEN 5 then subject end) as five,
'    max(CASE [id] WHEN 6 then subject end) as six,
'    max(CASE [id] WHEN 7 then subject end) as seven,
'    max(CASE [id] WHEN 8 then subject end) as eight,
'    max(CASE [id] WHEN 9 then subject end) as nine,
'    max(CASE [id] WHEN 10 then subject end) as ten
'From Syllabus
'Where Len(District) = 0
'group by teacher,weekid
'order by teacher,weekid'所有
Private Function MadeSQL1(strName As String) As String
    '//MadeSQL1 = "select class,weekid,"
    MadeSQL1 = "select teacher,weekid,"           '//
    MadeSQL1 = MadeSQL1 & "max(case [id] when 1 then " & strName & " end) as one,"
    MadeSQL1 = MadeSQL1 & "max(case [id] when 2 then " & strName & " end) as two,"
    MadeSQL1 = MadeSQL1 & "max(case [id] when 3 then " & strName & " end) as three,"
    MadeSQL1 = MadeSQL1 & "max(case [id] when 4 then " & strName & " end) as four,"
    MadeSQL1 = MadeSQL1 & "max(case [id] when 5 then " & strName & " end) as five,"
    MadeSQL1 = MadeSQL1 & "max(case [id] when 6 then " & strName & " end) as six,"
    MadeSQL1 = MadeSQL1 & "max(case [id] when 7 then " & strName & " end) as seven,"
    MadeSQL1 = MadeSQL1 & "max(case [id] when 8 then " & strName & " end) as eight,"
    MadeSQL1 = MadeSQL1 & "max(case [id] when 9 then " & strName & " end) as nine,"
    MadeSQL1 = MadeSQL1 & "max(case [id] when 10 then " & strName & " end) as ten"
    MadeSQL1 = MadeSQL1 & " from syllabus group by teacher,weekid order by teacher,weekid"
End Function'其他
Private Function MadeSQL2(strName As String) As String
    MadeSQL2 = "select teacher,weekid,"
    MadeSQL2 = MadeSQL2 & "max(case [id] when 1 then " & strName & " end) as one,"
    MadeSQL2 = MadeSQL2 & "max(case [id] when 2 then " & strName & " end) as two,"
    MadeSQL2 = MadeSQL2 & "max(case [id] when 3 then " & strName & " end) as three,"
    MadeSQL2 = MadeSQL2 & "max(case [id] when 4 then " & strName & " end) as four,"
    MadeSQL2 = MadeSQL2 & "max(case [id] when 5 then " & strName & " end) as five,"
    MadeSQL2 = MadeSQL2 & "max(case [id] when 6 then " & strName & " end) as six,"
    MadeSQL2 = MadeSQL2 & "max(case [id] when 7 then " & strName & " end) as seven,"
    MadeSQL2 = MadeSQL2 & "max(case [id] when 8 then " & strName & " end) as eight,"
    MadeSQL2 = MadeSQL2 & "max(case [id] when 9 then " & strName & " end) as nine,"
    MadeSQL2 = MadeSQL2 & "max(case [id] when 10 then " & strName & " end) as ten"
    MadeSQL2 = MadeSQL2 & " from syllabus where len(district)=0 group by teacher,weekid order by teacher,weekid"
End Function'条件
Private Function MadeSQL3(strName As String) As String
    MadeSQL3 = "select teacher,weekid,"
    MadeSQL3 = MadeSQL3 & "max(case [id] when 1 then " & strName & " end) as one,"
    MadeSQL3 = MadeSQL3 & "max(case [id] when 2 then " & strName & " end) as two,"
    MadeSQL3 = MadeSQL3 & "max(case [id] when 3 then " & strName & " end) as three,"
    MadeSQL3 = MadeSQL3 & "max(case [id] when 4 then " & strName & " end) as four,"
    MadeSQL3 = MadeSQL3 & "max(case [id] when 5 then " & strName & " end) as five,"
    MadeSQL3 = MadeSQL3 & "max(case [id] when 6 then " & strName & " end) as six,"
    MadeSQL3 = MadeSQL3 & "max(case [id] when 7 then " & strName & " end) as seven,"
    MadeSQL3 = MadeSQL3 & "max(case [id] when 8 then " & strName & " end) as eight,"
    MadeSQL3 = MadeSQL3 & "max(case [id] when 9 then " & strName & " end) as nine,"
    MadeSQL3 = MadeSQL3 & "max(case [id] when 10 then " & strName & " end) as ten"
    MadeSQL3 = MadeSQL3 & " from syllabus where left(teacher,6)='" & Trim(cboOut.Text)
    MadeSQL3 = MadeSQL3 & "' group by teacher,weekid order by teacher,weekid"
End FunctionPrivate Sub cmdOut_Click()
    cmdOut.Enabled = False
    Screen.MousePointer = 11
    
    '根据选择类型生成四个SQL语句
    Select Case Trim(cboOut.Text)
    Case "全部"
        SQL1 = MadeSQL1("subject")
        SQL2 = MadeSQL1("classtime")
        SQL3 = MadeSQL1("class")
        SQL4 = MadeSQL1("address")
    Case "其他"
        SQL1 = MadeSQL2("subject")
        SQL2 = MadeSQL2("classtime")
        SQL3 = MadeSQL2("class")
        SQL4 = MadeSQL2("address")
    Case Else
        SQL1 = MadeSQL3("subject")
        SQL2 = MadeSQL3("classtime")
        SQL3 = MadeSQL3("class")
        SQL4 = MadeSQL3("address")
    End Select