VB向ACCESS写数据,并输出到EXCEL上
串口通信得到的数据,这里也可以用随机值来代替,知道里花样百出的,也没太看明白,有没有快捷一点的方法?因为这是我程序里的一个很小的部分,谢谢...
串口通信得到的数据,这里也可以用随机值来代替,
知道里花样百出的,也没太看明白,有没有快捷一点的方法?因为这是我程序里的一个很小的部分,谢谢 展开
知道里花样百出的,也没太看明白,有没有快捷一点的方法?因为这是我程序里的一个很小的部分,谢谢 展开
展开全部
首先要引用"Microsoft ActiveX Data Objects 2.x Library"和"Microsoft Excel Object 11.0 Library"
Private Sub Form_Load()
Dim CN As ADODB.Connection
Dim EA As Excel.Application
Dim tSql As String
Dim I As Integer
Dim V As Single
'初始化MDB连接
Set CN = New ADODB.Connection
CN.Open "Driver={Microsoft Access Driver (*.mdb)};Dbq=D:\Test.mdb;Uid=Admin;Pwd="
'初始化XLS连接
Set EA = New Excel.Application
EA.Workbooks.Open "D:\Test.xls"
With EA.Sheets("Sheet1")
.Cells(1, 1) = "ID"
.Cells(1, 2) = "Val"
End With
'随机写100条记录到MDB与XLS中
For I = 1 To 100
Randomize
V = Rnd * 100
tSql = "Insert Into TestData(ID,Val) Values(" & I & "," & V & ")"
CN.Execute tSql
EA.Sheets("Sheet1").Cells(I + 1, 1) = I
EA.Sheets("Sheet1").Cells(I + 1, 2) = V
Next I
'提交事务
CN.Execute "Commit"
'保存表格
EA.SaveWorkspace
EA.Application.Quit
'关闭连接
Set EA = Nothing
Set RS1 = Nothing
End Sub
Private Sub Form_Load()
Dim CN As ADODB.Connection
Dim EA As Excel.Application
Dim tSql As String
Dim I As Integer
Dim V As Single
'初始化MDB连接
Set CN = New ADODB.Connection
CN.Open "Driver={Microsoft Access Driver (*.mdb)};Dbq=D:\Test.mdb;Uid=Admin;Pwd="
'初始化XLS连接
Set EA = New Excel.Application
EA.Workbooks.Open "D:\Test.xls"
With EA.Sheets("Sheet1")
.Cells(1, 1) = "ID"
.Cells(1, 2) = "Val"
End With
'随机写100条记录到MDB与XLS中
For I = 1 To 100
Randomize
V = Rnd * 100
tSql = "Insert Into TestData(ID,Val) Values(" & I & "," & V & ")"
CN.Execute tSql
EA.Sheets("Sheet1").Cells(I + 1, 1) = I
EA.Sheets("Sheet1").Cells(I + 1, 2) = V
Next I
'提交事务
CN.Execute "Commit"
'保存表格
EA.SaveWorkspace
EA.Application.Quit
'关闭连接
Set EA = Nothing
Set RS1 = Nothing
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |