473,778 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Moving Records from one table to another that meet a date between critiria

84 New Member
ok i think this question should go here seeming its more based around SQl code rather than the vb code.

Heres my problem i have a program in vb6 with a acees backend, the table "Invoice" in the Db has about 38500 records 9 and will go up)which seems to be over the limit that a vb data grid can display. As when i view the data grid it starts out invoice_Id 84 rather than 1, but that is now fixed as i just did an Order BY ASC so thats ok

The problem is i want to archive all the invoices with the "Invoice_Da te" less than or greater than two dates a user inputs eg 01/01/1997 -> 01/01/2007 in dd/mm/yyyy format

i have tried many different ways of doing this and one that works is my using a loop and searching each record if its invoice_date matches the criteria.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim InvNum As Long
  3. Dim InvRec As Long
  4. ArchProgressBar.Min = 0 
  5. ArchProgressBar.Max = InvoiceRS.RecordCount
  6.  
  7. InvDateFrom = Format(Txt_ArchiveDateFrom.Text, "dd/mm/yyyy")
  8. InvDateTill = Format(Txt_ArchiveDateTill.Text, "dd/mm/yyyy")
  9.  
  10. InvoiceSQL = "SELECT * FROM Invoice WHERE Invoice_Date >= 01/01/1996 ORDER BY Invoice_ID ASC"
  11. OpenInvoiceDB (InvoiceSQL)
  12. InvoiceArchSQL = "SELECT * FROM Invoice_Archive"
  13. OpenInvoiceArchDB (InvoiceArchSQL)
  14. InvoiceRS.MoveFirst
  15. InvNum = InvoiceRS.RecordCount
  16.  
  17.  
  18.  
  19. Do Until InvRec = InvNum
  20. For InvRec = 0 To InvNum
  21.  
  22. If (InvoiceRS("Invoice_Date") >= DateValue(InvDateFrom)) And (InvoiceRS("Invoice_Date") <= DateValue(InvDateTill)) Then
  23. InvoiceArchRS.AddNew
  24. InvoiceArchRS("Invoice_ID") = InvoiceRS("Invoice_ID")
  25. InvoiceArchRS("Invoice_Date") = InvoiceRS("Invoice_Date")
  26. InvoiceArchRS("Customer_ID") = InvoiceRS("Customer_ID")
  27. InvoiceArchRS("Customer_Name") = InvoiceRS("Customer_Name")
  28. InvoiceArchRS.Update
  29.  
  30.  
  31. ArchProgressBar.Value = ArchProgressBar.Value + 1
  32.  
  33. Arch_lblPercent.Caption = "Archiving " & Int(ArchProgressBar.Value * 100 / ArchProgressBar.Max) & "% Done"
  34. Arch_lblPercent.Refresh
  35. Lbl_ArchBarCurrentRec.Caption = "Warning! This will take some time, current record:" & ArchProgressBar.Value
  36. InvoiceRS.MoveNext
  37.  
  38. Next InvRec
  39. End If
  40. Loop
  41.  
this is very long-winded and im sure can be done must faster with a SQl statement so this is what i came up with


Expand|Select|Wrap|Line Numbers
  1.  
  2. InvoiceSQL = "SELECT * FROM Invoice WHERE (Invoice_Date >= " & DateValue(InvDateFrom) & ") AND (Invoice_Date <= " & DateValue(InvDateTill) & ") ORDER BY Invoice_ID ASC"
  3. OpenInvoiceDB (InvoiceSQL)
  4.  
  5.  
  6. 'OR 
  7.  
  8. InvoiceSQL = "SELECT * FROM Invoice WHERE (Invoice_date BETWEEN " & DateValue(InvDateFrom) & " AND " & DateValue(InvDateTill) & ") ORDER BY Invoice_ID ASC"
  9. OpenInvoiceDB (InvoiceSQL) 
  10.  
These both produce a recordcount of 0

but there is deffinatly records that meet that critria.

I also found a way so use INSERT INTO

such as
Expand|Select|Wrap|Line Numbers
  1.  
  2. InvoiceSQL = "Insert Into Invoice_Archive " _
  3. & " Select * From Invoice " _
  4. & " Where Invoice_Date BETWEEN " & InvDateFrom & " AND " & InvDateTill & ""
  5. OpenInvoiceDB (InvoiceSQL)
  6.  
But this time the code runs but doesnt actually move anything :S

So can someone give me a few pointers or correct my code plz :P
Thx in advance.
Mar 11 '08 #1
16 2061
amitpatel66
2,367 Recognized Expert Top Contributor
Did you give a commit after running INSERT statement?
Mar 11 '08 #2
ck9663
2,878 Recognized Expert Specialist
I think this is an Access question, not an SQL-Server. But t-sql are almost identical so I'll try to give my two cents.

You have this code:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  InvoiceSQL = "Insert Into Invoice_Archive " _
  3.       & " Select * From Invoice " _
  4.       & " Where Invoice_Date BETWEEN " & InvDateFrom &
  5.                 " AND " & InvDateTill & ""      
  6. OpenInvoiceDB (InvoiceSQL)
Find a way display the value of InvoiceSQL variable, like MessageBox maybe. Paste it in your query builder (i don't know how it's called in Access).

Try executing the SELECT first. Remove or comment-out the insert part first. If there are rows returned by your SELECT, then include the insert. If there are no rows returned by your select, then check your data, the WHERE part is not being fulfilled.

-- CK
Mar 11 '08 #3
metalheadstorm
84 New Member
Did you give a commit after running INSERT statement?
sorry i dont know what you meen :S


Heres an example without the INSERT

Expand|Select|Wrap|Line Numbers
  1.  
  2. InvoiceSQL = "SELECT * FROM Invoice WHERE Invoice_Date BETWEEN " & InvDateFrom & " AND " & InvDateTill & ""
  3. OpenInvoiceDB (InvoiceSQL)
  4. testreccount = InvoiceRS.RecordCount
  5. MsgBox ("Count " & testreccount & "")
  6.  
This also produces no records

Heres a example of some data

Invoice_Id Invoice_Date Customer_ID Customer_Name
1 24/10/2001 OLEADE01 MR DENIS O LEARY

InvDateFrom = 01/01/1997
InvDateTill = 01/01/2007

TF 24/10/2001 is between InvDateFrom and InvDateTill
Mar 12 '08 #4
amitpatel66
2,367 Recognized Expert Top Contributor
sorry i dont know what you meen :S


Heres an example without the INSERT

Expand|Select|Wrap|Line Numbers
  1.  
  2. InvoiceSQL = "SELECT * FROM Invoice WHERE Invoice_Date BETWEEN " & InvDateFrom & " AND " & InvDateTill & ""
  3. OpenInvoiceDB (InvoiceSQL)
  4. testreccount = InvoiceRS.RecordCount
  5. MsgBox ("Count " & testreccount & "")
  6.  
This also produces no records

Heres a example of some data

Invoice_Id Invoice_Date Customer_ID Customer_Name
1 24/10/2001 OLEADE01 MR DENIS O LEARY

InvDateFrom = 01/01/1997
InvDateTill = 01/01/2007

TF 24/10/2001 is between InvDateFrom and InvDateTill
After performing any INSERT,DELETE,U PDATE on database, COMMIT needs to be done in order to make the changes permanent.

ANyways your SELECT itself does not return any record even you have the data, so it is the problem with the select query. Are all the dates being passed in proper format (DD/MM/YYYY) ?
Mar 12 '08 #5
metalheadstorm
84 New Member
After performing any INSERT,DELETE,U PDATE on database, COMMIT needs to be done in order to make the changes permanent.

ANyways your SELECT itself does not return any record even you have the data, so it is the problem with the select query. Are all the dates being passed in proper format (DD/MM/YYYY) ?
Yes it is in DD/MM/YYYY InvDateFrom/Till is in DD/MM/YYYY

and in access it is in short date format so they both match
Mar 12 '08 #6
amitpatel66
2,367 Recognized Expert Top Contributor
Yes it is in DD/MM/YYYY InvDateFrom/Till is in DD/MM/YYYY

and in access it is in short date format so they both match
Did you try to execute your select query in Access DB instead of from your VB application? Does that give you the records??
Mar 12 '08 #7
metalheadstorm
84 New Member
this is what i have

SELECT *
FROM Invoice
WHERE Invoice_Date BETWEEN 01/01/1997 AND 01/01/2007;

and that doesnt return any either
Mar 12 '08 #8
amitpatel66
2,367 Recognized Expert Top Contributor
this is what i have

SELECT *
FROM Invoice
WHERE Invoice_Date BETWEEN 01/01/1997 AND 01/01/2007;

and that doesnt return any either
You need to enclose your dates with single quotes or something??
Mar 12 '08 #9
ck9663
2,878 Recognized Expert Specialist
this is what i have

SELECT *
FROM Invoice
WHERE Invoice_Date BETWEEN 01/01/1997 AND 01/01/2007;

and that doesnt return any either

Try converting those dates into a date datatype. Something like:


Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM Invoice
  3. WHERE Invoice_Date BETWEEN cast('01/01/1997' as datetime) AND cast('01/01/2007' as datetime)

-- CK
Mar 12 '08 #10

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

Similar topics

0
1801
by: RT | last post by:
Hello; I'm trying to create a updates pages that I can move either to the previous record or the next record within a recordset without having to return to the repeating list of records after each up date. Assuming I have done the recordset correctly, which I have and the update records command as well here is the code I'm trying to use: <?php
2
3120
by: M Wells | last post by:
Hi All, I need to change a column value in several thousand records in a table and output a list of the record ids of the records I've updated into another table. The table, however, is being used by other users via a website, so I need to make certain that website requests don't touch on the records I'm selecting / updating.
6
4083
by: Matt K. | last post by:
Hi there, I have a form in an Access project that contains a subform which displays the results of a query of the style "select * from where = #a certain date#". In the main part of the form the user can change the date, which will force a requery in the subform to bring up records from the date selected. My question is this... The query in the subform is a very simple one, with only three fields being returned. In the interest of...
2
1447
by: No Spam | last post by:
To fellow Access 2K users: I have run into a newbie situation where I have been assigned to create an Access database to act as a schedule. Basically, it is a very simple setup: the end product is an Excel like grid that needs to have: * The periods along the top of the grid (which are weekly periods) * The names of the emploees along the left * The clients in the body of the grid
6
2506
by: Robin S. | last post by:
**Eric and Salad - thank you both for the polite kick in the butt. I hope I've done a better job of explaining myself below. I am trying to produce a form to add products to a table (new products). Tables: tblCategoryDetails CategoryID SpecID
2
2782
by: Jason | last post by:
I have a table of 650,000 records associated to contracts. Some of the contracts have multiple records but each contract only has one Active Record (there might be several inactive records). There are dates associated with each of the records (whether active or inactive). I need to compare the dates between the active and inactive contract records. So far, I've created a "find duplicates query" for contract to identify contracts that...
9
1525
by: Sharktyyfa | last post by:
Hi, hoping someone can help. Access 2003, WinXP. I have built a database that uses synchro to co-ordinate with the mothership. All is well. The person i built it for wants the satellites to be able to edit the
11
3680
by: shriil | last post by:
Hi I have this database that calculates and stores the incentive amount earned by employees of a particular department. Each record is entered by entering the Date, Shift (morn, eve, or night) and the 'employee name'. There is another table which assigns an ID to the Shifts, i.e. 1,2 and 3 for morn, eve & night shifts respectively. From the mother table, the incentive is calculated datewise for each employee as per his shift duty. In...
3
1496
by: c0l0nelFlagg | last post by:
I have a moving dispatcher database. There are 99 drivers, 99 loaders, and 50 different vehicles. The scheduler database is built on a 13 4 week month year so that it can be used repeatedly in any subsequent year and recalculate the dates so that each year it is referenced to the first available business day of any given week. This is done by running an update query that calculates the date based on a formula related to the month week and...
0
9464
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
10292
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10122
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
10061
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
9923
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
6722
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
5368
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...
0
5497
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.