求助 為何無(wú)法在access中插入數(shù)據(jù)
作者:金字塔 來(lái)源:cxh99.com 發(fā)布時(shí)間:2015年03月25日
- 咨詢(xún)內(nèi)容:
求助,下面代碼中新建表的這部分沒(méi)問(wèn)題.但是不知為何不能在access中插入數(shù)據(jù).而如果我建立成功表格后單獨(dú)將下面紅色的一行在access中執(zhí)行的話(huà),是執(zhí)行成功的.請(qǐng)求幫忙解決.
Sub APPLICATION_Start() '設(shè)定access文件'AccessFilePath = "D:\*******\Asset.accdb" AccessFilePath = "D:\藝的文件夾\自編程序\風(fēng)控系列06 金字塔vbs文件\Asset.accdb"
'開(kāi)始在ACCESS數(shù)據(jù)庫(kù)中新建表********************************************************************application.MsgOut "新建Access表"Set adoConn=CreateObject("Adodb.Connection")'連接數(shù)據(jù)庫(kù)(數(shù)據(jù)指向是金字塔安裝目錄下的test.mdb,也可以自己更改想要的路徑)'adoConn.Open"Provider=MicroSoft.Jet.OLEDB.4.0;Data Source=D:\藝的文件夾\自編程序\風(fēng)控系列06 金字塔vbs文件\DailyAsset.mdb"adoConn.Open"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & AccessFilePath '可以連接access2007數(shù)據(jù)庫(kù)格式
'查找表是否存在Dim RsSet Rs = adoConn.OpenSchema(20)Do While Not Rs.EOF If Rs("Table_Name") = "TreadingRecord" Then '找到表的操作。。 strSqldro="drop table TreadingRecord" adoConn.Execute(strSqldro) End If Rs.MoveNextLoopRs.CloseSet Rs = Nothing
'創(chuàng)建表 各列代表的意義分別是 交易時(shí)間,交易品種,交易狀態(tài)(買(mǎi)賣(mài),買(mǎi)空,賣(mài)空,頭寸模型,風(fēng)險(xiǎn)百分比) strSqlcre="create table TreadingRecord (TreadeDateTime DateTime NOT NULL, TreadMarket varchar(10) NOT NULL, TreadeStatus varchar(10) NOT NULL,PositionSizeModle varchar(10) NOT NULL, PercentRisk real)" adoConn.Execute(strSqlcre)adoConn.Close'結(jié)束在ACCESS數(shù)據(jù)庫(kù)中新建表********************************************************************
'開(kāi)始在ACCESS數(shù)據(jù)庫(kù)中插入數(shù)據(jù)********************************************************************
'打開(kāi)數(shù)據(jù)庫(kù) Set adoConn=CreateObject("Adodb.Connection") adoConn.Open"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & AccessFilePath '可以連接access2007數(shù)據(jù)庫(kù)格式
'在access數(shù)據(jù)庫(kù)中寫(xiě)入記錄
strSqlcre="INSERT INTO TreadingRecord VALUES ('2009/1/4','CU13','Buy','FixedPR',.05) "
adoConn.Execute(strSqlcre)
application.MsgOut "SQL語(yǔ)句: "& strSqlcre adoConn.close application.MsgOut "結(jié)束"'結(jié)束在ACCESS數(shù)據(jù)庫(kù)中插入數(shù)據(jù)******************************************************************** End Sub
- 金字塔客服:
解決了 應(yīng)該將函數(shù)名改為Sub APPLICATION_VBAStart()