idoo 发表于 2006-9-18 09:13:00

VBA如何访问Access?

使用AutoCAD VBA如何访问Access数据库?(如何打开,关闭,和定义Access查询的条件?)

lijizhi007 发表于 2006-9-21 23:47:00

是啊,有谁会啊,请教了

wy_26 发表于 2006-9-26 13:13:00

<P>1 VB编辑器菜单工具-〉引用,把ActiveX Data Objects 2.8选上,然后确定</P>
<P>2 编写连接代码</P>
<P>'指定数据库文件路径返回一个数据库连接对象Connection</P>
<P>&nbsp;Function GetConnection(DBPath As String) As Connection&nbsp;<BR>&nbsp;&nbsp;&nbsp; Dim conn As New Connection<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; With conn<BR>&nbsp;&nbsp;&nbsp; .Provider = "Microsoft.Jet.OLEDB.4.0"<BR>&nbsp;&nbsp;&nbsp; .ConnectionString = DBPath<BR>&nbsp;&nbsp;&nbsp; .Open<BR>&nbsp;&nbsp;&nbsp; End With<BR>&nbsp;&nbsp;&nbsp; Set GetConnection = conn<BR>End Function</P>
<P>sub test()&nbsp;</P>
<P>dim conn as Connection</P>
<P>dim rs as RecordSet</P>
<P>dim SqlCmd as new Command</P>
<P>Set conn = GetConnection("F:\CAD\CadDataBase\CadDataBase.mdb")<BR>&nbsp;&nbsp; With SqlCmd<BR>&nbsp;&nbsp; .ActiveConnection = conn<BR>&nbsp;&nbsp; .CommandType = adCmdStoredProc '命令类型为存储过程,adCmdText是普通SQL语句<BR>&nbsp;&nbsp; .CommandText = "Validate" '指出在ACCESS中建立的查询的名称<BR>&nbsp;&nbsp;&nbsp; End With</P>
<P>rs = SqlCmd.Execute , Array(id,pwd) '给定查询所需要的参数 <BR>msgbox rs.Feilds("userName").value</P>
<P>rs.close</P>
<P>conn.close</P>
<P>end sub</P>

idoo 发表于 2006-9-29 14:12:00

研究中……
页: [1]
查看完整版本: VBA如何访问Access?