Breaking down imported information

  • Thread starter Russ via AccessMonster.com
  • Start date
R

Russ via AccessMonster.com

Any help in breaking down this information would be a BIG help!

Ok, I import a text file with the following information into a table;

Emp Depart Date Start Time Proc Item Scan Qty End Time
11218 11085 05-27-2008 09:02:17 A LLB-AA 55
11218 11085 05-27-2008 09:02:17 A LLB-Ab 65
11218 11085 05-27-2008 09:02:17 A LLB-Ac 75 09:06:17

11218 11085 05-27-2008 10:02:17 B LLB-AA 55
11218 11085 05-27-2008 10:02:17 B LLB-Ab 65
11218 11085 05-27-2008 10:02:17 B LLB-Ac 75 10:08:17

11218 11085 05-27-2008 11:02:17 C LLB-AA 55
11218 11085 05-27-2008 11:02:17 C LLB-Ab 65
11218 11085 05-27-2008 11:02:17 C LLB-Ac 75 11:06:17

11218 11085 05-27-2008 12:02:17 D LLB-AA 55
11218 11085 05-27-2008 12:02:17 D LLB-Ab 65
11218 11085 05-27-2008 12:02:17 D LLB-Ac 75 12:06:17

11218 11085 05-27-2008 13:02:17 D LLB-AA 55
11218 11085 05-27-2008 13:02:17 D LLB-Ab 65
11218 11085 05-27-2008 13:02:17 D LLB-Ac 75 13:06:17

11218 11085 05-27-2008 14:02:17 D N/A 0 14:06:17

11218 11085 05-27-2008 15:02:17 D Part1 32
11218 11085 05-27-2008 15:02:17 D Part2 64
11218 11085 05-27-2008 15:02:17 D Part3 365 15:06:17

My first question is how to determine the beginning and end of each record?
Note: there is a carriage return after each record , should this stay or be
removed.
Can it be used to determine the end of a record?
Can the beginning and end time be used somehow?

My second question is how could I break this down into two parts;
Part one the main record - [emp], [department],[date],[start time]
Part two details the record – [item scan], [qty]
 
S

Steve Sanford

Hi Russ,
My first question is how to determine the beginning and end of each record?
Note: there is a carriage return after each record , should this stay or be
removed.
Can it be used to determine the end of a record?
Can the beginning and end time be used somehow?

What are you defining as a record? In Access, each line in a text file that
is imported into a table becomes a record. A line in a text file is
terminated by a carrige return.

My second question is how could I break this down into two parts;
Part one the main record - [emp], [department],[date],[start time]
Part two details the record – [item scan], [qty]

IF I understand right, you want to put the fields [emp],
[department],[date],[start time] (and maybe [end time]) in one table and the
related fields [item scan], [qty] in another table? As in a normalized
structure?

I would use code to open a recordset and loop through each record, creating
one record in the One table, getting the PK for the record, then creating the
child records in the Many table (adding the FK).

Something like this (AIR code)
' -------code beg------------
Public Sub SplitTable()
Dim db As DAO.Database
Dim rsTEMP As DAO.Recordset
Dim rsMASTER As DAO.Recordset

Dim sSQL1 As String
Dim sSQL2 As String
Dim sSQL3 As String

Dim vEmp As Long
Dim vDepart As Long
Dim vdteDate As Date
Dim vStartTime As Date

Dim rsMASTER_PK As Long

Set db = CurrentDb

' open the TEMP table recordset
sSQL1 = "SELECT ID, Emp, Depart, dteDate, StartTime, Proc, ItemScan, Qty,
EndTime"
sSQL1 = sSQL1 & " FROM tblTEMP"
sSQL1 = sSQL1 & " ORDER BY Emp, Depart, dteDate, StartTime, Proc,
ItemScan;"

Set rsTEMP = db.OpenRecordset(sSQL1)

' open the MASTER table recordset
sSQL2 = "SELECT ID, Emp, Depart, dteDate, StartTime"
sSQL2 = sSQL2 & " FROM tblMASTER"
Set rsMASTER = db.OpenRecordset(sSQL2)

' check for records in rsTEMP
If Not rsTEMP.BOF And Not rsTEMP.EOF Then
rsTEMP.MoveFirst

' outer loop - for MASTER table records
Do
vEmp = rsTEMP.Fields("EMP")
vDepart = rsTEMP.Fields("Depart")
vdteDate = rsTEMP.Fields("dteDate")
vStartTime = rsTEMP.Fields("StartTime")

' insert a new record into the ONE table
With rsMASTER
.AddNew

.Fields("emp") = rsTEMP.Fields("EMP")
.Fields("Depart") = rsTEMP.Fields("Depart")
.Fields("dteDate") = rsTEMP.Fields("dteDate")
.Fields("StartTime") = rsTEMP.Fields("StartTime")

.Update 'saves new record

' find the last record added
.Bookmark = .LastModified

' get the PK for use in the child table
rsMASTER_PK = .Fields("ID")
End With

' inner loop for child table records
'the previous value must equal the current values
' to add related records to the child table
Do While vEmp = rsTEMP.Fields("EMP") And _
vDepart = rsTEMP.Fields("Depart") And _
vdteDate = rsTEMP.Fields("dteDate") And _
vStartTime = rsTEMP.Fields("StartTime")

' insert a new record into the child (MANY) table
sSQL3 = "INSERT INTO tblChild (Proc, ItemScan, Qty, LinkFK)"
sSQL3 = sSQL3 & " VALUES ('" & rsTEMP.Fields("Proc") & "', '" &
rsTEMP.Fields("ItemScan")
sSQL3 = sSQL3 & "', " & rsTEMP.Fields("Qty") & ", " &
rsMASTER_PK & ") ;"

db.Execute sSQL3, dbFailOnError

rsTEMP.MoveNext

' check if at EOF of rsTEMP
If rsTEMP.EOF Then
Exit Do
End If
Loop ' inner

If rsTEMP.EOF Then
Exit Do
End If
Loop ' outer
End If ' check for records in rsTEMP

'clean up
rsTEMP.Close
rsMASTER.Close
Set rsTEMP = Nothing
Set rsMASTER = Nothing
Set db = Nothing
End Sub
' -------code end------------

HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Russ via AccessMonster.com said:
Any help in breaking down this information would be a BIG help!

Ok, I import a text file with the following information into a table;

Emp Depart Date Start Time Proc Item Scan Qty End Time
11218 11085 05-27-2008 09:02:17 A LLB-AA 55
11218 11085 05-27-2008 09:02:17 A LLB-Ab 65
11218 11085 05-27-2008 09:02:17 A LLB-Ac 75 09:06:17

11218 11085 05-27-2008 10:02:17 B LLB-AA 55
11218 11085 05-27-2008 10:02:17 B LLB-Ab 65
11218 11085 05-27-2008 10:02:17 B LLB-Ac 75 10:08:17

11218 11085 05-27-2008 11:02:17 C LLB-AA 55
11218 11085 05-27-2008 11:02:17 C LLB-Ab 65
11218 11085 05-27-2008 11:02:17 C LLB-Ac 75 11:06:17

11218 11085 05-27-2008 12:02:17 D LLB-AA 55
11218 11085 05-27-2008 12:02:17 D LLB-Ab 65
11218 11085 05-27-2008 12:02:17 D LLB-Ac 75 12:06:17

11218 11085 05-27-2008 13:02:17 D LLB-AA 55
11218 11085 05-27-2008 13:02:17 D LLB-Ab 65
11218 11085 05-27-2008 13:02:17 D LLB-Ac 75 13:06:17

11218 11085 05-27-2008 14:02:17 D N/A 0 14:06:17

11218 11085 05-27-2008 15:02:17 D Part1 32
11218 11085 05-27-2008 15:02:17 D Part2 64
11218 11085 05-27-2008 15:02:17 D Part3 365 15:06:17

My first question is how to determine the beginning and end of each record?
Note: there is a carriage return after each record , should this stay or be
removed.
Can it be used to determine the end of a record?
Can the beginning and end time be used somehow?

My second question is how could I break this down into two parts;
Part one the main record - [emp], [department],[date],[start time]
Part two details the record – [item scan], [qty]
 
R

Russ via AccessMonster.com

Steve, thanks for the help...
IF I understand right, you want to put the fields [emp],
[department],[date],[start time] (and maybe [end time]) in one table and the
related fields [item scan], [qty] in another table? As in a normalized
structure?

yes, this is correct.

one problem I have is there is no primary key, I am using this code to import
the data and it does not create a primary key;
DoCmd.TransferText acImportDelim, , "DataViaCode", "C:\data.txt", True
it is creating the table using the first line as the field names.

Do not understad, still new with working with recordsets....
' open the TEMP table recordset
sSQL1 = "SELECT ID, Emp, Depart, dteDate, StartTime, Proc, ItemScan, Qty,
EndTime"
sSQL1 = sSQL1 & " FROM tblTEMP"
sSQL1 = sSQL1 & " ORDER BY Emp, Depart, dteDate, StartTime, Proc,
ItemScan;"
' open the MASTER table recordset
sSQL2 = "SELECT ID, Emp, Depart, dteDate, StartTime"
sSQL2 = sSQL2 & " FROM tblMASTER"
Set rsMASTER = db.OpenRecordset(sSQL2)

Is the temp table the information I imported? I assume that the master is my
table called DataViaCode? Or is this the beginning of the separation of the
information? Do I need to create the mater table?

Again, thanks for the help.

Russ



Steve said:
Hi Russ,
My first question is how to determine the beginning and end of each record?
Note: there is a carriage return after each record , should this stay or be
removed.
Can it be used to determine the end of a record?
Can the beginning and end time be used somehow?

What are you defining as a record? In Access, each line in a text file that
is imported into a table becomes a record. A line in a text file is
terminated by a carrige return.
My second question is how could I break this down into two parts;
Part one the main record - [emp], [department],[date],[start time]
Part two details the record – [item scan], [qty]

IF I understand right, you want to put the fields [emp],
[department],[date],[start time] (and maybe [end time]) in one table and the
related fields [item scan], [qty] in another table? As in a normalized
structure?

I would use code to open a recordset and loop through each record, creating
one record in the One table, getting the PK for the record, then creating the
child records in the Many table (adding the FK).

Something like this (AIR code)
' -------code beg------------
Public Sub SplitTable()
Dim db As DAO.Database
Dim rsTEMP As DAO.Recordset
Dim rsMASTER As DAO.Recordset

Dim sSQL1 As String
Dim sSQL2 As String
Dim sSQL3 As String

Dim vEmp As Long
Dim vDepart As Long
Dim vdteDate As Date
Dim vStartTime As Date

Dim rsMASTER_PK As Long

Set db = CurrentDb

' open the TEMP table recordset
sSQL1 = "SELECT ID, Emp, Depart, dteDate, StartTime, Proc, ItemScan, Qty,
EndTime"
sSQL1 = sSQL1 & " FROM tblTEMP"
sSQL1 = sSQL1 & " ORDER BY Emp, Depart, dteDate, StartTime, Proc,
ItemScan;"

Set rsTEMP = db.OpenRecordset(sSQL1)

' open the MASTER table recordset
sSQL2 = "SELECT ID, Emp, Depart, dteDate, StartTime"
sSQL2 = sSQL2 & " FROM tblMASTER"
Set rsMASTER = db.OpenRecordset(sSQL2)

' check for records in rsTEMP
If Not rsTEMP.BOF And Not rsTEMP.EOF Then
rsTEMP.MoveFirst

' outer loop - for MASTER table records
Do
vEmp = rsTEMP.Fields("EMP")
vDepart = rsTEMP.Fields("Depart")
vdteDate = rsTEMP.Fields("dteDate")
vStartTime = rsTEMP.Fields("StartTime")

' insert a new record into the ONE table
With rsMASTER
.AddNew

.Fields("emp") = rsTEMP.Fields("EMP")
.Fields("Depart") = rsTEMP.Fields("Depart")
.Fields("dteDate") = rsTEMP.Fields("dteDate")
.Fields("StartTime") = rsTEMP.Fields("StartTime")

.Update 'saves new record

' find the last record added
.Bookmark = .LastModified

' get the PK for use in the child table
rsMASTER_PK = .Fields("ID")
End With

' inner loop for child table records
'the previous value must equal the current values
' to add related records to the child table
Do While vEmp = rsTEMP.Fields("EMP") And _
vDepart = rsTEMP.Fields("Depart") And _
vdteDate = rsTEMP.Fields("dteDate") And _
vStartTime = rsTEMP.Fields("StartTime")

' insert a new record into the child (MANY) table
sSQL3 = "INSERT INTO tblChild (Proc, ItemScan, Qty, LinkFK)"
sSQL3 = sSQL3 & " VALUES ('" & rsTEMP.Fields("Proc") & "', '" &
rsTEMP.Fields("ItemScan")
sSQL3 = sSQL3 & "', " & rsTEMP.Fields("Qty") & ", " &
rsMASTER_PK & ") ;"

db.Execute sSQL3, dbFailOnError

rsTEMP.MoveNext

' check if at EOF of rsTEMP
If rsTEMP.EOF Then
Exit Do
End If
Loop ' inner

If rsTEMP.EOF Then
Exit Do
End If
Loop ' outer
End If ' check for records in rsTEMP

'clean up
rsTEMP.Close
rsMASTER.Close
Set rsTEMP = Nothing
Set rsMASTER = Nothing
Set db = Nothing
End Sub
' -------code end------------

HTH
Any help in breaking down this information would be a BIG help!
[quoted text clipped - 36 lines]
Part one the main record - [emp], [department],[date],[start time]
Part two details the record – [item scan], [qty]
 
S

Steve Sanford

I guess I didn't do so well explaining the code...... :(

Important: you should have a BackUp of your database. One more time: you
should have a BackUp of your database. Do your testing on a copy of the mdb
in case the code runs amok.......

Remember "The Three Rules of Computing"...

1) Backup
2) Backup and
3) Backup :D


You will need a reference set to Microsoft DAO 3.6 Object Library.

The import table, "DataViaCode", does not need a primary key field. But the
"Master" table does; the "Child" table should.

Also, "Date" is a reserved word in Access/SQL and shouldn't be used for
object names. A list of reserved words is at

http://allenbrowne.com/AppIssueBadWord.html

If you use one of the many naming conventions this won't be a problem.


So, "tblTEMP" is what I used for the name of the import table; it looks like
you used "DataViaCode".

What I called "tblMASTER", is the table that has the fields [Emp], [Depart],
[dteDate] and [StartTime]. "ID" is an autonumber type field - this is the
field that is "linked" to the table that has the fields [Proc], [ItemScan]
and [Qty] (the Child table).

What did you name this table?
What are the field names and types? (text,date/time,number)


The table that has the fields [Proc], [ItemScan] and [Qty], I called
"tblChild". This table also has a field [LinkFK] (can be any name) that links
the records to the "Master" (the One) table. There is an autonumber type
field - I used "ID".

What did you name this table?
What are the field names and types? (text,date/time,number)


What the code does is opens a recordset based on the import table, sorted by
-> Emp, Depart, dteDate, StartTime, Proc, ItemScan

Then it opens another recordset based on the "master" (one) table.

A record is read from the import table and the fields [Emp], [Depart],
[dteDate] and [StartTime] are inserted into the "master" table and also saved
in variables. The PK (primary key) of that record is saved in a variable.
Then a record is added to the "child" table.

The next record in the import table is read. If the current record fields
[Emp], [Depart], [dteDate] and [StartTime] match the data in the variables,
another record is added to the "child" table, linked to the "master" table
via the field [LinkFK].

If (when) the current record fields don't match the data in the variables, a
new record is added to the "master" table and also a record in the "child"
table.

This continues until the end of file marker is reached.


If you have more questions about the code or have problems modifing the
code, post back. Once you start using recordsets, you will be amazed at what
you can do...

HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Russ via AccessMonster.com said:
Steve, thanks for the help...
IF I understand right, you want to put the fields [emp],
[department],[date],[start time] (and maybe [end time]) in one table and the
related fields [item scan], [qty] in another table? As in a normalized
structure?

yes, this is correct.

one problem I have is there is no primary key, I am using this code to import
the data and it does not create a primary key;
DoCmd.TransferText acImportDelim, , "DataViaCode", "C:\data.txt", True
it is creating the table using the first line as the field names.

Do not understad, still new with working with recordsets....
' open the TEMP table recordset
sSQL1 = "SELECT ID, Emp, Depart, dteDate, StartTime, Proc, ItemScan, Qty,
EndTime"
sSQL1 = sSQL1 & " FROM tblTEMP"
sSQL1 = sSQL1 & " ORDER BY Emp, Depart, dteDate, StartTime, Proc,
ItemScan;"
' open the MASTER table recordset
sSQL2 = "SELECT ID, Emp, Depart, dteDate, StartTime"
sSQL2 = sSQL2 & " FROM tblMASTER"
Set rsMASTER = db.OpenRecordset(sSQL2)

Is the temp table the information I imported? I assume that the master is my
table called DataViaCode? Or is this the beginning of the separation of the
information? Do I need to create the mater table?

Again, thanks for the help.

Russ



Steve said:
Hi Russ,
My first question is how to determine the beginning and end of each record?
Note: there is a carriage return after each record , should this stay or be
removed.
Can it be used to determine the end of a record?
Can the beginning and end time be used somehow?

What are you defining as a record? In Access, each line in a text file that
is imported into a table becomes a record. A line in a text file is
terminated by a carrige return.
My second question is how could I break this down into two parts;
Part one the main record - [emp], [department],[date],[start time]
Part two details the record – [item scan], [qty]

IF I understand right, you want to put the fields [emp],
[department],[date],[start time] (and maybe [end time]) in one table and the
related fields [item scan], [qty] in another table? As in a normalized
structure?

I would use code to open a recordset and loop through each record, creating
one record in the One table, getting the PK for the record, then creating the
child records in the Many table (adding the FK).

Something like this (AIR code)
' -------code beg------------
Public Sub SplitTable()
Dim db As DAO.Database
Dim rsTEMP As DAO.Recordset
Dim rsMASTER As DAO.Recordset

Dim sSQL1 As String
Dim sSQL2 As String
Dim sSQL3 As String

Dim vEmp As Long
Dim vDepart As Long
Dim vdteDate As Date
Dim vStartTime As Date

Dim rsMASTER_PK As Long

Set db = CurrentDb

' open the TEMP table recordset
sSQL1 = "SELECT ID, Emp, Depart, dteDate, StartTime, Proc, ItemScan, Qty,
EndTime"
sSQL1 = sSQL1 & " FROM tblTEMP"
sSQL1 = sSQL1 & " ORDER BY Emp, Depart, dteDate, StartTime, Proc,
ItemScan;"

Set rsTEMP = db.OpenRecordset(sSQL1)

' open the MASTER table recordset
sSQL2 = "SELECT ID, Emp, Depart, dteDate, StartTime"
sSQL2 = sSQL2 & " FROM tblMASTER"
Set rsMASTER = db.OpenRecordset(sSQL2)

' check for records in rsTEMP
If Not rsTEMP.BOF And Not rsTEMP.EOF Then
rsTEMP.MoveFirst

' outer loop - for MASTER table records
Do
vEmp = rsTEMP.Fields("EMP")
vDepart = rsTEMP.Fields("Depart")
vdteDate = rsTEMP.Fields("dteDate")
vStartTime = rsTEMP.Fields("StartTime")

' insert a new record into the ONE table
With rsMASTER
.AddNew

.Fields("emp") = rsTEMP.Fields("EMP")
.Fields("Depart") = rsTEMP.Fields("Depart")
.Fields("dteDate") = rsTEMP.Fields("dteDate")
.Fields("StartTime") = rsTEMP.Fields("StartTime")

.Update 'saves new record

' find the last record added
.Bookmark = .LastModified

' get the PK for use in the child table
rsMASTER_PK = .Fields("ID")
End With

' inner loop for child table records
'the previous value must equal the current values
' to add related records to the child table
Do While vEmp = rsTEMP.Fields("EMP") And _
vDepart = rsTEMP.Fields("Depart") And _
vdteDate = rsTEMP.Fields("dteDate") And _
vStartTime = rsTEMP.Fields("StartTime")

' insert a new record into the child (MANY) table
sSQL3 = "INSERT INTO tblChild (Proc, ItemScan, Qty, LinkFK)"
sSQL3 = sSQL3 & " VALUES ('" & rsTEMP.Fields("Proc") & "', '" &
rsTEMP.Fields("ItemScan")
sSQL3 = sSQL3 & "', " & rsTEMP.Fields("Qty") & ", " &
rsMASTER_PK & ") ;"

db.Execute sSQL3, dbFailOnError

rsTEMP.MoveNext

' check if at EOF of rsTEMP
If rsTEMP.EOF Then
Exit Do
End If
Loop ' inner

If rsTEMP.EOF Then
Exit Do
End If
Loop ' outer
End If ' check for records in rsTEMP

'clean up
rsTEMP.Close
rsMASTER.Close
Set rsTEMP = Nothing
Set rsMASTER = Nothing
Set db = Nothing
End Sub
' -------code end------------

HTH
Any help in breaking down this information would be a BIG help!
[quoted text clipped - 36 lines]
Part one the main record - [emp], [department],[date],[start time]
Part two details the record – [item scan], [qty]
 
R

Russ via AccessMonster.com

Steve,
Great job, the code seems to working like I need it, I have further commented
the code trying to understand and document all the logic behind it. As I said
I am still new, so trying to get an understanding to how it all works.
Hopefully I am correct on the process and comments. One question I have is on
the inner and outer loops, can you explain the difference and the use of them?
Sure do appreciate all your help!
Russ

'******************************************************************************************
' For this codde to work we need a reference set to Microsoft DAO 3.6 Object
Library
'
' Step 1 We will need to set-up some recordsets. i.e. "rsDataViaCode" &
rsMASTER
' Create / Open First recordset "rsDataViaCode"
' Create / Open Second recordset "rsMASTER"
' Create / Open Third recordset "rsDataViaCode"
' What is a Recordset?
' ' A recordset represents the entire set of records from a base table or
the results
' ' of an executed command. At any time, the Recordset object refers to
only a single
' ' record within the set as the current record.
'
' Step 2 We need to add / insert new record into the "tblMaster" table
' To Accomplish this we will use the AddNew method.
' What does the AddNew method do?
' ' It tells Microsoft Access database engine to prepare a new blank
record.
' ' The new record is then made the current record
'
' Step 3 We will need to set a bookmark.
' What is a bookmark?
' ' A Bookmark sets or returns a bookmark that uniquely identifies
' ' the current record in a Recordset object.
'
' Step 4 We will need to insert records into thet "tblChild" Table.
' How do we use the insert?
' ' There are essentially two methods for adding records to a table. The
first
' ' is to add one record at a time; the second is to add many records at a
time.
' ' In both cases, you use the SQL statement INSERT INTO to accomplish the
task.
' ' INSERT INTO statements are commonly referred to as append queries.
' ' To add one record to a table, you must use the field list to define
which
' ' fields to put the data in, and then you must supply the data itself in
a
' ' value list. To define the value list, use the VALUES clause.
'
' Step 5 Ok, were done. Time to clean up, we need to close the recordsets.
' To Accomplish this we will use the Close method & Nothing Keyword.
' ' The Close method is used to Closes an open Recordset
' ' The Nothing keyword is used to disassociate an object
' ' variable from an actual object.
'******************************************************************************************

'Declares variables and allocates storage space
Dim db As DAO.Database
Dim rsDataViaCode As DAO.Recordset
Dim rsMASTER As DAO.Recordset

Dim sSQL1 As String
Dim sSQL2 As String
Dim sSQL3 As String

Dim vEmp As String ' Can be text or number.
Dim vDepart As String ' Can be text or number.
Dim vStartDate As Date ' Can only be a date.
Dim vStartTime As Date ' Can only be a date.
Dim vProc As String ' Can be text or number.
Dim rsMASTER_PK As Long ' Can only be a number.

' Set db (DAO.Database) using the CurrentDb method which returns an
' object variable of type Database that represents the database
' currently open in the Microsoft Access window.
Set db = CurrentDb

'******************************************************************************************
' Open the DataViaCode table recordset.
' Step 1) Set-up our query (sSQL1)
' Step 2) Set Recprdset "rsDataViaCode"
'******************************************************************************************

' Set-up the sSQL1 Select query.
' Here we will select all fields / records in the table.
sSQL1 = "SELECT * FROM DataViaCode"

' Set the Recordset rsDataViaCode using db.OpenRecordset which creates a
' new Recordset object and appends it to the Recordsets collection.
Set rsDataViaCode = db.OpenRecordset(sSQL1)

'******************************************************************************************
' Open the MASTER table recordset.
' Step 1) Set-up our query (sSQL2)
' Step 2) Set Recprdset "rsMASTER"
'******************************************************************************************

' Set-up the sSQL2 Select query.
' sSQL2 statement includes parameters for the query.
' Here we will select fields we want to update from the "tblMASTER" table.
sSQL2 = "SELECT ID, Emp, Department, StartDate, StartTime, Proc"
sSQL2 = sSQL2 & " FROM tblMASTER"

' Set Recordset rsMASTER using db.OpenRecordset which creates a new
' Recordset object and appends it to the Recordsets collection.
Set rsMASTER = db.OpenRecordset(sSQL2)

'******************************************************************************************
' Check for records in rsDataViaCode.
' We will use the BOF and EOF properties to determine whether a
' Recordset object contains records or whether you've gone beyond the
' limits of a Recordset object when you move from record to record.
'******************************************************************************************

' If rsDataViaCode is not at the beginning of the file or is not at
' the end of the file then continue and move to first record.
If Not rsDataViaCode.BOF And Not rsDataViaCode.EOF Then

' Here we move to the first record in our Recordset object
' and make that record the current record.
rsDataViaCode.MoveFirst

' outer loop - for tblMASTER table records
Do
vEmp = rsDataViaCode.Fields("EMP")
vDepart = rsDataViaCode.Fields("Department")
vStartDate = rsDataViaCode.Fields("StartDate")
vStartTime = rsDataViaCode.Fields("StartTime")
vProc = rsDataViaCode.Fields("Proc")

'******************************************************************************************
' Insert a new record into the ONE (tblMASTER) table.
' We will accomplish this by doing the following steps;
' 1) Use the AddNew method to create a record you can edit.
' 2) Assign values to each of the record's fields.
' 3) Use the Update method to save the new record.
'******************************************************************************************

' The With statement allows you to perform a series of statements on
a specified
' object without requalifying the name of the object. Allows us to
refer to the
' object once instead of referring to it with each property
assignment.
With rsMASTER ' Here we are referring to the object just once.

' Step 1) Use the AddNew method
' Database engine prepares a new blank record & makes it the
current record.
.AddNew 'since we used the With statement we do not have to refer
to "rsMaster"

' Step 2) Assign values to each of the record's fields.
.Fields("emp") = rsDataViaCode.Fields("EMP")
.Fields("Department") = rsDataViaCode.Fields("Department")
.Fields("StartDate") = rsDataViaCode.Fields("StartDate")
.Fields("StartTime") = rsDataViaCode.Fields("StartTime")
.Fields("Proc") = rsDataViaCode.Fields("Proc")

' Step 3) Use the Update method to save the new record.
.Update 'saves new record

' Step 4) Find the last record added.
' We will use this bookmark to move the current record pointer
' to the most recently changed or added record.
.Bookmark = .LastModified

' get the PK (Primary Key) for use in the child table
rsMASTER_PK = .Fields("ID")

End With

' inner loop for child table records
' the previous value must equal the current values
' to add related records to the child table
Do While vEmp = rsDataViaCode.Fields("EMP") And _
vDepart = rsDataViaCode.Fields("Department") And _
vStartDate = rsDataViaCode.Fields("StartDate") And _
vStartTime = rsDataViaCode.Fields("StartTime") And _
vProc = rsDataViaCode.Fields("Proc")

'******************************************************************************************
' Insert / Append a new record into the "tblChild" (MANY) table
' Use INSERT INTO This Adds a record or multiple records to a table.
This is referred
' to as an append query. Single-record append query:

'INSERT INTO target [(field1[, field2[, …]])] VALUES (value1[, value2[, …])

' TARGET
' The name of the table or query to append records to.

' VALUES
' The values to insert into the specific fields of the new record.
' Each value is inserted into the field that corresponds to the value's
' position in the list: value1 is inserted into field1 of the new record,
' value2 into field2, and so on. You must separate values with a comma, and
' enclose text fields in quotation marks (' ').
'******************************************************************************************

sSQL3 = "INSERT INTO tblChild (Proc, ItemScan, Qty, LinkFK)"
sSQL3 = sSQL3 & " VALUES ('" & rsDataViaCode.Fields("Proc") & "',
'" & rsDataViaCode.Fields("ItemScan")
sSQL3 = sSQL3 & "', " & rsDataViaCode.Fields("Qty") & ", " &
rsMASTER_PK & ") ;"

'Executes the specfied specifications in sSQL3
db.Execute sSQL3, dbFailOnError '(Runs the append query sSQL3)

' MoveNext - Moves to the next record in a specified Recordset
' object and make that record the current record
rsDataViaCode.MoveNext

' Check if at EOF (End Of File) of Recordset (rsDataViaCode)
' if at EOF (End Of File) Exit Do.
If rsDataViaCode.EOF Then
Exit Do ' Exit loop
End If
Loop ' inner

' if at EOF (End Of File) Exit Do.
If rsDataViaCode.EOF Then
Exit Do ' Exit loop
End If

Loop ' outer

End If

'******************************************************************************************
' Clean up, time to close the recordsets
' The Close Method is used to Closes an open Recordset
'******************************************************************************************
rsDataViaCode.Close
rsMASTER.Close
' The Nothing keyword is used to disassociate an object
' variable from an actual object.
Set rsDataViaCode = Nothing
Set rsMASTER = Nothing
Set db = Nothing

End Sub



Steve said:
I guess I didn't do so well explaining the code...... :(

Important: you should have a BackUp of your database. One more time: you
should have a BackUp of your database. Do your testing on a copy of the mdb
in case the code runs amok.......

Remember "The Three Rules of Computing"...

1) Backup
2) Backup and
3) Backup :D

You will need a reference set to Microsoft DAO 3.6 Object Library.

The import table, "DataViaCode", does not need a primary key field. But the
"Master" table does; the "Child" table should.

Also, "Date" is a reserved word in Access/SQL and shouldn't be used for
object names. A list of reserved words is at

http://allenbrowne.com/AppIssueBadWord.html

If you use one of the many naming conventions this won't be a problem.

So, "tblTEMP" is what I used for the name of the import table; it looks like
you used "DataViaCode".

What I called "tblMASTER", is the table that has the fields [Emp], [Depart],
[dteDate] and [StartTime]. "ID" is an autonumber type field - this is the
field that is "linked" to the table that has the fields [Proc], [ItemScan]
and [Qty] (the Child table).

What did you name this table?
What are the field names and types? (text,date/time,number)

The table that has the fields [Proc], [ItemScan] and [Qty], I called
"tblChild". This table also has a field [LinkFK] (can be any name) that links
the records to the "Master" (the One) table. There is an autonumber type
field - I used "ID".

What did you name this table?
What are the field names and types? (text,date/time,number)

What the code does is opens a recordset based on the import table, sorted by
-> Emp, Depart, dteDate, StartTime, Proc, ItemScan

Then it opens another recordset based on the "master" (one) table.

A record is read from the import table and the fields [Emp], [Depart],
[dteDate] and [StartTime] are inserted into the "master" table and also saved
in variables. The PK (primary key) of that record is saved in a variable.
Then a record is added to the "child" table.

The next record in the import table is read. If the current record fields
[Emp], [Depart], [dteDate] and [StartTime] match the data in the variables,
another record is added to the "child" table, linked to the "master" table
via the field [LinkFK].

If (when) the current record fields don't match the data in the variables, a
new record is added to the "master" table and also a record in the "child"
table.

This continues until the end of file marker is reached.

If you have more questions about the code or have problems modifing the
code, post back. Once you start using recordsets, you will be amazed at what
you can do...

HTH
Steve, thanks for the help...
[quoted text clipped - 166 lines]
Part one the main record - [emp], [department],[date],[start time]
Part two details the record – [item scan], [qty]
 
R

Russ via AccessMonster.com

Steve,
Having a little problem;
I need to capture the end time in the master field but some have nulls and
creating big problem for me.
This is because the end time is not used until the process is completed.
Example;
Emp Department StartDate StartTime Proc EndTime
11218 112182 05-28-2008 12:19:23 A
11218 112182 05-28-2008 12:19:23 A
11218 112182 05-28-2008 12:19:23 A 12:19:56
11218 112182 05-28-2008 12:20:00 B
11218 112182 05-28-2008 12:20:00 B
11218 112182 05-28-2008 12:20:00 B 12:20:21
11218 112182 05-28-2008 12:20:26 C
11218 112182 05-28-2008 12:20:26 C
Any help would be great.
Steve,
Great job, the code seems to working like I need it, I have further commented
the code trying to understand and document all the logic behind it. As I said
I am still new, so trying to get an understanding to how it all works.
Hopefully I am correct on the process and comments. One question I have is on
the inner and outer loops, can you explain the difference and the use of them?
Sure do appreciate all your help!
Russ

'******************************************************************************************
' For this codde to work we need a reference set to Microsoft DAO 3.6 Object
Library
'
' Step 1 We will need to set-up some recordsets. i.e. "rsDataViaCode" &
rsMASTER
' Create / Open First recordset "rsDataViaCode"
' Create / Open Second recordset "rsMASTER"
' Create / Open Third recordset "rsDataViaCode"
' What is a Recordset?
' ' A recordset represents the entire set of records from a base table or
the results
' ' of an executed command. At any time, the Recordset object refers to
only a single
' ' record within the set as the current record.
'
' Step 2 We need to add / insert new record into the "tblMaster" table
' To Accomplish this we will use the AddNew method.
' What does the AddNew method do?
' ' It tells Microsoft Access database engine to prepare a new blank
record.
' ' The new record is then made the current record
'
' Step 3 We will need to set a bookmark.
' What is a bookmark?
' ' A Bookmark sets or returns a bookmark that uniquely identifies
' ' the current record in a Recordset object.
'
' Step 4 We will need to insert records into thet "tblChild" Table.
' How do we use the insert?
' ' There are essentially two methods for adding records to a table. The
first
' ' is to add one record at a time; the second is to add many records at a
time.
' ' In both cases, you use the SQL statement INSERT INTO to accomplish the
task.
' ' INSERT INTO statements are commonly referred to as append queries.
' ' To add one record to a table, you must use the field list to define
which
' ' fields to put the data in, and then you must supply the data itself in
a
' ' value list. To define the value list, use the VALUES clause.
'
' Step 5 Ok, were done. Time to clean up, we need to close the recordsets.
' To Accomplish this we will use the Close method & Nothing Keyword.
' ' The Close method is used to Closes an open Recordset
' ' The Nothing keyword is used to disassociate an object
' ' variable from an actual object.
'******************************************************************************************

'Declares variables and allocates storage space
Dim db As DAO.Database
Dim rsDataViaCode As DAO.Recordset
Dim rsMASTER As DAO.Recordset

Dim sSQL1 As String
Dim sSQL2 As String
Dim sSQL3 As String

Dim vEmp As String ' Can be text or number.
Dim vDepart As String ' Can be text or number.
Dim vStartDate As Date ' Can only be a date.
Dim vStartTime As Date ' Can only be a date.
Dim vProc As String ' Can be text or number.
Dim rsMASTER_PK As Long ' Can only be a number.

' Set db (DAO.Database) using the CurrentDb method which returns an
' object variable of type Database that represents the database
' currently open in the Microsoft Access window.
Set db = CurrentDb

'******************************************************************************************
' Open the DataViaCode table recordset.
' Step 1) Set-up our query (sSQL1)
' Step 2) Set Recprdset "rsDataViaCode"
'******************************************************************************************

' Set-up the sSQL1 Select query.
' Here we will select all fields / records in the table.
sSQL1 = "SELECT * FROM DataViaCode"

' Set the Recordset rsDataViaCode using db.OpenRecordset which creates a
' new Recordset object and appends it to the Recordsets collection.
Set rsDataViaCode = db.OpenRecordset(sSQL1)

'******************************************************************************************
' Open the MASTER table recordset.
' Step 1) Set-up our query (sSQL2)
' Step 2) Set Recprdset "rsMASTER"
'******************************************************************************************

' Set-up the sSQL2 Select query.
' sSQL2 statement includes parameters for the query.
' Here we will select fields we want to update from the "tblMASTER" table.
sSQL2 = "SELECT ID, Emp, Department, StartDate, StartTime, Proc"
sSQL2 = sSQL2 & " FROM tblMASTER"

' Set Recordset rsMASTER using db.OpenRecordset which creates a new
' Recordset object and appends it to the Recordsets collection.
Set rsMASTER = db.OpenRecordset(sSQL2)

'******************************************************************************************
' Check for records in rsDataViaCode.
' We will use the BOF and EOF properties to determine whether a
' Recordset object contains records or whether you've gone beyond the
' limits of a Recordset object when you move from record to record.
'******************************************************************************************

' If rsDataViaCode is not at the beginning of the file or is not at
' the end of the file then continue and move to first record.
If Not rsDataViaCode.BOF And Not rsDataViaCode.EOF Then

' Here we move to the first record in our Recordset object
' and make that record the current record.
rsDataViaCode.MoveFirst

' outer loop - for tblMASTER table records
Do
vEmp = rsDataViaCode.Fields("EMP")
vDepart = rsDataViaCode.Fields("Department")
vStartDate = rsDataViaCode.Fields("StartDate")
vStartTime = rsDataViaCode.Fields("StartTime")
vProc = rsDataViaCode.Fields("Proc")

'******************************************************************************************
' Insert a new record into the ONE (tblMASTER) table.
' We will accomplish this by doing the following steps;
' 1) Use the AddNew method to create a record you can edit.
' 2) Assign values to each of the record's fields.
' 3) Use the Update method to save the new record.
'******************************************************************************************

' The With statement allows you to perform a series of statements on
a specified
' object without requalifying the name of the object. Allows us to
refer to the
' object once instead of referring to it with each property
assignment.
With rsMASTER ' Here we are referring to the object just once.

' Step 1) Use the AddNew method
' Database engine prepares a new blank record & makes it the
current record.
.AddNew 'since we used the With statement we do not have to refer
to "rsMaster"

' Step 2) Assign values to each of the record's fields.
.Fields("emp") = rsDataViaCode.Fields("EMP")
.Fields("Department") = rsDataViaCode.Fields("Department")
.Fields("StartDate") = rsDataViaCode.Fields("StartDate")
.Fields("StartTime") = rsDataViaCode.Fields("StartTime")
.Fields("Proc") = rsDataViaCode.Fields("Proc")

' Step 3) Use the Update method to save the new record.
.Update 'saves new record

' Step 4) Find the last record added.
' We will use this bookmark to move the current record pointer
' to the most recently changed or added record.
.Bookmark = .LastModified

' get the PK (Primary Key) for use in the child table
rsMASTER_PK = .Fields("ID")

End With

' inner loop for child table records
' the previous value must equal the current values
' to add related records to the child table
Do While vEmp = rsDataViaCode.Fields("EMP") And _
vDepart = rsDataViaCode.Fields("Department") And _
vStartDate = rsDataViaCode.Fields("StartDate") And _
vStartTime = rsDataViaCode.Fields("StartTime") And _
vProc = rsDataViaCode.Fields("Proc")

'******************************************************************************************
' Insert / Append a new record into the "tblChild" (MANY) table
' Use INSERT INTO This Adds a record or multiple records to a table.
This is referred
' to as an append query. Single-record append query:

'INSERT INTO target [(field1[, field2[, …]])] VALUES (value1[, value2[, …])

' TARGET
' The name of the table or query to append records to.

' VALUES
' The values to insert into the specific fields of the new record.
' Each value is inserted into the field that corresponds to the value's
' position in the list: value1 is inserted into field1 of the new record,
' value2 into field2, and so on. You must separate values with a comma, and
' enclose text fields in quotation marks (' ').
'******************************************************************************************

sSQL3 = "INSERT INTO tblChild (Proc, ItemScan, Qty, LinkFK)"
sSQL3 = sSQL3 & " VALUES ('" & rsDataViaCode.Fields("Proc") & "',
'" & rsDataViaCode.Fields("ItemScan")
sSQL3 = sSQL3 & "', " & rsDataViaCode.Fields("Qty") & ", " &
rsMASTER_PK & ") ;"

'Executes the specfied specifications in sSQL3
db.Execute sSQL3, dbFailOnError '(Runs the append query sSQL3)

' MoveNext - Moves to the next record in a specified Recordset
' object and make that record the current record
rsDataViaCode.MoveNext

' Check if at EOF (End Of File) of Recordset (rsDataViaCode)
' if at EOF (End Of File) Exit Do.
If rsDataViaCode.EOF Then
Exit Do ' Exit loop
End If
Loop ' inner

' if at EOF (End Of File) Exit Do.
If rsDataViaCode.EOF Then
Exit Do ' Exit loop
End If

Loop ' outer

End If

'******************************************************************************************
' Clean up, time to close the recordsets
' The Close Method is used to Closes an open Recordset
'******************************************************************************************
rsDataViaCode.Close
rsMASTER.Close
' The Nothing keyword is used to disassociate an object
' variable from an actual object.
Set rsDataViaCode = Nothing
Set rsMASTER = Nothing
Set db = Nothing

End Sub
I guess I didn't do so well explaining the code...... :(
[quoted text clipped - 70 lines]
Part one the main record - [emp], [department],[date],[start time]
Part two details the record – [item scan], [qty]
 
S

Steve Sanford

Add the new lines between the "####" :

<snip
'******************************************************************************************
' Insert / Append a new record into the "tblChild" (MANY) table
' Use INSERT INTO This Adds a record or multiple records to a table.
This is referred
' to as an append query. Single-record append query:

'INSERT INTO target [(field1[, field2[, …]])] VALUES (value1[, value2[, …])

' TARGET
' The name of the table or query to append records to.

' VALUES
' The values to insert into the specific fields of the new record.
' Each value is inserted into the field that corresponds to the value's
' position in the list: value1 is inserted into field1 of the new record,
' value2 into field2, and so on. You must separate values with a comma, and
' enclose text fields in quotation marks (' ')
'******************************************************************************************

sSQL3 = "INSERT INTO tblChild (Proc, ItemScan, Qty, LinkFK)"
sSQL3 = sSQL3 & " VALUES ('" & rsDataViaCode.Fields("Proc") & "',
'" & rsDataViaCode.Fields("ItemScan")
sSQL3 = sSQL3 & "', " & rsDataViaCode.Fields("Qty") & ", " &
rsMASTER_PK & ") ;"

'Executes the specfied specifications in sSQL3
db.Execute sSQL3, dbFailOnError '(Runs the append query sSQL3)

'##### ADD THESE LINES

'******************************************************************************************
' Check if field [EndTime] in "rsDataViaCode" is NULL
' If NOT NULL, update the field [EndTime] in "rsMASTER" with the time in
[EndTime]
' from the current record in "rsDataViaCode" where the PK = rsMASTER_PK
'
' Date/Time type fields must be delimited with "#"
'******************************************************************************************

If not IsNull(rsDataViaCode.Fields("EndTime") Then

' We're going to reuse sSQL3
sSQL3 = "UPDATE rsMASTER"
sSQL3 = sSQL3 & " SET [EndTime] = #" &
rsDataViaCode.Fields("EndTime") & "#"
sSQL3 = sSQL3 & " WHERE [ID] = " & rsMASTER_PK & ";"

'Executes the specfied specifications in sSQL3
db.Execute sSQL3, dbFailOnError

End If

'#####

' MoveNext - Moves to the next record in a specified Recordset
' object and make that record the current record
rsDataViaCode.MoveNext

' Check if at EOF (End Of File) of Recordset (rsDataViaCode)
' if at EOF (End Of File) Exit Do.
If rsDataViaCode.EOF Then
Exit Do ' Exit loop
End If
Loop ' inner

' if at EOF (End Of File) Exit Do.
If rsDataViaCode.EOF Then
Exit Do ' Exit loop
End If

<snip>

HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Russ via AccessMonster.com said:
Steve,
Having a little problem;
I need to capture the end time in the master field but some have nulls and
creating big problem for me.
This is because the end time is not used until the process is completed.
Example;
Emp Department StartDate StartTime Proc EndTime
11218 112182 05-28-2008 12:19:23 A
11218 112182 05-28-2008 12:19:23 A
11218 112182 05-28-2008 12:19:23 A 12:19:56
11218 112182 05-28-2008 12:20:00 B
11218 112182 05-28-2008 12:20:00 B
11218 112182 05-28-2008 12:20:00 B 12:20:21
11218 112182 05-28-2008 12:20:26 C
11218 112182 05-28-2008 12:20:26 C
Any help would be great.
Steve,
Great job, the code seems to working like I need it, I have further commented
the code trying to understand and document all the logic behind it. As I said
I am still new, so trying to get an understanding to how it all works.
Hopefully I am correct on the process and comments. One question I have is on
the inner and outer loops, can you explain the difference and the use of them?
Sure do appreciate all your help!
Russ

'******************************************************************************************
' For this codde to work we need a reference set to Microsoft DAO 3.6 Object
Library
'
' Step 1 We will need to set-up some recordsets. i.e. "rsDataViaCode" &
rsMASTER
' Create / Open First recordset "rsDataViaCode"
' Create / Open Second recordset "rsMASTER"
' Create / Open Third recordset "rsDataViaCode"
' What is a Recordset?
' ' A recordset represents the entire set of records from a base table or
the results
' ' of an executed command. At any time, the Recordset object refers to
only a single
' ' record within the set as the current record.
'
' Step 2 We need to add / insert new record into the "tblMaster" table
' To Accomplish this we will use the AddNew method.
' What does the AddNew method do?
' ' It tells Microsoft Access database engine to prepare a new blank
record.
' ' The new record is then made the current record
'
' Step 3 We will need to set a bookmark.
' What is a bookmark?
' ' A Bookmark sets or returns a bookmark that uniquely identifies
' ' the current record in a Recordset object.
'
' Step 4 We will need to insert records into thet "tblChild" Table.
' How do we use the insert?
' ' There are essentially two methods for adding records to a table. The
first
' ' is to add one record at a time; the second is to add many records at a
time.
' ' In both cases, you use the SQL statement INSERT INTO to accomplish the
task.
' ' INSERT INTO statements are commonly referred to as append queries.
' ' To add one record to a table, you must use the field list to define
which
' ' fields to put the data in, and then you must supply the data itself in
a
' ' value list. To define the value list, use the VALUES clause.
'
' Step 5 Ok, were done. Time to clean up, we need to close the recordsets.
' To Accomplish this we will use the Close method & Nothing Keyword.
' ' The Close method is used to Closes an open Recordset
' ' The Nothing keyword is used to disassociate an object
' ' variable from an actual object.
'******************************************************************************************

'Declares variables and allocates storage space
Dim db As DAO.Database
Dim rsDataViaCode As DAO.Recordset
Dim rsMASTER As DAO.Recordset

Dim sSQL1 As String
Dim sSQL2 As String
Dim sSQL3 As String

Dim vEmp As String ' Can be text or number.
Dim vDepart As String ' Can be text or number.
Dim vStartDate As Date ' Can only be a date.
Dim vStartTime As Date ' Can only be a date.
Dim vProc As String ' Can be text or number.
Dim rsMASTER_PK As Long ' Can only be a number.

' Set db (DAO.Database) using the CurrentDb method which returns an
' object variable of type Database that represents the database
' currently open in the Microsoft Access window.
Set db = CurrentDb

'******************************************************************************************
' Open the DataViaCode table recordset.
' Step 1) Set-up our query (sSQL1)
' Step 2) Set Recprdset "rsDataViaCode"
'******************************************************************************************

' Set-up the sSQL1 Select query.
' Here we will select all fields / records in the table.
sSQL1 = "SELECT * FROM DataViaCode"

' Set the Recordset rsDataViaCode using db.OpenRecordset which creates a
' new Recordset object and appends it to the Recordsets collection.
Set rsDataViaCode = db.OpenRecordset(sSQL1)

'******************************************************************************************
' Open the MASTER table recordset.
' Step 1) Set-up our query (sSQL2)
' Step 2) Set Recprdset "rsMASTER"
'******************************************************************************************

' Set-up the sSQL2 Select query.
' sSQL2 statement includes parameters for the query.
' Here we will select fields we want to update from the "tblMASTER" table.
sSQL2 = "SELECT ID, Emp, Department, StartDate, StartTime, Proc"
sSQL2 = sSQL2 & " FROM tblMASTER"

' Set Recordset rsMASTER using db.OpenRecordset which creates a new
' Recordset object and appends it to the Recordsets collection.
Set rsMASTER = db.OpenRecordset(sSQL2)

'******************************************************************************************
' Check for records in rsDataViaCode.
' We will use the BOF and EOF properties to determine whether a
' Recordset object contains records or whether you've gone beyond the
' limits of a Recordset object when you move from record to record.
'******************************************************************************************

' If rsDataViaCode is not at the beginning of the file or is not at
' the end of the file then continue and move to first record.
If Not rsDataViaCode.BOF And Not rsDataViaCode.EOF Then

' Here we move to the first record in our Recordset object
' and make that record the current record.
rsDataViaCode.MoveFirst

' outer loop - for tblMASTER table records
Do
vEmp = rsDataViaCode.Fields("EMP")
vDepart = rsDataViaCode.Fields("Department")
vStartDate = rsDataViaCode.Fields("StartDate")
vStartTime = rsDataViaCode.Fields("StartTime")
vProc = rsDataViaCode.Fields("Proc")

'******************************************************************************************
' Insert a new record into the ONE (tblMASTER) table.
' We will accomplish this by doing the following steps;
' 1) Use the AddNew method to create a record you can edit.
' 2) Assign values to each of the record's fields.
' 3) Use the Update method to save the new record.
'******************************************************************************************

' The With statement allows you to perform a series of statements on
a specified
' object without requalifying the name of the object. Allows us to
refer to the
' object once instead of referring to it with each property
assignment.
With rsMASTER ' Here we are referring to the object just once.

' Step 1) Use the AddNew method
' Database engine prepares a new blank record & makes it the
current record.
.AddNew 'since we used the With statement we do not have to refer
to "rsMaster"

' Step 2) Assign values to each of the record's fields.
.Fields("emp") = rsDataViaCode.Fields("EMP")
.Fields("Department") = rsDataViaCode.Fields("Department")
.Fields("StartDate") = rsDataViaCode.Fields("StartDate")
.Fields("StartTime") = rsDataViaCode.Fields("StartTime")
.Fields("Proc") = rsDataViaCode.Fields("Proc")

' Step 3) Use the Update method to save the new record.
.Update 'saves new record

' Step 4) Find the last record added.
' We will use this bookmark to move the current record pointer
' to the most recently changed or added record.
.Bookmark = .LastModified

' get the PK (Primary Key) for use in the child table
rsMASTER_PK = .Fields("ID")

End With

' inner loop for child table records
' the previous value must equal the current values
' to add related records to the child table
Do While vEmp = rsDataViaCode.Fields("EMP") And _
vDepart = rsDataViaCode.Fields("Department") And _
vStartDate = rsDataViaCode.Fields("StartDate") And _
vStartTime = rsDataViaCode.Fields("StartTime") And _
vProc = rsDataViaCode.Fields("Proc")

'******************************************************************************************
' Insert / Append a new record into the "tblChild" (MANY) table
' Use INSERT INTO This Adds a record or multiple records to a table.
This is referred
' to as an append query. Single-record append query:

'INSERT INTO target [(field1[, field2[, …]])] VALUES (value1[, value2[, …])

' TARGET
' The name of the table or query to append records to.

' VALUES
' The values to insert into the specific fields of the new record.
' Each value is inserted into the field that corresponds to the value's
' position in the list: value1 is inserted into field1 of the new record,
' value2 into field2, and so on. You must separate values with a comma, and
' enclose text fields in quotation marks (' ').
'******************************************************************************************

sSQL3 = "INSERT INTO tblChild (Proc, ItemScan, Qty, LinkFK)"
sSQL3 = sSQL3 & " VALUES ('" & rsDataViaCode.Fields("Proc") & "',
'" & rsDataViaCode.Fields("ItemScan")
sSQL3 = sSQL3 & "', " & rsDataViaCode.Fields("Qty") & ", " &
rsMASTER_PK & ") ;"

'Executes the specfied specifications in sSQL3
db.Execute sSQL3, dbFailOnError '(Runs the append query sSQL3)

' MoveNext - Moves to the next record in a specified Recordset
' object and make that record the current record
rsDataViaCode.MoveNext

' Check if at EOF (End Of File) of Recordset (rsDataViaCode)
' if at EOF (End Of File) Exit Do.
If rsDataViaCode.EOF Then
Exit Do ' Exit loop
End If
Loop ' inner

' if at EOF (End Of File) Exit Do.
If rsDataViaCode.EOF Then
Exit Do ' Exit loop
End If

Loop ' outer

End If

'******************************************************************************************
' Clean up, time to close the recordsets
' The Close Method is used to Closes an open Recordset
'******************************************************************************************
rsDataViaCode.Close
rsMASTER.Close
' The Nothing keyword is used to disassociate an object
' variable from an actual object.
Set rsDataViaCode = Nothing
Set rsMASTER = Nothing
Set db = Nothing

End Sub
I guess I didn't do so well explaining the code...... :(
[quoted text clipped - 70 lines]
Part one the main record - [emp], [department],[date],[start time]
Part two details the record – [item scan], [qty]
 
R

Russ via AccessMonster.com

Again, thanks for the help.
I inserted the additional lines, but when compile I get error compile error
do without loop
Do I need to move something?

' inner loop for child table records
' the previous value must equal the current values
' to add related records to the child table
Do While vEmp = rsDataViaCode.Fields("EMP") And _
vDepart = rsDataViaCode.Fields("Department") And _
vStartDate = rsDataViaCode.Fields("StartDate") And _
vStartTime = rsDataViaCode.Fields("StartTime") And _
vProc = rsDataViaCode.Fields("Proc")

'******************************************************************************************
' Insert / Append a new record into the "tblChild" (MANY) table
' Use INSERT INTO This Adds a record or multiple records to a table.
This is referred
' to as an append query. Single-record append query:

'INSERT INTO target [(field1[, field2[, …]])] VALUES (value1[, value2[, …])

' TARGET
' The name of the table or query to append records to.

' VALUES
' The values to insert into the specific fields of the new record.
' Each value is inserted into the field that corresponds to the value's
' position in the list: value1 is inserted into field1 of the new record,
' value2 into field2, and so on. You must separate values with a comma, and
' enclose text fields in quotation marks (' ').
'******************************************************************************************

sSQL3 = "INSERT INTO tblChild (Proc, ItemScan, Qty, LinkFK)"
sSQL3 = sSQL3 & " VALUES ('" & rsDataViaCode.Fields("Proc") & "',
'" & rsDataViaCode.Fields("ItemScan")
sSQL3 = sSQL3 & "', " & rsDataViaCode.Fields("Qty") & ", " &
rsMASTER_PK & ") ;"

'Executes the specfied specifications in sSQL3
db.Execute sSQL3, dbFailOnError '(Runs the append query sSQL3)


'##### ADD THESE LINES

'******************************************************************************************
' Check if field [EndTime] in "rsDataViaCode" is NULL
' If NOT NULL, update the field [EndTime] in "rsMASTER" with the time in
[EndTime]
' from the current record in "rsDataViaCode" where the PK = rsMASTER_PK
'
' Date/Time type fields must be delimited with "#"
'******************************************************************************************

If Not IsNull(rsDataViaCode.Fields("EndTime")) Then

' We're going to reuse sSQL3
sSQL3 = "UPDATE rsMASTER"
sSQL3 = sSQL3 & " SET [EndTime] = #" & rsDataViaCode.Fields
("EndTime") & "#"
sSQL3 = sSQL3 & " WHERE [ID] = " & rsMASTER_PK & ";"

'Executes the specfied specifications in sSQL3
db.Execute sSQL3, dbFailOnError

End If

'#####

' MoveNext - Moves to the next record in a specified Recordset
' object and make that record the current record
rsDataViaCode.MoveNext

' Check if at EOF (End Of File) of Recordset (rsDataViaCode)
' if at EOF (End Of File) Exit Do.
If rsDataViaCode.EOF Then
Exit Do ' Exit loop
End If
Loop ' inner

' if at EOF (End Of File) Exit Do.
If rsDataViaCode.EOF Then
Exit Do ' Exit loop
End If


'******************************************************************************************
' Clean up, time to close the recordsets
' The Close Method is used to Closes an open Recordset
'******************************************************************************************
rsDataViaCode.Close
rsMASTER.Close
' The Nothing keyword is used to disassociate an object
' variable from an actual object.
Set rsDataViaCode = Nothing
Set rsMASTER = Nothing
Set db = Nothing

End Sub
 
R

Russ via AccessMonster.com

Steve,
Updated the code again, must of had a space or something in the wrong place
because now I have it working.
Thanks to you that is...:)
You are correct this recordset stuff is powerful.
Can not wait to learn more about it and it's many uses.
Again, thanks for the help.
I inserted the additional lines, but when compile I get error compile error
do without loop
Do I need to move something?

' inner loop for child table records
' the previous value must equal the current values
' to add related records to the child table
Do While vEmp = rsDataViaCode.Fields("EMP") And _
vDepart = rsDataViaCode.Fields("Department") And _
vStartDate = rsDataViaCode.Fields("StartDate") And _
vStartTime = rsDataViaCode.Fields("StartTime") And _
vProc = rsDataViaCode.Fields("Proc")

'******************************************************************************************
' Insert / Append a new record into the "tblChild" (MANY) table
' Use INSERT INTO This Adds a record or multiple records to a table.
This is referred
' to as an append query. Single-record append query:

'INSERT INTO target [(field1[, field2[, …]])] VALUES (value1[, value2[, …])

' TARGET
' The name of the table or query to append records to.

' VALUES
' The values to insert into the specific fields of the new record.
' Each value is inserted into the field that corresponds to the value's
' position in the list: value1 is inserted into field1 of the new record,
' value2 into field2, and so on. You must separate values with a comma, and
' enclose text fields in quotation marks (' ').
'******************************************************************************************

sSQL3 = "INSERT INTO tblChild (Proc, ItemScan, Qty, LinkFK)"
sSQL3 = sSQL3 & " VALUES ('" & rsDataViaCode.Fields("Proc") & "',
'" & rsDataViaCode.Fields("ItemScan")
sSQL3 = sSQL3 & "', " & rsDataViaCode.Fields("Qty") & ", " &
rsMASTER_PK & ") ;"

'Executes the specfied specifications in sSQL3
db.Execute sSQL3, dbFailOnError '(Runs the append query sSQL3)

'##### ADD THESE LINES

'******************************************************************************************
' Check if field [EndTime] in "rsDataViaCode" is NULL
' If NOT NULL, update the field [EndTime] in "rsMASTER" with the time in
[EndTime]
' from the current record in "rsDataViaCode" where the PK = rsMASTER_PK
'
' Date/Time type fields must be delimited with "#"
'******************************************************************************************

If Not IsNull(rsDataViaCode.Fields("EndTime")) Then

' We're going to reuse sSQL3
sSQL3 = "UPDATE rsMASTER"
sSQL3 = sSQL3 & " SET [EndTime] = #" & rsDataViaCode.Fields
("EndTime") & "#"
sSQL3 = sSQL3 & " WHERE [ID] = " & rsMASTER_PK & ";"

'Executes the specfied specifications in sSQL3
db.Execute sSQL3, dbFailOnError

End If

'#####

' MoveNext - Moves to the next record in a specified Recordset
' object and make that record the current record
rsDataViaCode.MoveNext

' Check if at EOF (End Of File) of Recordset (rsDataViaCode)
' if at EOF (End Of File) Exit Do.
If rsDataViaCode.EOF Then
Exit Do ' Exit loop
End If
Loop ' inner

' if at EOF (End Of File) Exit Do.
If rsDataViaCode.EOF Then
Exit Do ' Exit loop
End If

'******************************************************************************************
' Clean up, time to close the recordsets
' The Close Method is used to Closes an open Recordset
'******************************************************************************************
rsDataViaCode.Close
rsMASTER.Close
' The Nothing keyword is used to disassociate an object
' variable from an actual object.
Set rsDataViaCode = Nothing
Set rsMASTER = Nothing
Set db = Nothing

End Sub
 

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