VB读取ACCESS数据库

来源:百度知道 编辑:UC知道 时间:2024/06/11 13:09:10
Private Sub Command1_Click()
Dim dbs As Database '定义为数据库类型
Dim rst As Recordset '定义为记录类型
Dim theday As Date '定义为日期类型
Dim i As Integer
Dim Bcell, Ecell, SQLString As String

On Error GoTo aa:
Set ExcelReport = New Excel.Application
ExcelReport.Workbooks.Open FileName:=App.Path + "\APP\脱硫系统运行日志.xls"
ExcelReport.DisplayAlerts = False
Set Sheet1 = ExcelReport.Sheets("Sheet1")
Set Sheet2 = ExcelReport.Sheets("Sheet2")
Set Sheet3 = ExcelReport.Sheets("Sheet3")
Set Sheet4 = ExcelReport.Sheets("Sheet4")

Sheet1.Activate
'theday = DateAdd("d", 1, DTPicker1.Value)
Set dbs = OpenDatabase(App.Path + "\APP\TL.mdb")
'dbs.Execute "delete from day1 "
SQLString = "select * from TL1 where DT='" & CStr(DTPicker1.Value) & "'"

dbs.Execute SQLString

Set dbs = OpenDatabase(App.Path + "\APP\TL.mdb")

参数不对:workspace.OpenDatabase(数据库名,打开模式,是否只读,连接字符串)
Set dbs=Workspaces(0).OpenDatabase(App.Path+"\APP\TL.mdb",False,False,"MS Access;PWD=***")

'下面引用ADO2.1
Private Sub Command1_Click()

On Error GoTo aa:
Dim dbs As New Connection '定义为数据库类型
Dim rst As Recordset '定义为记录类型
Dim theday As Date '定义为日期类型
Dim connstr As String
Dim i As Integer
Dim Bcell, Ecell, SQLString As String

Set ExcelReport = New Excel.Application
ExcelReport.Workbooks.Open FileName:=App.Path + "\APP\脱硫系统运行日志.xls"
ExcelReport.DisplayAlerts = False
Set Sheet1 = ExcelReport.Sheets("Sheet1")
Set Sheet2 = ExcelReport.Sheets("Sheet2")
Set Sheet3 = ExcelReport.Sheets("Sheet3")
Set Sheet4 = ExcelReport.Sheets("Sheet4")
Sheet1.Activate