Using recordsets to create or update tables.

D

Dom Torrez

hello
I'm trying to use a recordset to insert values into my access table. I can
get the insert working somewhat correctly, problem is that the inserted
records are all the same. I believe my problem is in my loop. it seems like
I need pointers to cycle down through my recordset rows, which isn't
happening. Can some one tell me where i'm going wrong? Like I said my
insert into the access table from the recordset is working partially, I just
get multiple rows of one record rather than multiple rows of different
records.

***************
With rsQry
Do Until .EOF
Set riTbl = CurrentDb.OpenRecordset("ttReconImagesForm_tbl")
riTbl.AddNew
riTbl![idLocale] = idLocale
riTbl![idRecon] = idRecon
riTbl![imageid] = imageid
riTbl![Activity] = Activity
riTbl![ImageCategory] = ImageCategory
riTbl![Path] = Path
riTbl![WhenTaken] = WhenTaken
riTbl![ImageCaption] = ImageCaption
riTbl.Update
riTbl.MoveNext
Loop
rsQry.MoveNext
End With
*************
I'd appreciate any help offered. Thanks
Dom Torrez
 
M

Martin

It looks like you need to move the rst.Query.MoveNext inside your loop. As
it stands, you are looping thru building your table, but not advancing to the
next record in the query.

Hope this helps.
 
D

Dom Torrez

Hi Martin,
I followed your suggestion and still get the same issue. My code cycles
through the loop, it just does so while inserting the same recordset row into
each new row in the table. My table ends up having many rows with the same
records. I was just reading about the .getrows method to copy and or get
multiple rows from a recordset. Is this the route I should be taking? If so
how would I use this?
thanks

Martin said:
It looks like you need to move the rst.Query.MoveNext inside your loop. As
it stands, you are looping thru building your table, but not advancing to the
next record in the query.

Hope this helps.


Dom Torrez said:
hello
I'm trying to use a recordset to insert values into my access table. I can
get the insert working somewhat correctly, problem is that the inserted
records are all the same. I believe my problem is in my loop. it seems like
I need pointers to cycle down through my recordset rows, which isn't
happening. Can some one tell me where i'm going wrong? Like I said my
insert into the access table from the recordset is working partially, I just
get multiple rows of one record rather than multiple rows of different
records.

***************
With rsQry
Do Until .EOF
Set riTbl = CurrentDb.OpenRecordset("ttReconImagesForm_tbl")
riTbl.AddNew
riTbl![idLocale] = idLocale
riTbl![idRecon] = idRecon
riTbl![imageid] = imageid
riTbl![Activity] = Activity
riTbl![ImageCategory] = ImageCategory
riTbl![Path] = Path
riTbl![WhenTaken] = WhenTaken
riTbl![ImageCaption] = ImageCaption
riTbl.Update
riTbl.MoveNext
Loop
rsQry.MoveNext
End With
*************
I'd appreciate any help offered. Thanks
Dom Torrez
 
J

John Spencer

Your code should look something like this.

With rsQry
Set riTbl = CurrentDb.OpenRecordset("ttReconImagesForm_tbl")
Do Until .EOF
riTbl.AddNew
riTbl![idLocale] = idLocale
riTbl![idRecon] = idRecon
riTbl![imageid] = imageid
riTbl![Activity] = Activity
riTbl![ImageCategory] = ImageCategory
riTbl![Path] = Path
riTbl![WhenTaken] = WhenTaken
riTbl![ImageCaption] = ImageCaption
riTbl.Update
rsQry.MoveNext
Loop
End With

Does it?

Dom Torrez said:
Hi Martin,
I followed your suggestion and still get the same issue. My code cycles
through the loop, it just does so while inserting the same recordset row
into
each new row in the table. My table ends up having many rows with the
same
records. I was just reading about the .getrows method to copy and or get
multiple rows from a recordset. Is this the route I should be taking? If
so
how would I use this?
thanks

Martin said:
It looks like you need to move the rst.Query.MoveNext inside your loop.
As
it stands, you are looping thru building your table, but not advancing to
the
next record in the query.

Hope this helps.


Dom Torrez said:
hello
I'm trying to use a recordset to insert values into my access table. I
can
get the insert working somewhat correctly, problem is that the inserted
records are all the same. I believe my problem is in my loop. it
seems like
I need pointers to cycle down through my recordset rows, which isn't
happening. Can some one tell me where i'm going wrong? Like I said my
insert into the access table from the recordset is working partially, I
just
get multiple rows of one record rather than multiple rows of different
records.

***************
With rsQry
Do Until .EOF
Set riTbl = CurrentDb.OpenRecordset("ttReconImagesForm_tbl")
riTbl.AddNew
riTbl![idLocale] = idLocale
riTbl![idRecon] = idRecon
riTbl![imageid] = imageid
riTbl![Activity] = Activity
riTbl![ImageCategory] = ImageCategory
riTbl![Path] = Path
riTbl![WhenTaken] = WhenTaken
riTbl![ImageCaption] = ImageCaption
riTbl.Update
riTbl.MoveNext
Loop
rsQry.MoveNext
End With
*************
I'd appreciate any help offered. Thanks
Dom Torrez
 
D

Dom Torrez

Hi John,
Yes my code looks just like this. My access table is getting populated with
38 rows all the same record. An example is the value for my field imageid
is unique to each record, the table has 38 rows all with imageid being the
same. as well as all my other fields which should be unique. It seems as if
my code is only looping through the first record in the recordset. As a side
note the record set is displaying the data correclty on my form, ie...no
duplicate records and all records are visible. Since the data is being sent
to the form correctly is there a way to save the data from the form into an
access table? I attached my full code below.

*******************************************
Private Sub Form_Open(Cancel As Integer)
'variables
Dim db As Database
Dim rsQry As DAO.Recordset
Dim qryDef As DAO.QueryDef
Dim sQuery As String
Dim riTbl As DAO.Recordset
'Table column values
Dim idLocale As Integer
Dim idRecon As Integer
Dim imageid As Integer
Dim Activity As String
Dim ImageCategory As Variant
Dim Path As String
Dim WhenTaken As Date
Dim ImageCaption As String
'SQL query.
sQuery = "select idlocale, idrecon, imageid, activity, imagecategory, path ,
whentaken, imagecaption from getReconImages(" &
Forms![LocalesDataEntry]![ReconnaissanceDataEntrySF]![idRecon] & ");"
Debug.Print sQuery
Set qryDef = CurrentDb.CreateQueryDef("")
'setting values
qryDef.Connect =
"ODBC;DSN=@@@;DATABASE=@@@;SERVER=@@@;PORT=5432;SSLMODE=prefer;"
qryDef.SQL = sQuery
Debug.Print sQuery
Set rsQry = qryDef.OpenRecordset
Set Me.Recordset = rsQry
'Set variables to recordset values
idLocale = rsQry![idLocale]
idRecon = rsQry![idRecon]
imageid = rsQry![imageid]
Activity = rsQry![Activity]
ImageCategory = rsQry![ImageCategory]
Path = rsQry![Path]
WhenTaken = rsQry![WhenTaken]
ImageCaption = rsQry![ImageCaption]
'Set variables to record set for access table
With rsQry
Set riTbl = CurrentDb.OpenRecordset("ttReconImagesForm_tbl")
Do Until .EOF
riTbl.AddNew
riTbl![idLocale] = idLocale
riTbl![idRecon] = idRecon
riTbl![imageid] = imageid
riTbl![Activity] = Activity
riTbl![ImageCategory] = ImageCategory
riTbl![Path] = Path
riTbl![WhenTaken] = WhenTaken
riTbl![ImageCaption] = ImageCaption
riTbl.Update
rsQry.MoveNext
Loop
End With
*************************
Thanks,
Dom Torrez



John Spencer said:
Your code should look something like this.

With rsQry
Set riTbl = CurrentDb.OpenRecordset("ttReconImagesForm_tbl")
Do Until .EOF
riTbl.AddNew
riTbl![idLocale] = idLocale
riTbl![idRecon] = idRecon
riTbl![imageid] = imageid
riTbl![Activity] = Activity
riTbl![ImageCategory] = ImageCategory
riTbl![Path] = Path
riTbl![WhenTaken] = WhenTaken
riTbl![ImageCaption] = ImageCaption
riTbl.Update
rsQry.MoveNext
Loop
End With

Does it?

Dom Torrez said:
Hi Martin,
I followed your suggestion and still get the same issue. My code cycles
through the loop, it just does so while inserting the same recordset row
into
each new row in the table. My table ends up having many rows with the
same
records. I was just reading about the .getrows method to copy and or get
multiple rows from a recordset. Is this the route I should be taking? If
so
how would I use this?
thanks

Martin said:
It looks like you need to move the rst.Query.MoveNext inside your loop.
As
it stands, you are looping thru building your table, but not advancing to
the
next record in the query.

Hope this helps.


:

hello
I'm trying to use a recordset to insert values into my access table. I
can
get the insert working somewhat correctly, problem is that the inserted
records are all the same. I believe my problem is in my loop. it
seems like
I need pointers to cycle down through my recordset rows, which isn't
happening. Can some one tell me where i'm going wrong? Like I said my
insert into the access table from the recordset is working partially, I
just
get multiple rows of one record rather than multiple rows of different
records.

***************
With rsQry
Do Until .EOF
Set riTbl = CurrentDb.OpenRecordset("ttReconImagesForm_tbl")
riTbl.AddNew
riTbl![idLocale] = idLocale
riTbl![idRecon] = idRecon
riTbl![imageid] = imageid
riTbl![Activity] = Activity
riTbl![ImageCategory] = ImageCategory
riTbl![Path] = Path
riTbl![WhenTaken] = WhenTaken
riTbl![ImageCaption] = ImageCaption
riTbl.Update
riTbl.MoveNext
Loop
rsQry.MoveNext
End With
*************
I'd appreciate any help offered. Thanks
Dom Torrez
 
J

John Spencer

You set the variables outside the loop, so they never get changed. Since
you seem to want the values from rsQry added to the new records in nTbl, I
would write the code as follows without the variables - just refer directly
to the fields in rsQry.

*******************************************
Private Sub Form_Open(Cancel As Integer)
'variables
Dim db As Database
Dim rsQry As DAO.Recordset
Dim qryDef As DAO.QueryDef
Dim sQuery As String
Dim riTbl As DAO.Recordset
'SQL query.
sQuery = "select idlocale, idrecon, imageid, activity, imagecategory, path ,
whentaken, imagecaption from getReconImages(" &
Forms![LocalesDataEntry]![ReconnaissanceDataEntrySF]![idRecon] & ");"
Debug.Print sQuery
Set qryDef = CurrentDb.CreateQueryDef("")
'setting values
qryDef.Connect =
"ODBC;DSN=@@@;DATABASE=@@@;SERVER=@@@;PORT=5432;SSLMODE=prefer;"
qryDef.SQL = sQuery
Debug.Print sQuery
Set rsQry = qryDef.OpenRecordset
'Don't understand what this line is going to do for you in this code
Set Me.Recordset = rsQry

With rsQry
Set riTbl = CurrentDb.OpenRecordset("ttReconImagesForm_tbl")
Do Until .EOF
riTbl.AddNew
riTbl![idLocale] =!idLocale
riTbl![idRecon] =!idRecon
riTbl![imageid] = !imageid
riTbl![Activity] = !Activity
riTbl![ImageCategory] = !ImageCategory
riTbl![Path] = !Path
riTbl![WhenTaken] = !WhenTaken
riTbl![ImageCaption] = !ImageCaption
riTbl.Update
rsQry.MoveNext
Loop
End With
*************************
Thanks,
Dom Torrez said:
Hi John,
Yes my code looks just like this. My access table is getting populated
with
38 rows all the same record. An example is the value for my field
imageid
is unique to each record, the table has 38 rows all with imageid being the
same. as well as all my other fields which should be unique. It seems as
if
my code is only looping through the first record in the recordset. As a
side
note the record set is displaying the data correclty on my form, ie...no
duplicate records and all records are visible. Since the data is being
sent
to the form correctly is there a way to save the data from the form into
an
access table? I attached my full code below.

*******************************************
Private Sub Form_Open(Cancel As Integer)
'variables
Dim db As Database
Dim rsQry As DAO.Recordset
Dim qryDef As DAO.QueryDef
Dim sQuery As String
Dim riTbl As DAO.Recordset
'Table column values
Dim idLocale As Integer
Dim idRecon As Integer
Dim imageid As Integer
Dim Activity As String
Dim ImageCategory As Variant
Dim Path As String
Dim WhenTaken As Date
Dim ImageCaption As String
'SQL query.
sQuery = "select idlocale, idrecon, imageid, activity, imagecategory, path
,
whentaken, imagecaption from getReconImages(" &
Forms![LocalesDataEntry]![ReconnaissanceDataEntrySF]![idRecon] & ");"
Debug.Print sQuery
Set qryDef = CurrentDb.CreateQueryDef("")
'setting values
qryDef.Connect =
"ODBC;DSN=@@@;DATABASE=@@@;SERVER=@@@;PORT=5432;SSLMODE=prefer;"
qryDef.SQL = sQuery
Debug.Print sQuery
Set rsQry = qryDef.OpenRecordset
Set Me.Recordset = rsQry
'Set variables to recordset values
idLocale = rsQry![idLocale]
idRecon = rsQry![idRecon]
imageid = rsQry![imageid]
Activity = rsQry![Activity]
ImageCategory = rsQry![ImageCategory]
Path = rsQry![Path]
WhenTaken = rsQry![WhenTaken]
ImageCaption = rsQry![ImageCaption]
'Set variables to record set for access table
With rsQry
Set riTbl = CurrentDb.OpenRecordset("ttReconImagesForm_tbl")
Do Until .EOF
riTbl.AddNew
riTbl![idLocale] = idLocale
riTbl![idRecon] = idRecon
riTbl![imageid] = imageid
riTbl![Activity] = Activity
riTbl![ImageCategory] = ImageCategory
riTbl![Path] = Path
riTbl![WhenTaken] = WhenTaken
riTbl![ImageCaption] = ImageCaption
riTbl.Update
rsQry.MoveNext
Loop
End With
*************************
Thanks,
Dom Torrez



John Spencer said:
Your code should look something like this.

With rsQry
Set riTbl = CurrentDb.OpenRecordset("ttReconImagesForm_tbl")
Do Until .EOF
riTbl.AddNew
riTbl![idLocale] = idLocale
riTbl![idRecon] = idRecon
riTbl![imageid] = imageid
riTbl![Activity] = Activity
riTbl![ImageCategory] = ImageCategory
riTbl![Path] = Path
riTbl![WhenTaken] = WhenTaken
riTbl![ImageCaption] = ImageCaption
riTbl.Update
rsQry.MoveNext
Loop
End With

Does it?

Dom Torrez said:
Hi Martin,
I followed your suggestion and still get the same issue. My code
cycles
through the loop, it just does so while inserting the same recordset
row
into
each new row in the table. My table ends up having many rows with the
same
records. I was just reading about the .getrows method to copy and or
get
multiple rows from a recordset. Is this the route I should be taking?
If
so
how would I use this?
thanks

:

It looks like you need to move the rst.Query.MoveNext inside your
loop.
As
it stands, you are looping thru building your table, but not advancing
to
the
next record in the query.

Hope this helps.


:

hello
I'm trying to use a recordset to insert values into my access table.
I
can
get the insert working somewhat correctly, problem is that the
inserted
records are all the same. I believe my problem is in my loop. it
seems like
I need pointers to cycle down through my recordset rows, which isn't
happening. Can some one tell me where i'm going wrong? Like I said
my
insert into the access table from the recordset is working
partially, I
just
get multiple rows of one record rather than multiple rows of
different
records.

***************
With rsQry
Do Until .EOF
Set riTbl = CurrentDb.OpenRecordset("ttReconImagesForm_tbl")
riTbl.AddNew
riTbl![idLocale] = idLocale
riTbl![idRecon] = idRecon
riTbl![imageid] = imageid
riTbl![Activity] = Activity
riTbl![ImageCategory] = ImageCategory
riTbl![Path] = Path
riTbl![WhenTaken] = WhenTaken
riTbl![ImageCaption] = ImageCaption
riTbl.Update
riTbl.MoveNext
Loop
rsQry.MoveNext
End With
*************
I'd appreciate any help offered. Thanks
Dom Torrez
 
D

Dom Torrez

OK thanks for all the help......
I figured out my mistake. I was assigning my record set to only one set of
variables. because of this only the first row in the record set was being
saved into my variables. So when I assigned my variables into my table there
was only one row of data in memory. This would loop 39 time because I was
saying until EOF on my record set, which actually had 39 rows of data. But
it would only save one row 39 times because of my goofy use of the variables.
I don't need to use the variables, i just use the following:
riTbl![field1] = rsQry![field1]
riTbl![field2] = rsQry![field2]
riTbl![field3] = rsQry![field3]
.....

and have that contained in my loop. now my table is populated with 39 rows
of data all unique, no copies.
Again thanks for all the help. It is much appreciated.
Dom Torrez


Dom Torrez said:
Hi John,
Yes my code looks just like this. My access table is getting populated with
38 rows all the same record. An example is the value for my field imageid
is unique to each record, the table has 38 rows all with imageid being the
same. as well as all my other fields which should be unique. It seems as if
my code is only looping through the first record in the recordset. As a side
note the record set is displaying the data correclty on my form, ie...no
duplicate records and all records are visible. Since the data is being sent
to the form correctly is there a way to save the data from the form into an
access table? I attached my full code below.

*******************************************
Private Sub Form_Open(Cancel As Integer)
'variables
Dim db As Database
Dim rsQry As DAO.Recordset
Dim qryDef As DAO.QueryDef
Dim sQuery As String
Dim riTbl As DAO.Recordset
'Table column values
Dim idLocale As Integer
Dim idRecon As Integer
Dim imageid As Integer
Dim Activity As String
Dim ImageCategory As Variant
Dim Path As String
Dim WhenTaken As Date
Dim ImageCaption As String
'SQL query.
sQuery = "select idlocale, idrecon, imageid, activity, imagecategory, path ,
whentaken, imagecaption from getReconImages(" &
Forms![LocalesDataEntry]![ReconnaissanceDataEntrySF]![idRecon] & ");"
Debug.Print sQuery
Set qryDef = CurrentDb.CreateQueryDef("")
'setting values
qryDef.Connect =
"ODBC;DSN=@@@;DATABASE=@@@;SERVER=@@@;PORT=5432;SSLMODE=prefer;"
qryDef.SQL = sQuery
Debug.Print sQuery
Set rsQry = qryDef.OpenRecordset
Set Me.Recordset = rsQry
'Set variables to recordset values
idLocale = rsQry![idLocale]
idRecon = rsQry![idRecon]
imageid = rsQry![imageid]
Activity = rsQry![Activity]
ImageCategory = rsQry![ImageCategory]
Path = rsQry![Path]
WhenTaken = rsQry![WhenTaken]
ImageCaption = rsQry![ImageCaption]
'Set variables to record set for access table
With rsQry
Set riTbl = CurrentDb.OpenRecordset("ttReconImagesForm_tbl")
Do Until .EOF
riTbl.AddNew
riTbl![idLocale] = idLocale
riTbl![idRecon] = idRecon
riTbl![imageid] = imageid
riTbl![Activity] = Activity
riTbl![ImageCategory] = ImageCategory
riTbl![Path] = Path
riTbl![WhenTaken] = WhenTaken
riTbl![ImageCaption] = ImageCaption
riTbl.Update
rsQry.MoveNext
Loop
End With
*************************
Thanks,
Dom Torrez



John Spencer said:
Your code should look something like this.

With rsQry
Set riTbl = CurrentDb.OpenRecordset("ttReconImagesForm_tbl")
Do Until .EOF
riTbl.AddNew
riTbl![idLocale] = idLocale
riTbl![idRecon] = idRecon
riTbl![imageid] = imageid
riTbl![Activity] = Activity
riTbl![ImageCategory] = ImageCategory
riTbl![Path] = Path
riTbl![WhenTaken] = WhenTaken
riTbl![ImageCaption] = ImageCaption
riTbl.Update
rsQry.MoveNext
Loop
End With

Does it?

Dom Torrez said:
Hi Martin,
I followed your suggestion and still get the same issue. My code cycles
through the loop, it just does so while inserting the same recordset row
into
each new row in the table. My table ends up having many rows with the
same
records. I was just reading about the .getrows method to copy and or get
multiple rows from a recordset. Is this the route I should be taking? If
so
how would I use this?
thanks

:

It looks like you need to move the rst.Query.MoveNext inside your loop.
As
it stands, you are looping thru building your table, but not advancing to
the
next record in the query.

Hope this helps.


:

hello
I'm trying to use a recordset to insert values into my access table. I
can
get the insert working somewhat correctly, problem is that the inserted
records are all the same. I believe my problem is in my loop. it
seems like
I need pointers to cycle down through my recordset rows, which isn't
happening. Can some one tell me where i'm going wrong? Like I said my
insert into the access table from the recordset is working partially, I
just
get multiple rows of one record rather than multiple rows of different
records.

***************
With rsQry
Do Until .EOF
Set riTbl = CurrentDb.OpenRecordset("ttReconImagesForm_tbl")
riTbl.AddNew
riTbl![idLocale] = idLocale
riTbl![idRecon] = idRecon
riTbl![imageid] = imageid
riTbl![Activity] = Activity
riTbl![ImageCategory] = ImageCategory
riTbl![Path] = Path
riTbl![WhenTaken] = WhenTaken
riTbl![ImageCaption] = ImageCaption
riTbl.Update
riTbl.MoveNext
Loop
rsQry.MoveNext
End With
*************
I'd appreciate any help offered. Thanks
Dom Torrez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top