caiqs 发表于 2006-7-4 12:05:00

VBA连接EXcel数据库(非引用Excel)

<P>Public conn As New ADODB.Connection&nbsp;&nbsp; '定义新连接<BR>Public cmd As New ADODB.Command&nbsp;&nbsp;&nbsp;&nbsp; '定义指令<BR>Public rs As New ADODB.Recordset&nbsp; '定义两个记录集<BR></P>
<P>'VBA连接Excel数据库<BR>Sub aa()<BR>Set conn = New Connection<BR>conn.Open "Provider=msdasql.1;DBQ=D:\1.xls;Driver={Driver do Microsoft Excel(*.xls)};ReadOnly=1"<BR>Set cmd.ActiveConnection = conn<BR>cmd.CommandText = "select * from AA"<BR>rs.CursorLocation = adUseClient<BR></P>

<P>'为什么下边这行会出错,换成连接access时,同样的语法可以通过<BR></P>
<P>rs.Open cmd, , adOpenStatic, adLockBatchOptimistic</P>

<P>Dim num As Integer<BR>num = rs.Fields.Count<BR>'声明动态数据,存储所有字段名<BR>Dim a()<BR>Dim n As Integer<BR>ReDim a(num - 1)<BR>n = 0<BR>For Each Red In rs.Fields<BR>a(n) = Red.Name<BR>n = n + 1<BR>Next<BR>cc = rs.GetRows(, 2, "半径")<BR>rs.Close<BR>End Sub</P>
<P><BR>&nbsp;</P>
页: [1]
查看完整版本: VBA连接EXcel数据库(非引用Excel)