(mccad请进)VBA与Access链接查询求教
本帖最后由 作者 于 2007-6-14 20:26:10 编辑 <br /><br /> <p>VBA中,需要根据三个条件在Access中查询符合条件的记录:要求同时以VBA中的三个条件,查找出Access中符合要求的记录。我在作数据库链接时好像是陷入了死循环,电脑就没有了反映……请高手指点该如何链接、查询!!!</p> 你的代码? <p>先谢谢mccad大师!</p><p>Dim wr As Workspace '声明工作空间变量,为打开数据库提供空间<br/>Dim db As Database '声明数据库对象变更,用于打开数据库<br/>Dim dl As Recordset '声明数据库表,用于打开数据库库表<br/>Dim xl As Recordset<br/>Dim DTYPE As Recordset<br/>Dim DLOAD As Recordset<br/>Dim DSPEED As Recordset<br/>Dim DNBE As Recordset<br/>Dim DNBS As Recordset<br/>Dim DRMax As Recordset</p><p><br/>Set wr = CreateWorkspace("", "admin", "", dbUseJet)<br/>Set db = wr.OpenDatabase("C:\Program Files\Database.mdb", False, True, "")<br/>Set dl = db.OpenRecordset("Data", dbOpenDynaset, dbReadOnly)<br/>Do<br/> If dl.EOF = False Then<br/> cobType.AddItem dl.Fields("Type").Value<br/> dl.MoveNext<br/> Else<br/> Exit Do<br/> End If<br/>Loop</p><p><br/>以上为Access连接部分,实际上我是要根据Access中的DTYPE、DLOAD、DSPEED、DNBE、DNBS、和DRMax,总共6列的数据来最终查找出一条记录,并将找出的这条记录中的其它字段的数据赋值给AutoCAD VBA窗口中的相应控件。</p><p></p> <p>改为</p><p> Do while Not dl.EOF <br/> cobType.AddItem dl.Fields("Type").Value<br/> dl.MoveNext<br/> Loop</p> <p></p><p>以上是我的Access数据</p> <p>怎么解决??</p><p>还有找到后怎么从access中取数值?这方面是一窍不通啊~~~</p> 控件=dl.Fields("Type").’将access字段值付给控件某个属性。 <p>大家有没有这种的成功案例啊~~</p> Public Function OpenConnection(DbPath)Dim TempConn
'On Error Resume Next
Set OpenConnection = CreateObject("ADODB.Connection")
OpenConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DbPath
If Err Then
Err.Clear
MsgBox "链接出错,请检查数据库链接字符串!"
End If
End Function
Sub DoAccess()
Dim RS, CONN
Dim DbPath As String
Dim TableList
Dim RecordCnt As Integer
Dim i As Integer
DbPath = "D:\mdbpath\mdbfile.mdb"
Set CONN = OpenConnection(DbPath)
If IsObject(CONN) Then
Set RS = CONN.execute("selectDTYPE,DLOAD,DSPEED,DNBE,DNBS,DRMax from mdbtable wheredtype=true and dload=15")
If RS.bof And RS.EOF Then
RS.Close: Set RS = Nothing: Set CONN = Nothing
Exit Sub
End If
'
TableList = RS.GetRows(-1)
Set RS = Nothing
Set CONN = Nothing
RecordCnt = UBound(TableList, 2)
For i = 0 To RecordCnt
Debug.Print TableList(0, i)
Debug.Print TableList(1, i)
Debug.Print TableList(2, i)
Debug.Print TableList(3, i)
Debug.Print TableList(4, i)
Debug.Print TableList(5, i)
Debug.Print "+++++++++++++++++++"
Next
End If
End Sub
本帖最后由 作者 于 2007-10-6 11:43:04 编辑
.
页:
[1]
2