idoo 发表于 2007-10-6 11:26:00

首先谢谢两位大侠的指点!!现在就认真拜读中……

jxh2003zfr 发表于 2007-10-6 11:40:00

希望对你有用,建议多看看SQL方面的书籍

<p>Dim wr As Workspace '声明工作空间变量,为打开数据库提供空间<br/>Dim db As Database&nbsp; '声明数据库对象变更,用于打开数据库<br/>Dim dl As Recordset '声明数据库表,用于打开数据库库表<br/>Dim DTYPE As String<br/>Dim DLOAD As String<br/>Dim DSPEED As String<br/>Dim DNBE As String<br/>Dim DNBS As String<br/>Dim DRMax As String<br/>Dim strSql As String<br/>Dim rs As Recordset<br/>Dim cou As Integer, i As Integer</p><p>'以下使用DAO<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)</p><p>strSql = "select * from GVF-II where type='" &amp; DTYPE &amp; _<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "' and load='" &amp; DLOAD &amp; "' and speed='" &amp; DSPEED &amp; _<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "' and nbe='" &amp; DNBE &amp; "' and nbstopmax='" &amp; DNBS &amp; _<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "' and rmax='" &amp; DRMax &amp; "'"<br/>Set rs = db.OpenRecordset(strSql, dbOpenDynaset)<br/>'如果根据此六个条件查得记录最多只有1条时,可使用以下代码<br/>If rs.RecordCount &gt; 0 Then<br/>&nbsp;&nbsp;&nbsp; Debug.Print rs.Fields("type")<br/>&nbsp;&nbsp;&nbsp; Debug.Print rs.Fields("load")<br/>&nbsp;&nbsp;&nbsp; Debug.Print rs.Fields("speed")<br/>&nbsp;&nbsp;&nbsp; Debug.Print rs.Fields("nbe")<br/>&nbsp;&nbsp;&nbsp; Debug.Print rs.Fields("nbstopmax")<br/>&nbsp;&nbsp;&nbsp; Debug.Print rs.Fields("rmax")<br/>&nbsp;&nbsp;&nbsp; '也可将值赋给控件,如:TextBox1.Text=rs.Fields("type")<br/>End If<br/>'如果根据此六个条件查得记录可能有2条以上时,可使用以下代码<br/>If Not rs.EOF Then<br/>&nbsp;&nbsp;&nbsp; rs.MoveLast<br/>&nbsp;&nbsp;&nbsp; cou = rs.RecordCount<br/>&nbsp;&nbsp;&nbsp; rs.MoveFirst<br/>Else<br/>&nbsp;&nbsp;&nbsp; cou = rs.RecordCount<br/>&nbsp;&nbsp;&nbsp; rs.MoveFirst<br/>End If</p><p>For i = 1 To cou<br/>&nbsp;&nbsp;&nbsp; Debug.Print rs.Fields("type")<br/>&nbsp;&nbsp;&nbsp; Debug.Print rs.Fields("load")<br/>&nbsp;&nbsp;&nbsp; Debug.Print rs.Fields("speed")<br/>&nbsp;&nbsp;&nbsp; Debug.Print rs.Fields("nbe")<br/>&nbsp;&nbsp;&nbsp; Debug.Print rs.Fields("nbstopmax")<br/>&nbsp;&nbsp;&nbsp; Debug.Print rs.Fields("rmax")<br/>&nbsp;&nbsp;&nbsp; rs.MoveNext<br/>Next i</p>

idoo 发表于 2007-10-6 22:26:00

<p><strong><font face="Verdana" color="#61b713">jxh2003zfr,谢谢你的帮助!</font></strong></p><p><font face="Verdana" color="#61b713"><strong>但是我研究了好久,程序总是在“</strong><font color="#000000">Set rs = db.OpenRecordset(strSql, dbOpenDynaset)</font><br/>”这里停住,并且提示“FROM 子句语法错误”。</font></p><p><font face="Verdana" color="#61b713">因为我对SQL不懂,然后自己调了半天,依旧是这样。还望赐教!</font></p><p><font face="Verdana" color="#61b713">谢谢!!</font></p>

jxh2003zfr 发表于 2007-11-14 19:14:00

问题解决了吗

idoo发表于2007-10-6 22:26:00static/image/common/back.gifjxh2003zfr,谢谢你的帮助!但是我研究了好久,程序总是在“Set rs = db.OpenRecordset(strSql, dbOpenDynaset)”这里停住,并且提示“FROM 子句语法错误”。因为我对SQL不懂,然后自己调了半天

<p>呵呵,问题解决了吗,如果还没解决的话请发你的sql语句上来看看。</p>
页: 1 [2]
查看完整版本: (mccad请进)VBA与Access链接查询求教