Information Technology Solutions, Answers and Experts
Write an Article Ask a Question

How do I loop through records

Anderson
P: n/a
Anderson
What is the VBA format/code for looping through records. This is what I
am trying to do.

I have a table called appointments. A student can have as many
appointments. This table has among other fields change date, reason and
grade.

I want to write a funnction which says:

If change date > "certain date" and reason = "something new" and grade
different from the previous record then out put something.


How can I structure and write this please.?


Nov 13 '05 #1

3 Replies



Salad
P: n/a
Salad

re: How do I loop through records

Anderson wrote:
[color=blue]
> What is the VBA format/code for looping through records. This is what I
> am trying to do.
>
> I have a table called appointments. A student can have as many
> appointments. This table has among other fields change date, reason and
> grade.
>
> I want to write a funnction which says:
>
> If change date > "certain date" and reason = "something new" and grade
> different from the previous record then out put something.
>
>
> How can I structure and write this please.?
>
>[/color]
I would select the records from a SQL statement and sort by Date
ascending. Then scan the results. Something like this

Function ScanRecs(lngID As Long) As Boolean
DIm strSQL As STring
Dim rst As REcordset
strSQL = "Select * From StudentRecs Where StudentID = " & _
lngID & " Order By SomeDate"

'open the results read-only
Set rst = currentdb.openrecordset(strSQL,dbopensnapshot)
If rst.Recordcount > 0 then
rst.MoveFirst
Do while not rst.EOF
'....process
rst.MoveNext
Loop
Endif
End Function

Drop the above code into a code module and start pressing F1 on the
methods and properties you need more info on.

Nov 13 '05 #2

Anderson
P: n/a
Anderson

re: How do I loop through records

Thanks for this code. very much appreciated. One mor questions the
variable "lngID" what is that supposedd to be? is the the same as the
student ID or not?

Salad wrote:
[color=blue]
> Anderson wrote:
>[color=green]
>> What is the VBA format/code for looping through records. This is what
>> I am trying to do.
>>
>> I have a table called appointments. A student can have as many
>> appointments. This table has among other fields change date, reason
>> and grade.
>>
>> I want to write a funnction which says:
>>
>> If change date > "certain date" and reason = "something new" and grade
>> different from the previous record then out put something.
>>
>>
>> How can I structure and write this please.?
>>
>>[/color]
> I would select the records from a SQL statement and sort by Date
> ascending. Then scan the results. Something like this
>
> Function ScanRecs(lngID As Long) As Boolean
> DIm strSQL As STring
> Dim rst As REcordset
> strSQL = "Select * From StudentRecs Where StudentID = " & _
> lngID & " Order By SomeDate"
>
> 'open the results read-only
> Set rst = currentdb.openrecordset(strSQL,dbopensnapshot)
> If rst.Recordcount > 0 then
> rst.MoveFirst
> Do while not rst.EOF
> '....process
> rst.MoveNext
> Loop
> Endif
> End Function
>
> Drop the above code into a code module and start pressing F1 on the
> methods and properties you need more info on.
>[/color]

Nov 13 '05 #3

Salad
P: n/a
Salad

re: How do I loop through records

Anderson wrote:[color=blue]
> Thanks for this code. very much appreciated. One mor questions the
> variable "lngID" what is that supposedd to be? is the the same as the
> student ID or not?
>
> Salad wrote:
>[color=green]
>> Anderson wrote:
>>[color=darkred]
>>> What is the VBA format/code for looping through records. This is what
>>> I am trying to do.
>>>
>>> I have a table called appointments. A student can have as many
>>> appointments. This table has among other fields change date, reason
>>> and grade.
>>>
>>> I want to write a funnction which says:
>>>
>>> If change date > "certain date" and reason = "something new" and
>>> grade different from the previous record then out put something.
>>>
>>>
>>> How can I structure and write this please.?
>>>
>>>[/color]
>> I would select the records from a SQL statement and sort by Date
>> ascending. Then scan the results. Something like this
>>
>> Function ScanRecs(lngID As Long) As Boolean
>> DIm strSQL As STring
>> Dim rst As REcordset
>> strSQL = "Select * From StudentRecs Where StudentID = " & _
>> lngID & " Order By SomeDate"
>>
>> 'open the results read-only
>> Set rst = currentdb.openrecordset(strSQL,dbopensnapshot)
>> If rst.Recordcount > 0 then
>> rst.MoveFirst
>> Do while not rst.EOF
>> '....process
>> rst.MoveNext
>> Loop
>> Endif
>> End Function
>>
>> Drop the above code into a code module and start pressing F1 on the
>> methods and properties you need more info on.
>>[/color]
>[/color]
Functions, and subroutines, can have arguments passed to them. For
example, from the debug window I could enter
? SumIt(1,2)
and this will call the function SumIt.
Function SumIt(intA as Integer, intB as Integer) As Integer
Sumit = intA + intB
End Function
and will return an integer value of adding the two values together.

I will assume you would want to only scan the records of the current
student record so I set up an argument that gets the ID. The code I
wrote is pretty useless...I don't know the table/names...just providing
some code that demonstrates the methods/properties that you'd probably use.
Nov 13 '05 #4

Post your reply

Sign in to post your reply or Sign up for a free account.



Didn't find the answer to your question? Post your Microsoft Access / VBA question on Bytes

You can also browse similar questions: Microsoft Access / VBA

Get Microsoft Access / VBA Help

Get Microsoft Access / VBA help from a network of professionals.

Post your Question » Over 331,214 Members | 3354 Online