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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting a query value into a module? 2

Status
Not open for further replies.
May 9, 2000
446
GB
Okay i've tried long enough, i have a query that groups on a year value and outputs only the year. How can i get these values into a module? I want to use each value retruned by the query to be used by the module to check for a directory called that, e.g. 1999 and if it isn't found then create it...... creating dir's is easy how do i get the query values into my module though???

Cheers
 
Vanilla,
You could pass the value into a function...ie. in the Field: row of the query grid, just put your function name with the year value ie:
X: Myfunction(Year([mydatefield]))
This assumes your function accepts this argument and expects to do this one-by-one.

If you want to do it all in the function calling the function just once, then you can open a recordset based on this query, and use the field in question in the logic in a loop through all records.

--Jim
 
Hey Jim,
Could you give me a bit more help with the calling the function just once method???
Thanks a lot
 
Hey Jim,
Could you give me a bit more help with the calling the function just once method??? I've never done that before....
Thanks a lot
 
vanilla,
All you'd need to do is create the query, then in a module open a recordsest based on the querydef, ie:
dim qd as querydef, rst as recordset
set qd = currentdb.querydefs("queryname")
set rst = qd.openrecordset(dbopensnapshot) 'snapshot would be default anyway
do until rst.eof
'the directory verify/create code goes here based on rst!X
loop
--Jim
 

Vanillapod,

Suppose you had a query by the name of qryMine with a field called MyYear. The variable would be saved to strField.

Within the module, you could do the following:

Set dbs = OpenDatabase(CurrentDb.Name)
Set rstQuery = dbs.OpenRecordset(qryMine)

rstQuery.MoveLast
x = rstQuery.RecordCount
rstQuery.MoveFirst

For i = 1 To x
strField = rstQuery.Fields("MyYear")

‘ data manipulation goes here

rstQuery.MoveNext
Next i

rstQuery.Close
dbs.Close

HTH,
GGleason



 

Vanillapod,

A minor correction to the code (quotes around query name, unless you have the query as a variable, then it has no quotes):


Set dbs = OpenDatabase(CurrentDb.Name)
Set rstQuery = dbs.OpenRecordset(”qryMine”)

rstQuery.MoveLast
x = rstQuery.RecordCount
rstQuery.MoveFirst

For i = 1 To x
strField = rstQuery.Fields("MyYear")

‘ data manipulation goes here

rstQuery.MoveNext
Next i

rstQuery.Close
dbs.Close


Thanks,
GGleason
 
Cheers for all the responses, I'll go do it now!!!
Thanks again
 
Okay here's the code i've been using... cheers to everyone above! However when i try and run the module i get an error saying "Compile Error: Method or Data Member Not Found" any ideas?

Dim dbs As Database
Dim rstQuery As QueryDef
Dim QueryName
QueryName = "UPLOADSYears"
Set dbs = OpenDatabase(CurrentDb.name)
Set rstQuery = dbs.OpenRecordset(QueryName)

rstQuery.MoveLast
x = rstQuery.RecordCount
rstQuery.MoveFirst

For i = 1 To x
strField = rstQuery.Fields("MyYear")
‘ data manipulation goes here
rstQuery.MoveNext
Next i

rstQuery.Close
dbs.Close
 

vanillapod,

When you are viewing the module, go to Tools->References. Make sure the following references are checked:

Visual Basic for Applications
Microsoft Access 8.0 Object Library
Microsoft DAO 3.5 Object Library

I don't know if this is the problem, but I would check it.

HTH,
GGLeason
 
Hey GGleason cheers for the quick response...
Yep there all there, this is typical the last part of my database to get working and the most troublesome.....
 

vanillapod,

Here is a minor correction to one of the Dim statements:

Dim
rstQuery As Recordset

You also may want to add this entire line:

Dim rstQuery As Recordset, i As Long, x As Long, strField As String


HTH,
GGleason

 
Oh yeah, its a recordset isn't it! Just tried it and it works fine, thank you sooooooo much....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top