Skip to content
Advertisement

How do I write my SQL Code in MS Access SQL?

Table Name: Records

ID Date Time Status BoxType Material Rack EmployeeNr Transaction
1 25-05-2020 13:11:12 70 36757 4 4 188 2
2 25-05-2020 13:12:40 71 31178 2 5 188 2
3 25-05-2020 13:13:31 71 31178 2 5 188 102
4 27-05-2020 13:14:14 71 38444 3 2 181 3
5 27-05-2020 13:14:15 71 38444 3 2 181 3
6 27-05-2020 13:14:41 71 38444 3 2 181 3
7 27-05-2020 13:15:10 71 39123 6 5 188 3
8 27-05-2020 13:15:51 71 38444 3 2 181 103
9 27-05-2020 13:16:51 71 38444 3 2 181 103

The Desired OUTPUT I want is Rows 1, 6 and 7

ID Date Time Status BoxType Material Rack EmployeeNr Transaction
1 25-05-2020 13:11:12 70 36757 4 4 188 2
6 27-05-2020 13:14:41 71 38444 3 2 181 3
7 27-05-2020 13:15:10 71 39123 6 5 188 3

Hello dear developer friends,

I have this table “Records“. There are two types of transaction viz. ‘Forward’ and ‘Reverse’. The Forward transactions are represented by single to two digits of numbers (that is from 1 to 30) and Reverse transactions are represented by three digits (that is from 100 to 130). Now, every unique Forward transaction has a unique reverse transaction. (For eg. Transaction 1 will have 101, transaction 2 will have 102 and transaction 3 will have 103 and so on).

I do not want to see the Reverse transactions and as well as their forward transactions in my report. (For eg. 102 and 2, 103 and 3) But, I have to find the matching forward transactions whose other columns data (Date, Status,BoxType, Material, Rack,EmployeeNr) are same of Reverse transaction. (As seen in the image, record no. 2 and 3) Also, there can be more than one matches of forward transactions for reverse transactions, and I want to skip only those many forward transactions equal to number of Reverse transactions. (*As seen in the table, record no. 4,5 and 6 matches to record no. 8 and 9, so I want to hide two records no. 4 and 5 with 8 and 9)

So, I tried to write a SQL query, but it is not working in MS access. Appreciate your great help! Thank you! Best Regards.

My code

Advertisement

Answer

Stackoverflow is not intended to be a code writing service but since you did make attempt with SQL and I was the one who brought up VBA and I was intrigued by the challenge, here is something to give you a start. First, build a ‘temp’ table – table is permanent but records are temporary – that has same fields as Records table except ID field is not an autonumber, just a number type. Call it RecordsSelected and make sure fields in both tables are in same order in design. Then, build three query objects.

CntRevs:

Query1:

Query2:

Code in a general module. The result is 3 records identified in your desired output are written to temp table.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement