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
13 2029
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.
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. -
Function Import()
-
Dim rs As DAO.Recordset
-
Dim rs1 As DAO.Recordset
-
Dim strSQL As String
-
Dim strSQL1 As String
-
Dim sID As Long
-
Dim I As Long
-
Dim J As Long
-
' Open your import table
-
strSQL = "SELECT * from ImportTable;"
-
Set rs = CurrentDb.OpenRecordset(strSQL)
-
rs.MoveFirst
-
Do Until rs.EOF
-
sID = rs![Order ID]
-
strSQL1 = "SELECT * from TargetTable where [Order ID]=" & sID & ";"
-
' Open your Target Table
-
Set rs1 = CurrentDb.OpenRecordset(strSQL1)
-
If rs1.EOF Then 'Didn't find any matching IDs
-
rs1.Addnew
-
For I = 1 To rs.Fields.count - 1 'skips the first column as it's usually the PK
-
For J = 1 To rs1.Fields.count - 1 'skips the first column as it's usually the PK
-
If rs1.Fields(J).Name = rs.Fields(I).Name Then ' If the field names match..insert the value
-
rs1.Fields(J) = rs.Fields(I)
-
End If
-
Next J
-
Next I
-
rs1.Update
-
Else
-
rs1.Edit
-
For I = 1 To rs.Fields.count - 1 'skips the first column as it's usually the PK
-
For J = J To rs1.Fields.count - 1 'skips the first column as it's usually the PK
-
If rs1.Fields(J).Name = rs.Fields(I).Name Then ' if the field names match...update the value
-
rs1.Fields(J) = rs.Fields(I)
-
End If
-
Next J
-
Next I
-
rs1.Update
-
End If
-
rs1.Close
-
Set rs1 = Nothing
-
rs.MoveNext
-
Loop
-
rs.Close
-
Set rs = Nothing
-
End Function
-
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. -
Function Import()
-
Dim rs As DAO.Recordset
-
Dim rs1 As DAO.Recordset
-
Dim strSQL As String
-
Dim strSQL1 As String
-
Dim sID As Long
-
Dim I As Long
-
Dim J As Long
-
' Open your import table
-
strSQL = "SELECT * from ImportTable;"
-
Set rs = CurrentDb.OpenRecordset(strSQL)
-
rs.MoveFirst
-
Do Until rs.EOF
-
sID = rs![Order ID]
-
strSQL1 = "SELECT * from TargetTable where [Order ID]=" & sID & ";"
-
' Open your Target Table
-
Set rs1 = CurrentDb.OpenRecordset(strSQL1)
-
If rs1.EOF Then 'Didn't find any matching IDs
-
rs1.Addnew
-
For I = 1 To rs.Fields.count - 1 'skips the first column as it's usually the PK
-
For J = 1 To rs1.Fields.count - 1 'skips the first column as it's usually the PK
-
If rs1.Fields(J).Name = rs.Fields(I).Name Then ' If the field names match..insert the value
-
rs1.Fields(J) = rs.Fields(I)
-
End If
-
Next J
-
Next I
-
rs1.Update
-
Else
-
rs1.Edit
-
For I = 1 To rs.Fields.count - 1 'skips the first column as it's usually the PK
-
For J = J To rs1.Fields.count - 1 'skips the first column as it's usually the PK
-
If rs1.Fields(J).Name = rs.Fields(I).Name Then ' if the field names match...update the value
-
rs1.Fields(J) = rs.Fields(I)
-
End If
-
Next J
-
Next I
-
rs1.Update
-
End If
-
rs1.Close
-
Set rs1 = Nothing
-
rs.MoveNext
-
Loop
-
rs.Close
-
Set rs = Nothing
-
End Function
-
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.
Rabbit 12,516
Recognized Expert Moderator MVP
Perhaps a delete query followed by an append query?
Perhaps a delete query followed by an append query?
Good point too Rabbit. Simpler probably.
J
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
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 : -
strSQL1 = "SELECT * from TargetTable where [Order ID]=" & sID & ";"
-
Chances are your order ID field is text..the code assumes numeric.
so that line changes to this -
strSQL1 = "SELECT * from TargetTable where [Order ID]='" & sID & "';"
-
text variables have to be surrounded with ' ' single ticks.
Let us know how it's going.
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
>>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
>>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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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....
|
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"
|
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",...
|
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,...
| |
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)-----------------------------------------------------------
-------------------------------------------------------------------
...
|
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...
|
by: bvdp |
last post by:
Terry Reedy wrote:
<snip>
<snip>
<snip>
|
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__...
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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();...
|
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...
|
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
| |
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...
| | |