Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting data in sql queries using visual basic in access

Status
Not open for further replies.

mk2lee1

MIS
Jul 8, 2003
51
US
Hi, I want to retrieve data that is returned by sql query that I defined in access queries in visual basic. In order to count the rows I know we use DCount() method. What method do I use to get the actual data in the queries? Please Help me on this. Thank you.
 
if i understood you do something like

dim db as database
dim rcs as recordset

set db = currentdb

set rcs = db.openrecordset("queryname")

to have the data you do either
rcs.Fields("fieldname").value
or
rcs!fieldname

to got through the records i do:
if rcs.recordCount <> 0 then
rcs.movelast
rcs.movefirst
for i = 0 to rcs.recordCount
variable = rcs!fieldname

rcs.movenext
next i
end if
 
why do i get yellow highlight (i guess warning or something) over Set db= CurrentDb. Do I have to include something?
 
This is what I wrote.
And in my querysection, I have query called &quot;testDate1MonthNotice&quot; And within this query, there is 3 field retrieving. SchoolName, P-ID, and TestDate.

I get erroe in here saying that &quot;Item not found in this collection&quot;

With rst.Fields(SchoolName).Value

==============================================
Dim db As DAO.Database, rst As DAO.Recordset
Dim i As Integer
Dim variable As String
Dim msg As String


Set db = CurrentDb
Set rst = db.OpenRecordset(&quot;testDate1MonthNotice&quot;)
With rst.Fields(SchoolName).Value
If rst.RecordCount <> 0 Then
rst.MoveLast
rst.MoveFirst

For i = 0 To rst.RecordCount
variable = rst!SchoolName
rst.MoveNext
Next i
End If

If variable <> &quot;&quot; Then
MsgBox &quot;hi&quot;, vbOKOnly, &quot;Message Title&quot;
End If
End With


 
in my version i don't need to put DAO.Database, have you tried looking at the help for CurrentDb?
 
I now have changed my code this this
but I get type mismatch error at

Set rst = db.OpenRecordset(SQL)

Any idea?
==========================================
Private Sub Form_Load()

Dim db As Database
Dim rst As Recordset
Dim i As Integer

Dim SQL As String
SQL = &quot;SELECT [school].[schoolName], [Participation].[ParticipationID], [Participation].[TestDAte]&quot; & _
&quot;FROM school RIGHT JOIN Participation ON [Participation].[SchoolID]=[School].[SchooliD]&quot; & _
&quot;WHERE ([Participation].[testDate]-date()<=30 And [Participation].[testDate]-date()>0)&quot;

Set db = CurrentDb
Set rst = db.OpenRecordset(SQL)

If rst.RecordCount <> 0 Then
rst.MoveLast
rst.MoveFirst

For i = 0 To rst.RecordCount
variable = rst.Fields(i)
rst.MoveNext
Next i
End If
 
more than likely in the SQL statement. Create it with access to test if it works.
 
well sql statement is working fine.. I just copied and tested from access. it worked fine...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top