473,670 Members | 2,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Import, Append/Update question.

6 New Member
This is my first time posting here, so if I have made any mistakes (i.e. wrong forum area, etc) I am sorry.

I have an Access database that I have to import a text file into and append it to a certain table. I know this part of my question has been answered and I understand how to do it quite well after searching long and hard for an answer to my next part. I can either import the text file directly into the table or into it's own seperate table to combine the two later. My problem however is that, I can't just append the new data to the existing table I have to be able to update any of the records in the existing table if the new data has a record that contains an Order ID already in the existing table in order to avoid duplicates. I cannot just not enter the new data since its record may contain updated information.
Hope all of that made sense. Thanks to everyone in advance.

Thank you,

Matthew
May 10 '07 #1
13 2029
JConsulting
603 Recognized Expert Contributor
This is my first time posting here, so if I have made any mistakes (i.e. wrong forum area, etc) I am sorry.

I have an Access database that I have to import a text file into and append it to a certain table. I know this part of my question has been answered and I understand how to do it quite well after searching long and hard for an answer to my next part. I can either import the text file directly into the table or into it's own seperate table to combine the two later. My problem however is that, I can't just append the new data to the existing table I have to be able to update any of the records in the existing table if the new data has a record that contains an Order ID already in the existing table in order to avoid duplicates. I cannot just not enter the new data since its record may contain updated information.
Hope all of that made sense. Thanks to everyone in advance.

Thank you,

Matthew
guess what...you get to learn to code now! Aren't you excited?

Here's the gist of it.

You have a table full of imported records.
Somewhere in your table you have an Order ID field.

You need to do a lookup using that id for each record you want to process.

If the Order ID is found, then you run an update query

if the Order ID is not found, you do an insert.

I know...DUH. But the method I'll give you in a second actually does that.

You'll need to plug in your table names and hopefully the field names match.
May 10 '07 #2
JConsulting
603 Recognized Expert Contributor
This is my first time posting here, so if I have made any mistakes (i.e. wrong forum area, etc) I am sorry.

I have an Access database that I have to import a text file into and append it to a certain table. I know this part of my question has been answered and I understand how to do it quite well after searching long and hard for an answer to my next part. I can either import the text file directly into the table or into it's own seperate table to combine the two later. My problem however is that, I can't just append the new data to the existing table I have to be able to update any of the records in the existing table if the new data has a record that contains an Order ID already in the existing table in order to avoid duplicates. I cannot just not enter the new data since its record may contain updated information.
Hope all of that made sense. Thanks to everyone in advance.

Thank you,

Matthew

Ready to code? This is commented, so hopefully will make sense.
This code goes either on a button...or in a Module where you can call it from a macro or something.

Expand|Select|Wrap|Line Numbers
  1. Function Import()
  2. Dim rs As DAO.Recordset
  3. Dim rs1 As DAO.Recordset
  4. Dim strSQL As String
  5. Dim strSQL1 As String
  6. Dim sID As Long
  7. Dim I As Long
  8. Dim J As Long
  9. ' Open your import table
  10. strSQL = "SELECT * from ImportTable;"
  11.     Set rs = CurrentDb.OpenRecordset(strSQL)
  12.     rs.MoveFirst
  13.     Do Until rs.EOF
  14.         sID = rs![Order ID]
  15.             strSQL1 = "SELECT * from TargetTable where [Order ID]=" & sID & ";"
  16.         ' Open your Target Table
  17.         Set rs1 = CurrentDb.OpenRecordset(strSQL1)
  18.         If rs1.EOF Then 'Didn't find any matching IDs
  19.             rs1.Addnew
  20.             For I = 1 To rs.Fields.count - 1 'skips the first column as it's usually the PK
  21.                 For J = 1 To rs1.Fields.count - 1 'skips the first column as it's usually the PK
  22.                     If rs1.Fields(J).Name = rs.Fields(I).Name Then ' If the field names match..insert the value
  23.                         rs1.Fields(J) = rs.Fields(I)
  24.                     End If
  25.                 Next J
  26.             Next I
  27.             rs1.Update
  28.         Else
  29.             rs1.Edit
  30.             For I = 1 To rs.Fields.count - 1  'skips the first column as it's usually the PK
  31.                 For J = J To rs1.Fields.count - 1 'skips the first column as it's usually the PK
  32.                     If rs1.Fields(J).Name = rs.Fields(I).Name Then ' if the field names match...update the value
  33.                         rs1.Fields(J) = rs.Fields(I)
  34.                     End If
  35.                 Next J
  36.             Next I
  37.             rs1.Update
  38.         End If
  39.     rs1.Close
  40.     Set rs1 = Nothing
  41.     rs.MoveNext
  42.     Loop
  43.     rs.Close
  44.     Set rs = Nothing
  45. End Function
  46.  
May 10 '07 #3
mpslanker
6 New Member
Ready to code? This is commented, so hopefully will make sense.
This code goes either on a button...or in a Module where you can call it from a macro or something.

Expand|Select|Wrap|Line Numbers
  1. Function Import()
  2. Dim rs As DAO.Recordset
  3. Dim rs1 As DAO.Recordset
  4. Dim strSQL As String
  5. Dim strSQL1 As String
  6. Dim sID As Long
  7. Dim I As Long
  8. Dim J As Long
  9. ' Open your import table
  10. strSQL = "SELECT * from ImportTable;"
  11.     Set rs = CurrentDb.OpenRecordset(strSQL)
  12.     rs.MoveFirst
  13.     Do Until rs.EOF
  14.         sID = rs![Order ID]
  15.             strSQL1 = "SELECT * from TargetTable where [Order ID]=" & sID & ";"
  16.         ' Open your Target Table
  17.         Set rs1 = CurrentDb.OpenRecordset(strSQL1)
  18.         If rs1.EOF Then 'Didn't find any matching IDs
  19.             rs1.Addnew
  20.             For I = 1 To rs.Fields.count - 1 'skips the first column as it's usually the PK
  21.                 For J = 1 To rs1.Fields.count - 1 'skips the first column as it's usually the PK
  22.                     If rs1.Fields(J).Name = rs.Fields(I).Name Then ' If the field names match..insert the value
  23.                         rs1.Fields(J) = rs.Fields(I)
  24.                     End If
  25.                 Next J
  26.             Next I
  27.             rs1.Update
  28.         Else
  29.             rs1.Edit
  30.             For I = 1 To rs.Fields.count - 1  'skips the first column as it's usually the PK
  31.                 For J = J To rs1.Fields.count - 1 'skips the first column as it's usually the PK
  32.                     If rs1.Fields(J).Name = rs.Fields(I).Name Then ' if the field names match...update the value
  33.                         rs1.Fields(J) = rs.Fields(I)
  34.                     End If
  35.                 Next J
  36.             Next I
  37.             rs1.Update
  38.         End If
  39.     rs1.Close
  40.     Set rs1 = Nothing
  41.     rs.MoveNext
  42.     Loop
  43.     rs.Close
  44.     Set rs = Nothing
  45. End Function
  46.  
Wow, that was an incredibly quick response, and so far it makes perfect sense. I know a little bit about using VBA so I should be able to stumble through it, until I figure how out it all fits together. Thank you so much. I really appreciate it. I will get to work on getting that in my database and let you know how it goes. Thanks again.

Sincerely,

Matthew.
May 10 '07 #4
Rabbit
12,516 Recognized Expert Moderator MVP
Perhaps a delete query followed by an append query?
May 10 '07 #5
JConsulting
603 Recognized Expert Contributor
Perhaps a delete query followed by an append query?
Good point too Rabbit. Simpler probably.
J
May 10 '07 #6
mpslanker
6 New Member
Good point too Rabbit. Simpler probably.
J
I may wind up having to go that route, but I wanna try the other way first. (I love learning new things)

I had to go in and make sure when the text file imported pulled the field names in too, and that they had the same name as the table the data is being appended to. After I got all that taken care of and the above function modified with the appropriate table/column names, I now recieve a runtime error. Error #3464: "Data type mis-matched in criteria expresssion" at this line of code -
Set rs1 = CurrentDb.OpenR ecordset(strSQL 1)
I think it has someting to do with the fact that the sID is dimmed as a Long, but the Order_ID field in the database is text in both tables. I tried dimming sID as a string, unfortunately still recieve the same error.

Should I change the field in the database to number instead of text?
Thank you all again for all your help.

Sincerely,
Matthew
May 10 '07 #7
JConsulting
603 Recognized Expert Contributor
I may wind up having to go that route, but I wanna try the other way first. (I love learning new things)

I had to go in and make sure when the text file imported pulled the field names in too, and that they had the same name as the table the data is being appended to. After I got all that taken care of and the above function modified with the appropriate table/column names, I now recieve a runtime error. Error #3464: "Data type mis-matched in criteria expresssion" at this line of code -
Set rs1 = CurrentDb.OpenR ecordset(strSQL 1)
I think it has someting to do with the fact that the sID is dimmed as a Long, but the Order_ID field in the database is text in both tables. I tried dimming sID as a string, unfortunately still recieve the same error.

Should I change the field in the database to number instead of text?
Thank you all again for all your help.

Sincerely,
Matthew
actually the error is one line above where you actually define the strSQL1. In this case :
Expand|Select|Wrap|Line Numbers
  1. strSQL1 = "SELECT * from TargetTable where [Order ID]=" & sID & ";"
  2.  
Chances are your order ID field is text..the code assumes numeric.
so that line changes to this

Expand|Select|Wrap|Line Numbers
  1. strSQL1 = "SELECT * from TargetTable where [Order ID]='" & sID & "';"
  2.  
text variables have to be surrounded with ' ' single ticks.

Let us know how it's going.
J
May 10 '07 #8
JConsulting
603 Recognized Expert Contributor
I may wind up having to go that route, but I wanna try the other way first. (I love learning new things)

I had to go in and make sure when the text file imported pulled the field names in too, and that they had the same name as the table the data is being appended to. After I got all that taken care of and the above function modified with the appropriate table/column names, I now recieve a runtime error. Error #3464: "Data type mis-matched in criteria expresssion" at this line of code -
Set rs1 = CurrentDb.OpenR ecordset(strSQL 1)
I think it has someting to do with the fact that the sID is dimmed as a Long, but the Order_ID field in the database is text in both tables. I tried dimming sID as a string, unfortunately still recieve the same error.

Should I change the field in the database to number instead of text?
Thank you all again for all your help.

Sincerely,
Matthew

>>I think it has someting to do with the fact that the sID is dimmed as a Long, but the Order_ID field in the database is text in both tables. I tried dimming sID as a string, unfortunately still recieve the same error.

YES. Sorry, I missed that in my first parousal.
Change

Dim sID as Long

to

Dim sID as string

should do it.
J
May 10 '07 #9
mpslanker
6 New Member
>>I think it has someting to do with the fact that the sID is dimmed as a Long, but the Order_ID field in the database is text in both tables. I tried dimming sID as a string, unfortunately still recieve the same error.

YES. Sorry, I missed that in my first parousal.
Change

Dim sID as Long

to

Dim sID as string

should do it.
J
So the single tick marks made the difference, and it is coming along a lot faster than I expected it to. Everything is starting to make sense. I did run into a new error that I don't understand. The program breaks at:
If rs1.Fields(J).N ame = rs.Fields(I).Na me Then ' if the field names match...update the value
Throwing another error:
Run-time error: '3265'
Item not found in this collection.
I don't understand where the collection is for rs1 is. When the program is paused and you point to that line of code it will show in the tool tip:
rs1.Field(24).N ame = <Item not found in this collection> while the other part shows rs.Field(1).Nam e = "Order_ID" All of this comes from the ELSE part of the first IF statement.

No clue where to begin on this one.

Thank you.
May 10 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
6267
by: Doug Baroter | last post by:
Hi, One of my clients has the following situation. They use Access DB for data update etc. some business functions while they also want to view the Access data quickly and more efficiently in SQL Server 2000. Huge Access db with over 100 user tables, over 60 MB data. The DTS package that comes with SQL Server 2000 seems pretty "messy" in the sense that it assumes that one needs to do one time import only or accurately it does not...
2
5783
by: JMCN | last post by:
hi i need some advice on whether if it would be better to use an append query or an update query. here is the situation, i have linked another database table to my current database. then i created a query for the linked database but eventually i need to have this query to be constantly updated and append to another table in the current database. i hope i'm not too vague but i have no idea if i should use an update or an append query....
1
3410
by: Aaron | last post by:
Hello fellow programmers, I am trying to run an append/update query from code, a command button on a form initiates the queries. the format i am using is; _____________________________________________________ SELECT "criteria"
5
2729
by: strauss.sean | last post by:
Hi! I am trying to import a query as a table from a MS Access database in a specified drive, path, and filename; my filenames and paths are being stored in a table for easy reconfiguration. Anybody done this before? Please let me know! Thanks, Sean BTW: I have learned that if I wanted to import a table, this is how it's done: DoCmd.TransferDatabase acImport, "microsoft access", "C:\Folder\file.mdb", acTable, "sourcetable",...
10
2555
by: shumaker | last post by:
I don't need a detailed description of a solution(although I wouldn't mind), but I am hoping someone could tell me in general the best path to go about accomplishing a task, since I don't know all the capabilities of what I have available. I can learn the details myself I think. I am trying to set this up to be as simple to use as possible since others will be importing data on a weekly or daily basis. I need to import some text files,...
1
5373
by: socasteel21 via AccessMonster.com | last post by:
I have a spreadsheet that has 3 tabs each of the worksheets is setup exactly like a cooresponding table in Access. I created a button that should import each tab to a new table and then append those records in the new table to the cooresponding table. Here is my code for the button: ----(start of code)----------------------------------------------------------- ------------------------------------------------------------------- ...
11
2439
by: kaisersose1995 | last post by:
Hi, I've got an import procedure working, using a standard import specification to import a .csv file into a temporary table. The problem i'm having is that i have 4 different sets of borrower details on the same line e.g. B1-Title, B1-Initials, B1-Surname, B2-Title, B2-Initials, B2-Surname, etc. all linked to my main borrower table via an unique account number. My 1st append query matches which account numbers are new to the main...
4
1242
by: bvdp | last post by:
Terry Reedy wrote: <snip> <snip> <snip>
8
1560
by: tow | last post by:
I have a python script (part of a django application, if it makes any difference) which is exhibiting the following behaviour: import my_module # succeeds imp.find_module("my_module") # fails, raising ImportError which is completely baffling me. According to sys.path, both should fail; the directory containing my_module is not in sys.path (though the my_module directory itself is). More puzzlingly, printing out my_module.__file__...
0
8469
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8814
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8592
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8661
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5684
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4211
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2800
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1794
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.