我在excel宏中,需要对数据库进行查询,并粘贴到当前excel表中。但是,一个成功了,一个却不成功。不成功的,我不明白是错在什么地方,您能帮我看看是怎么回事吗? 1. 下面这段代码是成功的。查询的是DATE段,该段是2位数字(就是日期),格式为文本。功能是查询每一天的数据。 For i = StartDay To EndDay
If Len(i) = 1 Then
iDay = "0" & i
Else
iDay = i
End If
Workbooks.Add
Text1 = "ODBC;DSN=MS Access Database;DBQ=" & InDir & InFileName & ".mdb" & ";DefaultDir=" & InDir & ";DriverId=281;FIL=MS Access;MaxBufferSize=2048;PageTi"
Text2 = "SELECT base.DATE, base.TIME, base.BSC, base.SITE, base.SECTOR, base.data0, base.data1, base.data2, base.data3, base.data4, base.data5, base.data6"
text3 = "FROM `" & InDir & InFileName & "`.base base"
text4 = "WHERE (base.DATE='" & iDay & "')"
With ActiveSheet.QueryTables.Add(Connection:=Array(Array(Text1), Array("meout=5;")), Destination:=Range("A1"))
.CommandText = Array(Text2 & Chr(13) & Chr(10) & text3 & Chr(13) & Chr(10) & text4)
.Name = "查询来自 MS Access Database"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
next 2. 这段是不成功的,执行到红色字这一行出错。错误代码:13,错误信息:类型不匹配。查询的是sDate,该段内容形如2005-3-1,格式为文本。变量StartDay与EndDay来自excel单元格,内容就是如2005-3-1。功能同样也是要查询每一天的数据。 For i = StartDay To EndDay
iTime = i
Text1 = "ODBC;DSN=MS Access Database;DBQ=" & InDir & InFileName & ".mdb" & ";DefaultDir=" & InDir & ";DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTi"
Text2 = "SELECT sidecell.nID, sidecell.sRemark, sidecell.nBlock, sidecell.sDate, sidecell.sBusytime, sidecell.fSucceedPer, sidecell.fTCHLostPer, sidecell.fChangePer, sidecell.fSDCCH, sidecell.fTCH, sidecell.fTCHPower"
text3 = "FROM `" & InDir & InFileName & "`.sidecell sidecell"
text4 = "WHERE (sidecell.sDate='" & iTime & "')"
Workbooks.Add
With ActiveSheet.QueryTables.Add(Connection:=Array(Array(Text1), Array("meout=5;")), Destination:=Range("A1"))
.CommandText = Array(Text2 & Chr(13) & Chr(10) & text3 & Chr(13) & Chr(10) & text4)
.Name = "查询来自 MS Access Database"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
next 如果您懂,帮我看看好吗?第2段代码该怎么样才能执行呢?多谢!!
[此贴子已经被作者于2005-3-25 14:23:08编辑过] |