Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Help   Contact   Merch   Join   Order   Logon   Forums   
 
Home > Forums > Access Developers > VBA by Maarten Brusselers
Back to Access Developers    Comments List
Upload Images | @Reply | Bookmark | Link | Email | Next Unseen |
VBA
Maarten Brusselers 
   
3 years ago
I am trying to first define the last date of entry for a product, next determine the balance of that product on that date, after which I want to update the next balance after entering the usage in the form. No problem to get the date but no luck with the balance. I obviously did not get around the updating part yet. Any help would be much appreciated.

Dim B
    Dim LastD
        LastD = DMax("SeedDate", "SeedStockT", "ProductID=" & ProductCombo.Column(0))
        B = DLookup("Balance", "SeedStockT", "ProductID=" & ProductCombo.Column(0) And SeedDate = LastD)
        MsgBox B
Kevin Robertson  @Reply  
          
3 years ago
Give this a go:

B = DLookup("Balance", "SeedStockT", "ProductID=" & ProductCombo & " And SeedDate=#" & LastD & "#")
Maarten Brusselers OP  @Reply  
   
3 years ago
Thanks Kevin, that worked. I got close, trying combinations of ",# and & but never quite hit the mark.
I presume I now need to run SQL to actually do the update.  Can I use the DIM variable B in the SQL statement?
Maarten Brusselers OP  @Reply  
   
3 years ago
I know how I can update the balance through a couple of (invisible) textboxes in my form but that is not very elegant.
I now have the correct table balance in my DIM variable B.  I have the seeds used in my form.  I now need to deduct the seeds used from the table balance and insert the new balance with the correct date and ProductID in the table. I presume SQL is the way to go. I got this but it didn't work, I suspect I can't use a DIM variable in a SQL statement:

Dim B
    Dim NB
    Dim LastD
        LastD = DMax("SeedDate", "SeedStockT", "ProductID=" & ProductCombo.Column(0))
        B = DLookup("Balance", "SeedStockT", "ProductID=" & ProductCombo.Column(0) & " And SeedDate=#" & LastD & "#")
        NB = B - Quantity
        DoCmd.RunSQL "INSERT INTO SeedStockT(SeedDate,ProductID,Balance)" & _
       "VALUES(#" & StageDate & "#,"ProductCombo.Column(0)","NB")";
Adam Schwanz  @Reply  
           
3 years ago
Your can use variables. You're missing your ambersands (&) around the productcombo
Maarten Brusselers OP  @Reply  
   
3 years ago
Got it to work, forgot the & where they were needed
Kevin Robertson  @Reply  
          
3 years ago
You're missing some ampersands (&).

DoCmd.RunSQL "INSERT INTO SeedStockT(SeedDate,ProductID,Balance) " & _
       "VALUES(#" & StageDate & "#," & ProductCombo.Column(0) & "," & NB & ")";
Kevin Robertson  @Reply  
          
3 years ago
Adam beat me to it. LOL
Maarten Brusselers OP  @Reply  
   
3 years ago
Thanks guys. At least I worked it out myself before reading your posts.
Here is the working Sub.  Any comments for improvements?

Private Sub Quantity_AfterUpdate()
    Dim B
    Dim NB
    Dim LastD
        LastD = DMax("SeedDate", "SeedStockT", "ProductID=" & ProductCombo.Column(0))
        B = DLookup("Balance", "SeedStockT", "ProductID=" & ProductCombo.Column(0) & " And SeedDate=#" & LastD & "#")
        NB = B - Quantity
        DoCmd.RunSQL "INSERT INTO SeedStockT(SeedDate,ProductID,SeedsUsed,Balance)" & _
       "VALUES(#" & StageDate & "#," & ProductCombo.Column(0) & "," & Quantity & "," & Val(NB) & ")"
End Sub

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Developers.
 

Next Unseen

 
 
What's This?

 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 11/14/2026 3:30:02 PM. PLT: 1s