Private Sub Command3_Click()Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
conn.ConnectionString = "dsn=hgsql;uid=sa;pwd=o4w5n6;database=hgprd"
conn.Open
str = "select * from shouli where 受理日期 between " & Trim(Text1.Text) & "and " & Trim(Text2.Text) & "order by 受理日期"
rs.CursorLocation = adUseClient
rs.Open str, conn, adOpenDynamic, adLockOptimistic
Dim strSource, strDestination As StringstrSource = App.Path & "\1.xls"
strDestination = App.Path & "\2.xls"
FileCopy strSource, strDestination
Dim xlapp As Excel.Application
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Set xlapp = CreateObject("Excel.Application")
xlapp.Visible = FalseSet xlbook = xlapp.Workbooks.Open(strDestination)
Set xlsheet = xlbook.Worksheets(1)
rs.MoveFirst
If IsNull(rs!保单号) = False Then
xlsheet.Cells(1, 1) = rs!保单号
End If
If IsNull(rs!业务员) = False Then
xlsheet.Cells(1, 2) = rs!业务员
End If
xlbook.Savexlapp.Quit
End Sub
每次都只能保存第一行的记录,如果将所有的记录全部保存xlsheet.cells()将如何写

解决方案 »

  1.   

    Private Sub Command3_Click()Set conn = New ADODB.Connection
    Set rs = New ADODB.Recordset
    conn.ConnectionString = "dsn=hgsql;uid=sa;pwd=o4w5n6;database=hgprd"
    conn.Open
    str = "select * from shouli where 受理日期 between " & Trim(Text1.Text) & "and " & Trim(Text2.Text) & "order by 受理日期"
    rs.CursorLocation = adUseClient
    rs.Open str, conn, adOpenDynamic, adLockOptimistic
    Dim strSource, strDestination As StringstrSource = App.Path & "\1.xls"
    strDestination = App.Path & "\2.xls"
    FileCopy strSource, strDestination
    Dim xlapp As Excel.Application
    Dim xlbook As Excel.Workbook
    Dim xlsheet As Excel.Worksheet
    dim i as integer
    Set xlapp = CreateObject("Excel.Application")
    xlapp.Visible = FalseSet xlbook = xlapp.Workbooks.Open(strDestination)
    Set xlsheet = xlbook.Worksheets(1)
    rs.MoveFirst
    do while not rs.eof
    i=i+1
    If IsNull(rs!保单号) = False Then
    xlsheet.Cells(i, 1) = rs!保单号
    End If
    If IsNull(rs!业务员) = False Then
    xlsheet.Cells(i, 2) = rs!业务员
    End If
    loopxlbook.Savexlapp.Quit
    End Sub
      

  2.   

    dim N as long
    rs.MoveFirst
    rs.movelast
    N=rs.recordcountfor i =1 to N
    If IsNull(rs!保单号) = False Then
    xlsheet.Cells(i, 1) = rs!保单号
    End If
    If IsNull(rs!业务员) = False Then
    xlsheet.Cells(i, 2) = rs!业务员
    End If
    next N
      

  3.   

    do while not rs.eof
        i=i+1
        If IsNull(rs!保单号) = False Then
           xlsheet.Cells(i, 1) = rs!保单号
        End If
        If IsNull(rs!业务员) = False Then
           xlsheet.Cells(i, 2) = rs!业务员
        End If
        rs.MoveNext
    loop
      

  4.   

    dim N as long
    rs.MoveFirst
    rs.movelast
    N=rs.recordcountfor i =1 to N
    If IsNull(rs!保单号) = False Then
    xlsheet.Cells(i, 1) = rs!保单号
    End If
    If IsNull(rs!业务员) = False Then
    xlsheet.Cells(i, 2) = rs!业务员
    End If
    next N
    错误提示“无效的NEXT空间变量引用”