Happy new year...hope someone can help with this..
The nub of this problem is to populate 2 tables in a 1-M relationship from
an imported Excel spreadsheet. The problem being that there is nothing
within the data that can uniquely identify a record.
My mdb has 3 tables:
tblImport (ID, CustName, Owner1, Process1, Owner2, Process2)
tblComplaints (PK_Complaint_ID, CustName)
tblComplaintReasons (FK_Complaint_ID, Owner, Process)
Step1: Users complete a UserForm (MS Excel) which is emailed as an
attachment to a central recipient (automated thro' Excel)
Step 2: The attachments are saved in C:\Temp\ and then imported into
tblImport (tblImport contains identical fields / columns to the .xls files
(automated thro' the database). So far so good.
I then use the autonumber (from tblImport) to seed the PK_Complaint_ID.
Having a common value in tblImport and tblComplaints, I can populate the
'one' and 'many' tables.
Finally I delete the data from tblImport.
The process is risky to say the least, relying as it does on the uniqueness
of the autonumber ID field in tblImport (e.g. if anyone were to compact the
mdb all hell would break loose).
This has been a sod to explain & I've simplified it a little bit. I'm sure
this problem has reared its head before & I'd appreciate any advice.
Thanks,
Paul 6 2308
I will attempt to help you, but I need a little more info...
1) You are using the autonumber from the import table to set the unique
number of the Complaints table?
2) If y ou answered YES to 1, then, WHY??
3) You mentioned the Complaintreasons table in the beggining of your
request for help, but never mentioned it afterward.. I think you may
have made a typo somewhere.. This is how I understand your
importation process to work (with a couple assumptions):
a)User inputs information into Excel spreadsheet
b)Spreadsheet is imported into import table
c)Complain table is populated from the import table, using the
autonumbers from the import table as unique identifiers for the
complaints table.....
d) The complaintreasons table has a relationship to the complaint
table, using the unique identifier field as the 1-m connection.
right?
~Red
I will attempt to help you, but I need a little more info...
1) You are using the autonumber from the import table to set the unique
number of the Complaints table?
2) If y ou answered YES to 1, then, WHY??
3) You mentioned the Complaintreasons table in the beggining of your
request for help, but never mentioned it afterward.. I think you may
have made a typo somewhere.. This is how I understand your
importation process to work (with a couple assumptions):
a)User inputs information into Excel spreadsheet
b)Spreadsheet is imported into import table
c)Complain table is populated from the import table, using the
autonumbers from the import table as unique identifiers for the
complaints table.....
d) The complaintreasons table has a relationship to the complaint
table, using the unique identifier field as the 1-m connection.
right?
~Red
Red,
Yep. Imagine column A data goes into the 'one' table and columns B-M are to
go into the 'many' table. For most records users will only complete maybe
columns A-C (hence lots of empty fields, hence I'd prefer a 1-M)
I need to split the data from tblImport into two tables. Once I populate the
'one' table, I need to go back to tblImport and, for each record that now
exists in tblComplaint, populate the many reasons behind it.
Sounds toturous, and I'm thinking of just loading it in one table and hang
the consequences!
Cheers,
Paul
"Red" <do**********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com... I will attempt to help you, but I need a little more info...
1) You are using the autonumber from the import table to set the unique number of the Complaints table?
2) If y ou answered YES to 1, then, WHY??
3) You mentioned the Complaintreasons table in the beggining of your request for help, but never mentioned it afterward.. I think you may have made a typo somewhere.. This is how I understand your importation process to work (with a couple assumptions):
a)User inputs information into Excel spreadsheet b)Spreadsheet is imported into import table c)Complain table is populated from the import table, using the autonumbers from the import table as unique identifiers for the complaints table..... d) The complaintreasons table has a relationship to the complaint table, using the unique identifier field as the 1-m connection.
right? ~Red
I can think of 2 seperate ways to do this..
Both require that you completely forget about using that silly
autonumber from the temp table... 1 requires you forget you even need a
temp table ;)
I will give you the way I think would work best for you... This is the
progmatic way, but it works, and work very good too... You may need to
change the X variable to a starting value of 2 if you have a header
line in your report... you may even need to tweak it a bit for your
needs... But, this is how I would handle it
(Copy and paste below)
'*************************CODE STARTS
HERE************************************
Public Function ImportExcelReports(strFileName As String, Optional
strWorkBookName As String) As Boolean
Dim XLS As Object, Wkb As Object, rs1 As Recordset, rs2 As Recordset,
rs3 As Recordset, X As Integer, Y As Integer, LastNumber As Long
On Error GoTo HandleError
ImportExcelReports = False
'Open Excel, and Open your file
Set XLS = CreateObject("Excel.Application")
XLS.Application.Visible = True
XLS.Application.workbooks.Open strFileName
'Set your wokb pointer to the worksheet you want to grab the data
from
If strWorkBookName <> "" Then
Set Wkb =
XLS.Application.XLS.Application.activeworkbook.wor ksheets(strWorkBookName)
Else
Set Wkb =
XLS.Application.XLS.Application.activeworkbook.wor ksheets(1)
End If
'Get the last used number from your Complaint table'
Set rs1 = CurrentDb.OpenRecordset("SELECT
Max(tblComplaints.PK_Complaint_ID) AS ID FROM tblComplaints;")
'Set up your recordsets for entry
Set rs2 = CurrentDb.OpenRecordset("tblComplaints")
Set rs3 = CurrentDb.OpenRecordset("tblComplaintReasons")
'This is where you add the data
Do While Wkb.cells(X, 1) <> ""
rs1.Requery
If rs1.RecordCount < 1 Then GoTo HandleError
LastNumber = rs1("ID")
'Add your customer to your complaints table
rs1.Edit
rs1("PK_Complaint_ID,") = LastNumber
rs1("CustName") = Wkb.cells(X, 1) ' This adds column 1 to
the name of the customer
rs1.Update
'Now add their complaints.. this assumes that there are no more
complaints after the first empty workbook cell
Y = 2
Do While Wkb.cells(X, Y) <> ""
rs2.Edit
rs2("FK_Complaint_ID") = LastNumber
rs2("Owner") = Wkb.cells(X, Y)
rs2("Process") = Wkb.cells(X, Y + 1)
rs2.Update
Y = Y + 2 ' Add 2 to y, because from your info, it
seems the 'owner' repeats/changes for each Process
Loop
X = X + 1
Loop
XLS.Application.Quit
ImportExcelReports = True
GoTo EndMe
HandleError:
MsgBox "There has been an error, please call your database
administrator!"
ImportExcelReports = False
GoTo EndMe
EndMe:
End Function
'*************************CODE ENDS
HERE************************************
Also, you may need to fix it, as google has screwed up my lines.. I'm
horrible at making 'line continuations' ;)
And, as usual, make SURE YOU USE A BACKUP TO TEST! =P
Red
Having slept on it, looking up and then incrementing a max(Id) value is
miles better - I'll work through thias today.
Thanks for the advice / code.
Paul.
"Red" <do**********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com... I can think of 2 seperate ways to do this..
Both require that you completely forget about using that silly autonumber from the temp table... 1 requires you forget you even need a temp table ;)
I will give you the way I think would work best for you... This is the progmatic way, but it works, and work very good too... You may need to change the X variable to a starting value of 2 if you have a header line in your report... you may even need to tweak it a bit for your needs... But, this is how I would handle it
(Copy and paste below) '*************************CODE STARTS HERE************************************ Public Function ImportExcelReports(strFileName As String, Optional strWorkBookName As String) As Boolean Dim XLS As Object, Wkb As Object, rs1 As Recordset, rs2 As Recordset, rs3 As Recordset, X As Integer, Y As Integer, LastNumber As Long On Error GoTo HandleError ImportExcelReports = False 'Open Excel, and Open your file Set XLS = CreateObject("Excel.Application") XLS.Application.Visible = True XLS.Application.workbooks.Open strFileName
'Set your wokb pointer to the worksheet you want to grab the data from If strWorkBookName <> "" Then Set Wkb = XLS.Application.XLS.Application.activeworkbook.wor ksheets(strWorkBookName) Else Set Wkb = XLS.Application.XLS.Application.activeworkbook.wor ksheets(1) End If
'Get the last used number from your Complaint table' Set rs1 = CurrentDb.OpenRecordset("SELECT Max(tblComplaints.PK_Complaint_ID) AS ID FROM tblComplaints;")
'Set up your recordsets for entry Set rs2 = CurrentDb.OpenRecordset("tblComplaints") Set rs3 = CurrentDb.OpenRecordset("tblComplaintReasons")
'This is where you add the data Do While Wkb.cells(X, 1) <> "" rs1.Requery If rs1.RecordCount < 1 Then GoTo HandleError LastNumber = rs1("ID") 'Add your customer to your complaints table rs1.Edit rs1("PK_Complaint_ID,") = LastNumber rs1("CustName") = Wkb.cells(X, 1) ' This adds column 1 to the name of the customer rs1.Update 'Now add their complaints.. this assumes that there are no more complaints after the first empty workbook cell Y = 2 Do While Wkb.cells(X, Y) <> "" rs2.Edit rs2("FK_Complaint_ID") = LastNumber rs2("Owner") = Wkb.cells(X, Y) rs2("Process") = Wkb.cells(X, Y + 1) rs2.Update Y = Y + 2 ' Add 2 to y, because from your info, it seems the 'owner' repeats/changes for each Process Loop X = X + 1 Loop
XLS.Application.Quit ImportExcelReports = True GoTo EndMe
HandleError: MsgBox "There has been an error, please call your database administrator!" ImportExcelReports = False GoTo EndMe
EndMe:
End Function '*************************CODE ENDS HERE************************************ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Fred |
last post by:
Hi.
How do I import while mapping an excel table to an access table please???
I've searched around and all I can find is a software product or code that
does the same thing as the access...
|
by: OM |
last post by:
How do I import excel data into 2 joined tables ?
The data is fldDate, fldTruck, fldLitres, fldSpeedometer.
fldDate goes in a table tblFuelHeader, and the rest goes in tblFuelDetail,
with a...
|
by: WindAndWaves |
last post by:
Dear Gurus
I am trying to export some data to excel from a form, but I seem to run into
difficulties when the user has filtered for particular data that is from a
dropdown (linking to another...
|
by: Jen |
last post by:
Trying to take one table in access and split it into multiple excel
files(using an excel template); and then email based on email
addresses in Table2; Of course, I would like to do all of this...
|
by: Jim Heavey |
last post by:
Hello, I am using a generic AD Domain Account to create an excel spreadsheet,
but I am getting an "Access Denied" error when I attempt to instatiate the
excel object. The generic account does is...
|
by: sparks |
last post by:
We get more and more data done in excel and then they want it imported
into access.
The data is just stupid....values of 1 to 5 we get a lot of 0's ok
that alright but 1-jan ?
we get colums...
|
by: aamax |
last post by:
I have to read data from an excel spreadsheet. the first column has a variable number of rows that are associated with one value (which is in only one of the rows, and it's variable about which...
|
by: Shootah |
last post by:
Hi,
I have succeeded in adding automated relationships with refference tables after importing an excel file created from a query to an Access database.
However I have the following problem: ...
|
by: }{ |
last post by:
Guys,
I have a database that basically logs service calls. The calls table has
entries for call no among other things. This call number field has a one to
many relationship with another table...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |