PC Review


Reply
Thread Tools Rate Thread

How can I loop through fields in a known record

 
 
Steve
Guest
Posts: n/a
 
      4th Jan 2008
Hi
my question is this

how can I loop through a Known record ( same record every time) and store
each field into a variable
Somthing like this - (dont use this as it dont work)
Dim Deft(i) as variant
For i = 1 To 20
Deft(i) = DLookup(Fields(i), "tblSetupLocal", "[SID]=1")
Next i

So I can manipulate the variables and then send each variable value back to
the correct field in the rocord the same way. it would be very helpfull if I
could do this.

Steve - From a land down under

 
Reply With Quote
 
 
 
 
John W. Vinson
Guest
Posts: n/a
 
      4th Jan 2008
On Thu, 3 Jan 2008 21:07:01 -0800, Steve <(E-Mail Removed)>
wrote:

>Hi
>my question is this
>
>how can I loop through a Known record ( same record every time) and store
>each field into a variable
>Somthing like this - (dont use this as it dont work)
>Dim Deft(i) as variant
>For i = 1 To 20
>Deft(i) = DLookup(Fields(i), "tblSetupLocal", "[SID]=1")
>Next i
>
>So I can manipulate the variables and then send each variable value back to
>the correct field in the rocord the same way. it would be very helpfull if I
>could do this.
>
>Steve - From a land down under


Use a Recordset:

Dim i As Integer
Dim Deft(1 to 20) As Variant
Dim rs As DAO.Recordset
Dim db As DAO.Database
Set db = CurrentDb ' get the current database
Set rs = db.OpenRecordset("SELECT * FROM tblSetupLocal WHERE [SID]=1", _
dbOpenDynaset)
For i = 1 to 20
Deft(i) = rs.Fields(i)
Next I

or use rs!fieldname instead of rs.Fields(i), or set rs!fieldname to a value,
or set rs.Fields(i) to a value as appropriate.

Note that although you can manipulate data in tables using recordsets in this
way - and it's often the only, or best way - it is frequently better to use
Update queries or other types of action queries.

John W. Vinson [MVP]
 
Reply With Quote
 
Allen Browne
Guest
Posts: n/a
 
      4th Jan 2008
See how this goes, Steve:

Function LoopFields()
Dim rs As DAO.Recordset
Dim strFields() As String
Dim i As Integer

Set rs = DBEngine(0)(0).OpenRecordset("MyTable", dbOpenDynaset)
ReDim strFields(rs.Fields.Count - 1)
For i = 0 To rs.Fields.Count - 1
strFields(i) = rs.Fields(i).Name
Next
rs.Close
Set rs = Nothing

For i = LBound(strFields) To UBound(strFields)
Debug.Print strFields(i)
Next
End Function

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Steve" <(E-Mail Removed)> wrote in message
news:405365E4-52FC-4731-8363-(E-Mail Removed)...
> Hi
> my question is this
>
> how can I loop through a Known record ( same record every time) and store
> each field into a variable
> Somthing like this - (dont use this as it dont work)
> Dim Deft(i) as variant
> For i = 1 To 20
> Deft(i) = DLookup(Fields(i), "tblSetupLocal", "[SID]=1")
> Next i
>
> So I can manipulate the variables and then send each variable value back
> to
> the correct field in the rocord the same way. it would be very helpfull if
> I
> could do this.
>
> Steve - From a land down under
>


 
Reply With Quote
 
Steve
Guest
Posts: n/a
 
      4th Jan 2008
Thanks John
Im feeling very dumb right now as I spent hours going through Microsoft help
files ( there are so many and make no sence to me whatsoever and Im no fool
just on a learning curve)
It sems to me that when I type into the help box on access a who list of
stuff comes up but sorting through it to find what you want is a nightmare -
any suggestions on narrowing it all down.

"John W. Vinson" wrote:

> On Thu, 3 Jan 2008 21:07:01 -0800, Steve <(E-Mail Removed)>
> wrote:
>
> >Hi
> >my question is this
> >
> >how can I loop through a Known record ( same record every time) and store
> >each field into a variable
> >Somthing like this - (dont use this as it dont work)
> >Dim Deft(i) as variant
> >For i = 1 To 20
> >Deft(i) = DLookup(Fields(i), "tblSetupLocal", "[SID]=1")
> >Next i
> >
> >So I can manipulate the variables and then send each variable value back to
> >the correct field in the rocord the same way. it would be very helpfull if I
> >could do this.
> >
> >Steve - From a land down under

>
> Use a Recordset:
>
> Dim i As Integer
> Dim Deft(1 to 20) As Variant
> Dim rs As DAO.Recordset
> Dim db As DAO.Database
> Set db = CurrentDb ' get the current database
> Set rs = db.OpenRecordset("SELECT * FROM tblSetupLocal WHERE [SID]=1", _
> dbOpenDynaset)
> For i = 1 to 20
> Deft(i) = rs.Fields(i)
> Next I
>
> or use rs!fieldname instead of rs.Fields(i), or set rs!fieldname to a value,
> or set rs.Fields(i) to a value as appropriate.
>
> Note that although you can manipulate data in tables using recordsets in this
> way - and it's often the only, or best way - it is frequently better to use
> Update queries or other types of action queries.
>
> John W. Vinson [MVP]
>

 
Reply With Quote
 
John W. Vinson
Guest
Posts: n/a
 
      4th Jan 2008
On Thu, 3 Jan 2008 21:37:01 -0800, Steve <(E-Mail Removed)>
wrote:

>Thanks John
>Im feeling very dumb right now as I spent hours going through Microsoft help
>files ( there are so many and make no sence to me whatsoever and Im no fool
>just on a learning curve)
>It sems to me that when I type into the help box on access a who list of
>stuff comes up but sorting through it to find what you want is a nightmare -
>any suggestions on narrowing it all down.


Don't beat up on yourself! You are in very good (and very large) company in
considering Microsoft's Help system to be seriously lacking. I'm pretty good
at Access, and have a good memory... but I *STILL* have Access 97 installed,
because it was the last version to have a reasonably usable Help file.

Microsoft developers have been hearing (loudly, sometimes not all that
politely) from MVP's and other concerned folks about this, and there is *some*
improvement; what I've seen of 2007 help is... well, maybe up to 60% of the
quality of 97, disregarding the fact that most of it requires a fast web
connection (which of course not everyone has).

I wish I had some better suggestions, but the indexing and searching of Access
Help simply stinks, and I don't know of any good alternative (other than these
newsgroups and their assembled centuries of experience).

John W. Vinson [MVP]
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
update multiple fields in a record based on a record in another table bertil.hedenstrom@gmail.com Microsoft Access Queries 3 19th Sep 2006 11:49 AM
Get current record in subform and fill fields of that record to another form khanhly246 via AccessMonster.com Microsoft Access Form Coding 1 23rd Aug 2006 01:01 AM
Loop through all fields in a single record? tedqn@yahoo.com Microsoft ASP .NET 4 28th Apr 2006 09:46 AM
No current record error..loop within loop Dale Microsoft Access VBA Modules 1 4th Feb 2006 05:09 PM
How to Loop to get fields in a record Kirk Groome Microsoft Access VBA Modules 3 23rd Aug 2005 03:14 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:03 PM.