473,626 Members | 3,291 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorting Access Tables in VB

12 New Member
Hi again everyone.

Just trying to work out how to sort tables in VB. I have two Access queries I'm usling to append data into one table, I tried sorting each query as it was apended in the SQL but using an orderby didn't work and besides I have an added field that give the source table I want to sort by.

I have tried orderby but I can't seem to get it working.

I want to order the data in the table after they both have been appended by InvNo, Date and then the Source Table, this is just to make it easy to view.

After I've sorted I want to run a query over it that counts where the invoices match- thats easy to do but I already know I'll get and error as I'm not doing anyhting with the query other than counting matches, all I want to do is count and display the result to the screen, as if I was running a normal query in access.
Jun 29 '07 #1
6 6357
Dököll
2,364 Recognized Expert Top Contributor
Hi again everyone.

Just trying to work out how to sort tables in VB. I have two Access queries I'm usling to append data into one table, I tried sorting each query as it was apended in the SQL but using an orderby didn't work and besides I have an added field that give the source table I want to sort by.

I have tried orderby but I can't seem to get it working.

I want to order the data in the table after they both have been appended by InvNo, Date and then the Source Table, this is just to make it easy to view.

After I've sorted I want to run a query over it that counts where the invoices match- thats easy to do but I already know I'll get and error as I'm not doing anyhting with the query other than counting matches, all I want to do is count and display the result to the screen, as if I was running a normal query in access.
Hello Daine!

Try using ORDER BY, life saver. Here is an example:

http://www.adit.co.uk/html/sqlforaccess2.html

You will find other means to do this, especially if you need to be more specific. ORDER BY always did it for me.

Good luck and welcome!
Jun 29 '07 #2
Killer42
8,435 Recognized Expert Expert
The important thing to remember is that you don't sort data when storing it. It is stored in an arbitrary sequence. In other words, you generally won't know, or care, what order it is stored in. You retrieve it in the order you want it. After all, if you had two reports that need the data sorted different ways, would you store two copies of the data?

If you want to count groups, for instance, that's where the aggregate functions come in. The Access forum is probably the best place to ask about them.
Jun 29 '07 #3
Daine
12 New Member
The important thing to remember is that you don't sort data when storing it. It is stored in an arbitrary sequence. In other words, you generally won't know, or care, what order it is stored in. You retrieve it in the order you want it. After all, if you had two reports that need the data sorted different ways, would you store two copies of the data?

If you want to count groups, for instance, that's where the aggregate functions come in. The Access forum is probably the best place to ask about them.
Yeah I realised you can't sort data as it is appending the record as it come to it, the reason I'm asking it here is because I'm coding it from a button, and I have the queries but some will not work as indended in code.

I've used two quieies to append the two tables to one.

Below it I have an order by statement in the query but understandably can't get it to do anything- I have tired appending the tables to the new one, use Orderby to order the table- I used the in program help (usually works well), and looked at the website but it is all done as SQL again I can't do that as I'm not doing anything with the query.

Not sure if this would help:
My program is to compare two programs- our Accounting package and Invoicing System to see if the invoiced values match- I have a table from the Accounting package and one from the invoicing system.

I have appended the values of interest to the table and then want to sort my InvNo, Date and which program its from- I can do this is a series for quiery, which is fine for those literate enought to use Access- but I want it off of a button press for those who could not undertstand the steps.

I have gotten the first three steps done, now I need to order it for viewing and finding the data needed. Then I will do a quiry to find values where both the Invoice number and Value are distinct- then do a count on each Invoice number to see where it is over 1 (ie now apprears in the table twice as duplicate numbers should have been removed in the distinct query unless they were invoiced and charged at different values- which is what I want to find).

Again I have done this though individual queries but the problem is that some of them give runtime errors when copied into code as the query itself does nothing other than allow them to view the data- this will also be a problem at the end when I want to view the final data, but I'll worry about that when I get there, my thought is to dump it to a new table and delete the table when the window is closed, like a normal query would display, can this be done?
Jun 29 '07 #4
Dököll
2,364 Recognized Expert Top Contributor
Hello, Daine!

I reread your note and realized you have been trying to use Order by and it does not work for you. Have you tried typing up the query:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT * From ThisTable
  3.  
  4.    WHERE UserID <= 3
  5.    AND Name Like "Pool*"
  6.    ORDER BY Name, UserID
  7.  
  8. '{you can also GROUP BY}
  9.  
  10.  
This is just from the top of my head. You can then add to VB, under your button. You mentioned also looking for duplicates, UserID <= 3 would look for UserIDs or the field you think will have dupes if more than one.

I would change to UserID > 1 if yu think there may be more. This should work.

Also if you needed to, and you probably already tried this, but you should query to find dupes in Access (Using Duplicates query). I think running a query to find dupes before hand, in Access, then going in the SQL view, copy the code there, can save you some time.

I would also attempt a Crosstab query, then copy the code. Sorting and grouping through design view can be cumbersome in Access.

Here is something, I just ran using the Northwind database information:

Expand|Select|Wrap|Line Numbers
  1.  
  2. TRANSFORM Count(Customers.FirstName) AS CountOfFirstName
  3. SELECT Customers.CustomerID, Customers.CompanyName, Count(Customers.FirstName) AS [Total Of FirstName]
  4. FROM Customers
  5. GROUP BY Customers.CustomerID, Customers.CompanyName
  6. PIVOT Customers.BillingAddress;
  7.  
Give that a whirl then write if additional help is needed...
Jun 30 '07 #5
Daine
12 New Member
[quote=Dököll]Hello, Daine!

I reread your note and realized you have been trying to use Order by and it does not work for you. Have you tried typing up the query:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT * From ThisTable
  3.  
  4.    WHERE UserID <= 3
  5.    AND Name Like "Pool*"
  6.    ORDER BY Name, UserID
  7.  
  8. '{you can also GROUP BY}
  9.  
  10.  
I got the ORDER BY working I just had a silly error- the date field was a text field type instead of date/time meaning the last date would be the 9th of the month, once I changed it to a date it read as it should.


And yes I copy from the SQL part of Access, it where I got a lot of my code from lol.


For the other part, finding the non-unique values:
These are the three Access Queries I have:

SelectUnique
SELECT DISTINCT [Invoicing Errors].InvNo, [Invoicing Errors].GSTFREE FROM [Invoicing Errors];

CountUnique
SELECT First([SelectUnique].InvNo) AS [InvNo Field], Count([SelectUnique].InvNo) AS NumberOfDups FROM [SelectUnique] GROUP BY [SelectUnique].InvNo HAVING (Count([SelectUnique].InvNo)>1);"

DisplayErrors
SELECT CountUnique.[InvNo Field], [Invoicing Errors].Period, [Invoicing Errors].ACCID, [Invoicing Errors].NAME, [Invoicing Errors].GSTFREE, [Invoicing Errors].From INTO DisplayResults FROM CountUnique INNER JOIN [Invoicing Errors] ON CountUnique.[InvNo Field] = [Invoicing Errors].InvNo WHERE (((CountUnique.[InvNo Field])=[Invoicing Errors].[InvNo] And (CountUnique.[InvNo Field])<>'HS000000' And (CountUnique.[InvNo Field])<>'OX000000')) ORDER BY CountUnique.[InvNo Field], [Invoicing Errors].Period, [Invoicing Errors].From;

If I run the last one in Access it works fine as it run all the others VB doesn't do this so I can't get the last step done as VB will not run other queries.
Jul 1 '07 #6
Daine
12 New Member
Hello, Daine!

I reread your note and realized you have been trying to use Order by and it does not work for you. Have you tried typing up the query:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT * From ThisTable
  3.  
  4.    WHERE UserID <= 3
  5.    AND Name Like "Pool*"
  6.    ORDER BY Name, UserID
  7.  
  8. '{you can also GROUP BY}
  9.  
  10.  
This is just from the top of my head. You can then add to VB, under your button. You mentioned also looking for duplicates, UserID <= 3 would look for UserIDs or the field you think will have dupes if more than one.

I would change to UserID > 1 if yu think there may be more. This should work.

Also if you needed to, and you probably already tried this, but you should query to find dupes in Access (Using Duplicates query). I think running a query to find dupes before hand, in Access, then going in the SQL view, copy the code there, can save you some time.

I would also attempt a Crosstab query, then copy the code. Sorting and grouping through design view can be cumbersome in Access.

Here is something, I just ran using the Northwind database information:

Expand|Select|Wrap|Line Numbers
  1.  
  2. TRANSFORM Count(Customers.FirstName) AS CountOfFirstName
  3. SELECT Customers.CustomerID, Customers.CompanyName, Count(Customers.FirstName) AS [Total Of FirstName]
  4. FROM Customers
  5. GROUP BY Customers.CustomerID, Customers.CompanyName
  6. PIVOT Customers.BillingAddress;
  7.  
Give that a whirl then write if additional help is needed...
I got the Order By working it was a silly error- the date field was a text type rather than Date/Time meaning the 9th dates were always last, a type change to Date/Time fixed that.

I always copy from the Access SQL unless it is basic, makes it easier and less chance of errors.

As for the finding the unique invoice/invoice value totals, here are the three queries I used in Access, it would link them together wher the last one was referenced VB won't so I now need help on how to join them up, any help is apprecaited.


SelectUnique
SELECT DISTINCT [Invoicing Errors].InvNo, [Invoicing Errors].GSTFREE
FROM [Invoicing Errors];

CountUnique
SELECT First([SelectUnique].InvNo) AS [InvNo Field], Count([SelectUnique].InvNo) AS NumberOfDups
FROM [SelectUnique]
GROUP BY [SelectUnique].InvNo HAVING (Count([SelectUnique].InvNo)>1);

DisplayErrors
SELECT CountUnique.[InvNo Field], [Invoicing Errors].Period, [Invoicing Errors].ACCID, [Invoicing Errors].NAME, [Invoicing Errors].GSTFREE, [Invoicing Errors].From INTO DisplayResults
FROM CountUnique INNER JOIN [Invoicing Errors] ON CountUnique.[InvNo Field] = [Invoicing Errors].InvNo
WHERE (((CountUnique.[InvNo Field])=[Invoicing Errors].[InvNo] And (CountUnique.[InvNo Field])<>'HS000000' And (CountUnique.[InvNo Field])<>'OX000000') )
ORDER BY CountUnique.[InvNo Field], [Invoicing Errors].Period, [Invoicing Errors].From;
Jul 1 '07 #7

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

Similar topics

10
6004
by: Job Lot | last post by:
Is there any way to programmatically link access tables using vb.net
5
9646
by: Santiago Ordax Solivellas | last post by:
Hi. We have almost all our tables defined on library lib1 and some on lib2. We have alias defined on lib1 to access tables on lib2 so there is no need to qualify library name. Alias for tables on lib2 are defined this way: CREATE ALIAS lib1.table1 FOR lib2.table1; Both table owner and alias owner is the same. Tables reside on a AS/400 server and we are accesing database from a DB2 client on a PC workstation (DB2 is version 7)
3
24021
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked tables in the database where the code resides. If we move the database with the data tables to a new directory, the links are no longer valid. I tried to update the links by changing the Connect property and refreshing: Set td = db.TableDefs(0)...
46
3830
by: Adam Turner via AccessMonster.com | last post by:
If I had a field called "Name" in an Access table "Contact Info", and the field contained VBScript... Function Main(rstFields) Main = rstFields.Item("FirstName").Value End Function 1. How do I pass a recordset into this field? 2. How do I capture the return value?
0
1758
by: | last post by:
I am updating MS access tables with data in an xml document. I create two dataset, one for existing data and one for new data. I fill the first dataset with the records from MS Access, the second with data from the xml Document. Both datasets are populated properly. When the new data contains records that exist in the existing Access table but with changes in individual elements, the matching records in the first dataset are not changed...
1
9769
by: smaczylo | last post by:
Hello, I've recently been asked to work with Microsoft Access, and while I feel quite comfortable with Excel, I'm at a complete loss with databases. If someone could help me with this issue I'm having I'd be most appreciative. The database is already constructed, I'm just wanting to export the data to an excel file. In short, I'm hoping to export two Tables (or queries...not sure which to use - they both seem to have the same data) in...
2
2549
by: drek01 | last post by:
hi i am trying to import access tables into my backend database( current database) which is sql server. i created file browser which browse access database from harddrive and places all the tables into listbox. and upon clicking table name in list box, i want it to import it to my current database( which is sql server). in order i used this code: Private Sub ListTables_AfterUpdate() docmd.TransferDatabase acLink, "Microsoft Access",...
3
5579
by: inepu | last post by:
I have some excel sheets that I would like to manage in access. I've created the access tables and relationships; the excel sheet is only one table and with duplicate data, I'd like to make sure that when I import the sheet, the proper tables are filled correctly. For example, the excel sheet is in the format clientID Name phone City Country In acces, I have a "Cities" table and a "Coutries" table, with the fields (e.g. for cities) ID...
13
4826
by: BrokenMachine | last post by:
Hi there, I'm using Access 2003 and Word 2003 and I'm trying to import data from the word form to multiple access tables in a one to many relationship, but I can't seem to figure it out. I have used the following example to create a word form that transfers data to a single access table http://msdn2.microsoft.com/en-us/library/aa155434(office.10).aspx This seems to work fine. My problem is that I want to record several addresses on...
0
8262
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
8701
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
8637
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
8364
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,...
1
6122
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5571
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
4196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1507
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.