1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > VB读取ACCESS数据库

VB读取ACCESS数据库

时间:2018-10-23 18:08:29

相关推荐

VB读取ACCESS数据库

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 rst = dbs.OpenRecordset("TL1")

If rst.EOF = False Then

rst.MoveFirst

End If

ExcelReport.Visible = True

i = 0

While rst.EOF = False

i = i + 1

Sheet1.Cells(i + 7, 2) = rst!GLFH

Sheet1.Cells(i + 7, 3) = rst!PH

Sheet1.Cells(i + 7, 4) = rst!TFTW

Sheet1.Cells(i + 7, 5) = rst!TFMD

Sheet1.Cells(i + 7, 6) = rst!JT1

Sheet1.Cells(i + 7, 7) = rst!CT1

Sheet1.Cells(i + 7, 8) = rst!JP1

Sheet1.Cells(i + 7, 9) = rst!CP1

Sheet1.Cells(i + 7, 10) = rst!CWSP

Sheet1.Cells(i + 7, 11) = rst!CWXP

Sheet1.Cells(i + 7, 12) = rst!XAI

Sheet1.Cells(i + 7, 13) = rst!XBI

Sheet1.Cells(i + 7, 14) = rst!XCI

Sheet1.Cells(i + 7, 15) = rst!MAI

Sheet1.Cells(i + 7, 16) = rst!MBI

Sheet1.Cells(i + 7, 17) = rst!YAI

Sheet1.Cells(i + 7, 18) = rst!YAP

Sheet1.Cells(i + 7, 19) = rst!YBI

Sheet1.Cells(i + 7, 20) = rst!YBP

Sheet1.Cells(i + 7, 21) = rst!SHAP

Sheet1.Cells(i + 7, 22) = rst!SHBP

Sheet1.Cells(i + 7, 23) = rst!SH_4MIDU

Sheet1.Cells(i + 7, 24) = rst!SGAI

Sheet1.Cells(i + 7, 25) = rst!SGBI

Sheet1.Cells(i + 7, 26) = rst!MFT

Sheet1.Cells(i + 7, 27) = rst!MFP

rst.MoveNext

Wend

ExcelReport.Visible = True

GoTo cc

aa:

ExcelReport.DisplayAlerts = False

Unload Me

cc:

End Sub

执行到 Set dbs = OpenDatabase(App.Path + "\APP\TL.mdb")之后就跳到了ExcelReport.DisplayAlerts = False

请高手们帮我看看我的程序哪里出问题了!!

满意答案

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

theday = Day(CDate(DTPicker1.Value))

connstr = "DBQ=" + App.Path + "\APP\TL.mdb" + ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"

Set dbs = New Connection

dbs.Open connstr

"dbs.Execute "delete from day1 "

SQLString = "select * from TL1 where DT="" & CStr(DTPicker1.Value) & """

dbs.Execute SQLString

Set rst = dbs.Execute("select * from TL1")

If rst.EOF = False Then

rst.MoveFirst

End If

ExcelReport.Visible = True

i = 0

While rst.EOF = False

i = i + 1

Sheet1.Cells(i + 7, 2) = rst!GLFH

Sheet1.Cells(i + 7, 3) = rst!PH

Sheet1.Cells(i + 7, 4) = rst!TFTW

Sheet1.Cells(i + 7, 5) = rst!TFMD

Sheet1.Cells(i + 7, 6) = rst!JT1

Sheet1.Cells(i + 7, 7) = rst!CT1

Sheet1.Cells(i + 7, 8) = rst!JP1

Sheet1.Cells(i + 7, 9) = rst!CP1

Sheet1.Cells(i + 7, 10) = rst!CWSP

Sheet1.Cells(i + 7, 11) = rst!CWXP

Sheet1.Cells(i + 7, 12) = rst!XAI

Sheet1.Cells(i + 7, 13) = rst!XBI

Sheet1.Cells(i + 7, 14) = rst!XCI

Sheet1.Cells(i + 7, 15) = rst!MAI

Sheet1.Cells(i + 7, 16) = rst!MBI

Sheet1.Cells(i + 7, 17) = rst!YAI

Sheet1.Cells(i + 7, 18) = rst!YAP

Sheet1.Cells(i + 7, 19) = rst!YBI

Sheet1.Cells(i + 7, 20) = rst!YBP

Sheet1.Cells(i + 7, 21) = rst!SHAP

Sheet1.Cells(i + 7, 22) = rst!SHBP

Sheet1.Cells(i + 7, 23) = rst!SH_4MIDU

Sheet1.Cells(i + 7, 24) = rst!SGAI

Sheet1.Cells(i + 7, 25) = rst!SGBI

Sheet1.Cells(i + 7, 26) = rst!MFT

Sheet1.Cells(i + 7, 27) = rst!MFP

rst.MoveNext

Wend

ExcelReport.Visible = True

GoTo cc

aa:

ExcelReport.DisplayAlerts = False

dbs.Close

Set dbs = Nothing

Unload Me

cc:

End Sub

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。