(Noob) Populating Tables from Excel files (1 Viewer)

Rick Stanich

King and Supreme Ruler
Local time
Today, 00:08
Joined
May 13, 2009
Messages
93
I have a db with several tables and now I need to import data from excel csv files. (Thank you jzwp22)
Typically, like in excel, I record macros for the import process and modify for my needs but it seems that Access doesn't have this option.

My 1st goal is to import a spreadsheet (I think I have accomplshed this part and there will be more than one spreadsheet), populate my tables and remove the newly imported spreadsheet table, plus, I have to read the name of the file to extract information to populate tables and tie my data together for traceability.

A typical file name:
4502730 135b 32225-1 090408 M6.csv
Red is the part number
Light Blue is the operation number
Green is the job number
Violet is the serial number
The remainder is not needed from the file name.

Repeat some umteen bazillion times. :rolleyes:

Attached is my db and a sample csv file. The db has an imported csv file as a table (same name as file name above).

Any hints, tips or examples are appreciated. ;)
 

Attachments

  • GM DataBase.zip
    80.3 KB · Views: 92

jzwp22

Access Hobbyist
Local time
Today, 03:08
Joined
Mar 15, 2008
Messages
2,629
I could not unzip your attachment; I kept getting an error.
 

Rick Stanich

King and Supreme Ruler
Local time
Today, 00:08
Joined
May 13, 2009
Messages
93
Unzipped attachments.
Rename "4502730 135b 32225-1 090408 M6.txt" to "4502730 135b 32225-1 090408 M6.csv"
The new db file is copied into the original zip file we used before.

Right now the db is 860kb, a little over the 795kb limit.

I think the issue is NT4.0ld :D
 

Attachments

  • 4502730 135b 32225-1 090408 M6.txt
    98.4 KB · Views: 173
  • db1v4.zip
    113.9 KB · Views: 74

jzwp22

Access Hobbyist
Local time
Today, 03:08
Joined
Mar 15, 2008
Messages
2,629
I was able to unzip now. I'll see what I can come up with over the weekend.
 

Rick Stanich

King and Supreme Ruler
Local time
Today, 00:08
Joined
May 13, 2009
Messages
93
Dont ruin your weekend over this, I can wait for any help. ;)
 

jzwp22

Access Hobbyist
Local time
Today, 03:08
Joined
Mar 15, 2008
Messages
2,629
How do you know what the revision level is of the part? You have part and serial number in the table name but not the revision level. Is it related to the operation number?
 

Rick Stanich

King and Supreme Ruler
Local time
Today, 00:08
Joined
May 13, 2009
Messages
93
I caught that as well on Friday. I ammended my post for the file name as follows:
A typical file name:
4502730 C 135b 32225-1 090408 M6.csv
Red is the part number (4502730)
Orange is the revision level (C)
Light Blue is the operation number (135b)
Green is the job number (32225-1)
Violet is the serial number (090408)
The remainder is not needed from the file name.

The revision level is related to the part number and serial number along with the other relationships already established.
The only odd part is, the revision level can change mid job and still be related to the same job number.
 

jzwp22

Access Hobbyist
Local time
Today, 03:08
Joined
Mar 15, 2008
Messages
2,629
I don't see operation number anywhere in your tables. What do you want to do with it?
 

jzwp22

Access Hobbyist
Local time
Today, 03:08
Joined
Mar 15, 2008
Messages
2,629
Because it might impact the table structure and the code I am writing to move the data into the appropriate tables, it needs to be worked out before I move further with the code. Is the operation number tied to the part rev or the piece? Can their be multiple operations to a piece or part rev?
 

Rick Stanich

King and Supreme Ruler
Local time
Today, 00:08
Joined
May 13, 2009
Messages
93
I did not realise the impact with my last reply.

The operation is tied to the piece.
There are multiple operations to a piece.
The rev can be tied to the piece and operation.
The revision level is already related to the part number and serial number.
 

jzwp22

Access Hobbyist
Local time
Today, 03:08
Joined
Mar 15, 2008
Messages
2,629
The table structure will have to be reworked a little, but it should not be a big issue. It is always best to get the table structure iron-out up front. It saves a lot of headaches later on. I've attached the reworked table structure. Will there be any impact on the bubbles? In other words, are the bubbles in any way tied to the operations?
 

Attachments

  • db1v5.zip
    86.4 KB · Views: 77
Last edited:

Rick Stanich

King and Supreme Ruler
Local time
Today, 00:08
Joined
May 13, 2009
Messages
93
For internal traceability, the bubbles are tied to an operation, in some instances multiple operations (Redundancy checking of critical dimensions). For aerospace traceability it is not required.
For root cause analysis I trace bubbles to operations which can trace bubbles to specific manufacturing tooling and or fixturing.
 

jzwp22

Access Hobbyist
Local time
Today, 03:08
Joined
Mar 15, 2008
Messages
2,629
Base on your last post, I reworked the tables and relationships. Please take a look at the attached database and see if it makes sense with your application.

What I struggled with was the nominal and upper/lower tolerance values associated with the bubbles. The nominal values are your specifications for the part at each certain revision-operation combination, I assume. If the bubbles are operation dependent than the partrev must tie to the operation. The specification tied to each bubble need only be entered/imported 1 time. Then to get the actual CMM data for a piece you would junction directly to the bubble table.
 

Attachments

  • db1v6.zip
    50.9 KB · Views: 81

Rick Stanich

King and Supreme Ruler
Local time
Today, 00:08
Joined
May 13, 2009
Messages
93
Base on your last post, I reworked the tables and relationships. Please take a look at the attached database and see if it makes sense with your application.

What I struggled with was the nominal and upper/lower tolerance values associated with the bubbles. The nominal values are your specifications for the part at each certain revision-operation combination, I assume. If the bubbles are operation dependent than the partrev must tie to the operation. The specification tied to each bubble need only be entered/imported 1 time. Then to get the actual CMM data for a piece you would junction directly to the bubble table.

Good catch! I would have been smacking myself in the back of my head on that one.

I have one question trying to follow your work.
In relationships window, tblBubbles is "fkPartRevOpID" the same as "fkPartRev" and "fkOpID"?
 

jzwp22

Access Hobbyist
Local time
Today, 03:08
Joined
Mar 15, 2008
Messages
2,629
In relationships window, tblBubbles is "fkPartRevOpID" the same as "fkPartRev" and "fkOpID"?

Not quite, since a part can have many revisions and each revision can have many operations, you have first establish that relationship. Then for each combination of part rev + operation you have many bubbles defined.
 

jzwp22

Access Hobbyist
Local time
Today, 03:08
Joined
Mar 15, 2008
Messages
2,629
I had a little free time this week and have made some limited progress. The following code populates all but the last two tables (tblPieceCMMData and tblBubbles). I'll keep working on it as time permits. Let me know if you come up with a solution in the meantime.

Code:
Public Sub transferdata()

'set up connection
Dim cnn1 As ADODB.Connection
Set cnn1 = CurrentProject.Connection


Dim mytbl As AccessObject

'set up variables to hold the parsed values of the table name holding imported data
Dim mypart As String
Dim myrev As String
Dim myjob As String
Dim myop As String
Dim mysn As String

'set up variable that will hold the position number of the spaces in the table names
Dim firstspaceposition As Long
Dim secondspaceposition As Long
Dim thirdspaceposition As Long
Dim fourthspaceposition As Long
Dim fifthspaceposition As Long
Dim holdpartpk As Long
Dim holdjobpk As Long
Dim holdpartrevpk As Long

'loop through the table names to find the imported tables; ignore system tables and tables beginning with tbl

For Each mytbl In CurrentData.AllTables
    If Not Left(mytbl.Name, 4) = "Msys" Then
        If Not Left(mytbl.Name, 3) = "tbl" Then
            'if an imported table is found, parse out the names into their respective variables
                
            firstspaceposition = InStr(1, mytbl.Name, " ")
            secondspaceposition = InStr(firstspaceposition + 1, mytbl.Name, " ")
            thirdspaceposition = InStr(secondspaceposition + 1, mytbl.Name, " ")
            fourthspaceposition = InStr(thirdspaceposition + 1, mytbl.Name, " ")
            fifthspaceposition = InStr(fourthspaceposition + 1, mytbl.Name, " ")
            
            mypart = Mid(mytbl.Name, 1, firstspaceposition)
            myrev = Mid(mytbl.Name, firstspaceposition + 1, secondspaceposition - 1 - firstspaceposition + 1)
            myop = Mid(mytbl.Name, secondspaceposition + 1, thirdspaceposition - 1 - secondspaceposition + 1)
            myjob = Mid(mytbl.Name, thirdspaceposition + 1, fourthspaceposition - 1 - thirdspaceposition + 1)
            mysn = Mid(mytbl.Name, fourthspaceposition + 1, fifthspaceposition - 1 - fourthspaceposition + 1)
            
           'check to see if the job has been previously created if so get pk; if not create new
           If DCount("*", "tblJobs", "txtJobNo='" & myjob & "'") > 0 Then
            holdjobpk = DLookup("pkJobID", "tblJobs", "txtJobNo='" & myjob & "'")
           Else
            Dim myrecset1 As New ADODB.Recordset
            myrecset1.ActiveConnection = cnn1
            myrecset1.Open "tblJobs", , adOpenDynamic, adLockOptimistic
            With myrecset1
                .AddNew
                !txtJobNo = myjob
                holdjobpk = !pkJobID
                .Update
                .Close
            End With
            Set myrecset1 = Nothing
            End If
            
            
            'check to see if the part has been previously created if so get pk; if not create new
           If DCount("*", "tblParts", "txtPartNo='" & mypart & "'") > 0 Then
            holdpartpk = DLookup("pkPartID", "tblParts", "txtPartNo='" & mypart & "'")
           Else
            Dim myrecset2 As New ADODB.Recordset
            myrecset2.ActiveConnection = cnn1
            myrecset2.Open "tblParts", , adOpenDynamic, adLockOptimistic
            With myrecset2
                .AddNew
                !txtPartNo = mypart
                holdpartpk = !pkPartID
                .Update
                .Close
            End With
            Set myrecset2 = Nothing
           End If
              'check to see if the rev for the part has been previously created if so get pk; if not create new
         If DCount("*", "tblPartRev", "fkPartID=" & holdpartpk & " AND txtrev='" & myrev & "'") > 0 Then
            holdpartrevpk = DLookup("pkPartRevID", "tblPartRev", "fkPartID=" & holdpartpk & " AND txtrev='" & myrev & "'")
           Else
            Dim myrecset3 As New ADODB.Recordset
            myrecset3.ActiveConnection = cnn1
            myrecset3.Open "tblPartRev", , adOpenDynamic, adLockOptimistic
            With myrecset3
                .AddNew
                !txtRev = myrev
                !fkPartID = holdpartpk
                holdpartrevpk = !pkPartRevID
                .Update
                .Close
            End With
            Set myrecset3 = Nothing
           End If
           
            'check to see if the operation has been previously created if so get pk; if not create new
         If DCount("*", "tblOperations", "txtOperationNo='" & myop & "'") > 0 Then
            holdoppk = DLookup("pkOpID", "tblOperations", "txtOperationNo='" & myop & "'")
           Else
            Dim myrecset4 As New ADODB.Recordset
            myrecset4.ActiveConnection = cnn1
            myrecset4.Open "tblOperations", , adOpenDynamic, adLockOptimistic
            With myrecset4
                .AddNew
                !txtOperationNo = myop
                holdoppk = !pkOpID
                .Update
                .Close
            End With
            Set myrecset4 = Nothing
           End If
           
        'check to see if the part rev & operation junction record has been previously created if so get pk; if not create new
         If DCount("*", "tblPartRevOps", "fkPartRevID=" & holdpartrevpk & " AND fkOpID=" & holdoppk) > 0 Then
            holdpartrevopspk = DLookup("pkPartRevOpsID", "tblPartRevOps", "fkPartRevID=" & holdpartrevpk & " AND fkOpID=" & holdoppk)
           
           Else
            Dim myrecset5 As New ADODB.Recordset
            myrecset5.ActiveConnection = cnn1
            myrecset5.Open "tblPartRevOps", , adOpenDynamic, adLockOptimistic
            With myrecset5
                .AddNew
                !fkPartrevID = holdpartrevpk
                !fkOpID = holdoppk
                holdpartrevopspk = !pkPartRevOpsID
                .Update
                .Close
            End With
            Set myrecset5 = Nothing
           End If
            
            
           
              'check to see if the job part record has been previously created if so get pk; if not create new
         If DCount("*", "tblJobParts", "fkJobID=" & holdjobpk & " AND fkPartRevID=" & holdpartrevpk) > 0 Then
            holdjobpartpk = DLookup("pkJobPartID", "tblJobParts", "fkJobID=" & holdjobpk & " AND fkPartRevID=" & holdpartrevpk)
           Else
            Dim myrecset7 As New ADODB.Recordset
            myrecset7.ActiveConnection = cnn1
            myrecset7.Open "tblJobParts", , adOpenDynamic, adLockOptimistic
            With myrecset7
                .AddNew
                !fkJobID = holdjobpk
                !fkPartrevID = holdpartrevpk
                holdjobpartpk = !pkJobPartID
                .Update
                .Close
            End With
            Set myrecset7 = Nothing
           End If
           
           
             'check to see if the piece record has been previously created if so get pk; if not create new
         If DCount("*", "tblPieces", "fkJobPartID=" & holdjobpartpk & " AND txtSerialNo='" & mysn & "'") > 0 Then
            holdpiecepk = DLookup("pkPieceID", "tblPieces", "fkJobPartID=" & holdjobpartpk & " AND txtSerialNo='" & mysn & "'")
           Else
            Dim myrecset8 As New ADODB.Recordset
            myrecset8.ActiveConnection = cnn1
            myrecset8.Open "tblPieces", , adOpenDynamic, adLockOptimistic
            With myrecset8
                .AddNew
                !txtSerialNo = mysn
                !fkJobPartID = holdjobpartpk
                holdpiecepk = !pkPieceID
                .Update
                .Close
            End With
            Set myrecset8 = Nothing
           End If
           
           
           
        End If
    End If
'reset the variables for the next table
firstspaceposition = 0
secondspaceposition = 0
thirdspaceposition = 0
fourthspaceposition = 0
fifthspaceposition = 0

mypart = ""
myrev = ""
myjob = ""
myop = ""
mysn = ""
    
Next mytbl
End Sub
 

Rick Stanich

King and Supreme Ruler
Local time
Today, 00:08
Joined
May 13, 2009
Messages
93
This is excellent, there are some key coded areas I was failing to figure out.
I havent read so much since the eleventyth grade. :lol:
 

jzwp22

Access Hobbyist
Local time
Today, 03:08
Joined
Mar 15, 2008
Messages
2,629
Attached is the database with the completed code. I did not have multiple imported data tables to test the code entirely.
 

Attachments

  • db1v6.zip
    49.4 KB · Views: 74

jzwp22

Access Hobbyist
Local time
Today, 03:08
Joined
Mar 15, 2008
Messages
2,629
I noticed in your imported data that you had multiple data points for the same bubble but not all. I was not sure if this is typical and what you do with the extra data points.
 

Users who are viewing this thread

Top Bottom