CodingVBA query

 

Press Ctrl+Enter to quickly submit your post
Quick Reply  
 
 
  
 From:  Peter (BOUGHTONP)  
 To:  Dave!!      
33156.2 In reply to 33156.1 
Your post is reading as an almost indecipherable block of text, but if I'm interpreting it right then all you need is an SQL query.

If you can provide an ERM of the database (Tools>Relationships in Access) and concisely specify what info you want, I might be able to suggest something.
0/0
 Reply   Quote More 

 From:  Dave!!   
 To:  Peter (BOUGHTONP)     
33156.3 In reply to 33156.2 
By all means, I've attached the relationship below. As you can see, it's not a complicated database by any means. The point of the "Loan Data" table is to both hold data about current loans, and also to act as a general log of information for previous loans.
---

The intelligence of American Politics

"Osama Bin Laden is either alive and well, or alive and not very well, or not alive" - Donald Rumsfeld
0/0
 Reply   Quote More 

 From:  Ally  
 To:  Dave!!      
33156.4 In reply to 33156.1 
Reet. Pete is pretty much right. I haven't fiddled with Access and VBA for about a year now (thank the lord for .NET), but I believe something like the following:

sql code:
SELECT Count([Loan ID]) AS Loans FROM [Loan DATA] WHERE IsNull([Date Loaned Out]) = FALSE AND IsNull([Date of RETURN]) = TRUE GROUP BY [Test ID]


Will give you the number of current loans for each item. Like I said, not used VBA in a while so the IsNull() stuff might be a bit wrong. But if you save that as query you should then be able to join it to [Test] on [Test ID] and do [No of Copies] - [Loans] to get the number left.

You can do it all in one query but sometimes it's easier to split it up in Access or the poor mite gets confused.
0/0
 Reply   Quote More 

 From:  Dave!!   
 To:  Ally     
33156.5 In reply to 33156.4 

Thanks very much for that. I'll have a play with it at work tomorrow to see if I can get it (with any necessary changes) to work.

 

Muchly appreciated!

---

The intelligence of American Politics

"Osama Bin Laden is either alive and well, or alive and not very well, or not alive" - Donald Rumsfeld
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  Dave!!      
33156.6 In reply to 33156.5 
I don't know about nulls either, but if it doesn't work then you can try the dangerous assumption that Access does nulls normally you can try [col name] IS NULL.

Oh, and unless Access does magic things, there's a [Test ID] missing from Ally's select.

In summary...
SQL code:
SELECT [Test ID], Count([Loan ID]) AS Loans
FROM [Loan DATA]
WHERE [Date Loaned Out] IS NOT NULL
AND [Date of RETURN] IS NULL
GROUP BY [Test ID]
0/0
 Reply   Quote More 

 From:  Dave!!   
 To:  Peter (BOUGHTONP)     
33156.7 In reply to 33156.6 

Cheers, that pretty much works for creating a query which lists the number of current loans. The next problem is how to make use of that data. Access won't let me use the "Loans" field from the query in my form because the data for that query comes from one of the tables being referenced in the form. Not a big deal I'd say, but Access disagrees.

 

Gah I hate Access some times >:-(

 

Thanks for the help so far mind you!

---

The intelligence of American Politics

"Osama Bin Laden is either alive and well, or alive and not very well, or not alive" - Donald Rumsfeld
0/0
 Reply   Quote More 

 From:  Dave!!   
 To:  ALL
33156.8 
And Access is really beginning to irritate me know. It also won't allow me to take the results of one query and use them in another if a "Count" is performed in the first query. Even sub-queries aren't allowed. I've been trying to use an "Update" query to dump the new "available" figures back into the Tests table - seeing as Access won't allow me to reference a query and that query's source table in the same form.

Closest I've come is this:

SQL code:
UPDATE Tests INNER JOIN [Loan DATA] ON Tests.[Test ID]=[Loan DATA].[Test ID] 
SET Tests.[Available Copies] = Tests.[No of Copies]-(SELECT Count([Loan DATA].[Loan ID]) AS Loans 
     FROM Tests INNER JOIN [Loan DATA] ON Tests.[Test ID] = [Loan DATA].[Test ID] 
     WHERE ((([Loan DATA].[Loaned TO]) IS NOT NULL) AND (([Loan DATA].[Date of RETURN]) IS NULL)));


The big Select was derived from the first code that BP/Ally provided. I did get the code working in it's own query to produce a list of Test IDs with number of loans for each item. Since then it's been modified slightly in here to try and get Access to accept it, but the fact that there's a Count in a sub-query means that it's just not having it.

For what it's worth, the code which worked for the first bit is:

SQL code:
SELECT [Loan DATA].[Test ID], Count([Loan DATA].[Loan ID]) AS Loans
FROM Tests INNER JOIN [Loan DATA] ON Tests.[Test ID] = [Loan DATA].[Test ID]
WHERE ((([Loan DATA].[Loaned TO]) IS NOT NULL) AND (([Loan DATA].[Date of RETURN]) IS NULL))
GROUP BY [Loan DATA].[Test ID];


Anyone got any ideas?

<goes looking for more SQL sites and info>
---

The intelligence of American Politics

"Osama Bin Laden is either alive and well, or alive and not very well, or not alive" - Donald Rumsfeld
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  Dave!!      
33156.9 In reply to 33156.8 
Don't know if you can do inner joins on update. :?

Try this:
SQL code:
UPDATE Tests
 
FROM [Loan DATA] 
 
SET [Available Copies] = Tests.[No of Copies]-(SELECT Count([Loan DATA].[Loan ID])
 
WHERE Tests.[Test ID] = [Loan DATA].[Test ID]
AND  ((([Loan DATA].[Loaned TO]) IS NOT NULL) AND (([Loan DATA].[Date of RETURN]) IS NULL)))
;
0/0
 Reply   Quote More 

 From:  Dave!!   
 To:  Peter (BOUGHTONP)     
33156.10 In reply to 33156.9 

Access added those so I assume they're allowed!

 

Thanks for the new code. Unfortunately you can't use a "FROM" statement with an "UPDATE". :(

---

The intelligence of American Politics

"Osama Bin Laden is either alive and well, or alive and not very well, or not alive" - Donald Rumsfeld
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  Dave!!      
33156.11 In reply to 33156.10 
quote:
Unfortunately you can't use a "FROM" statement with an "UPDATE"

You can with a proper database. :(


Is it an option exporting it to MySQL/Derby/etc?
0/0
 Reply   Quote More 

 From:  Dave!!   
 To:  Peter (BOUGHTONP)     
33156.12 In reply to 33156.11 

Regrettably not unfortunately. Which is a pity because Access has really been annoying me today due to its extreme shitness. If the worst comes to the worst, I'll just have to update 2 fields every time a loan comes or goes. Hardly an ideal solution though.

 

Might have another go at it tomorrow. Might be possible to find some way of doing it by modifying the table design a little perhaps.

---

The intelligence of American Politics

"Osama Bin Laden is either alive and well, or alive and not very well, or not alive" - Donald Rumsfeld
0/0
 Reply   Quote More 

 From:  Dave!!   
 To:  ALL
33156.13 

Solved!

 

OK it's a bit of a bodge and one fuck of a work-around, but it now works.

 

The final solution involves 4(!!) queries and a temporary table. The first query empties the temporary table, the second query fills it with a list of current loan counts (you can append whilst using "Count" in a sub query, but not update - bizarre!), the third query resets the "Amount available" field in the main "Tests" table to match the number of copies (needed so that returned items were being properly updated) and the fourth query takes the data from the temporary table, calculates the amount available from the total number of tests and the amount out on loan, then stuffs it into the main "Tests" table.

 

Bung in a couple of action events to execute that lot whenever the loan information is updated and the end result is that the form/table correctly updates itself whenever loan information is changed. One fuck of a bodge admittedly, but the part I'm bothered about is that it works. Plus this isn't going to be a heavily used database so overhead won't be a problem. Most of the time it's just going to be viewed and in which case, no queries are executed at all.

 

So. Thanks very much for the suggestions, help, etc - particulaly with the second query which lends most of its code from Ally and BP. And I now fucking hate Access (not that I exactly loved it before mind you)!

---

The intelligence of American Politics

"Osama Bin Laden is either alive and well, or alive and not very well, or not alive" - Donald Rumsfeld
0/0
 Reply   Quote More 

Message 33156.14 was deleted

 From:  Dave!!   
 To:  Mr (M00RL0CK)     
33156.15 In reply to 33156.14 
quote:
It's ace though


I'd have to disagree with that bit. Problem solving is one thing (and I enjoy it!), but having Access repeatedly turning its nose up at what should be solutions isn't exactly what I'd call "ace". :|
---

The intelligence of American Politics

"Osama Bin Laden is either alive and well, or alive and not very well, or not alive" - Donald Rumsfeld
0/0
 Reply   Quote More 

Reply to All    
 

1–15

Rate my interest:

Adjust text size : Smaller 10 Larger

Beehive Forum 1.5.2 |  FAQ |  Docs |  Support |  Donate! ©2002 - 2024 Project Beehive Forum

Forum Stats