从access显示到datagrid的代码:
Private Sub Command1_Click()
SQL = "select * from dingdan"
Adodc1.RecordSource = SQL
Set DataGrid1.DataSource = Adodc1
Adodc1.Refresh
OpenDBFile
gCon.Execute SQL
CloseDBFile
End Sub
添加到excel表的代码:
Private Sub Command2_Click()
Dim i As Integer
Dim j As Integer
SQL = "select * from dingan"
OpenDBFile
OpenRS (SQL)
gRst.MoveFirst
Set gX = GetObject("", "excel.application")
gX.Workbooks.Add
For i = 1 To 100
For j = 0 To 1000
DataGrid1.Row = i
DataGrid1.Col = j
xlWorkSheet.Cells(i, j) = DataGrid1.DataSource
Next j
Next i
Unload Me
gX.ActiveWorkbook.SaveAs App.Path & "\dingdan.xls"
gX.Quit
CloseRS
End Sub
FORM代码:
Private Sub form_load()
Dim adoRecordset As ADODB.Recordset
Set adoRecordset = New ADODB.Recordset
Dim i As Integer
Dim j As Integer
For i = 1 To 101
adoRecordset.Fields.Append "第" & i & "列", adVariant
Next i
adoRecordset.Open
For j = 1 To 1001
adoRecordset.AddNew
Next j
Set DataGrid1.DataSource = adoRecordset
adoRecordset.MoveFirst
Adodc1.ConnectionString = "provider=microsoft.jet.oledb.4.0;data source=" _
& App.Path & "\dvdlent.mdb;Mode=ReadWrite;Persist Security Info=True;Jet OLEDB:Database Password=vcdglxt"
Adodc1.CursorLocation = adUseClient
Adodc1.CommandType = adCmdText
End Sub