Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Update Table with New Numbers Sometimes
Monica Jones 
       
9 months ago
At work we have a spreadsheet that isn't updatable from my end, so we linked it to a database to make changes. I've appended the data into a table; we get new data monthly.

I've created a separate table for updates that only contains numbers if they need to be changed. I was thinking I could change my imported numbers if there's a number in the update table. I'm adding sample data below.

I need to keep updates separate incase the spreadsheet is changed, as I don't want to overwrite my updates.

I'm getting stuck at the part on an update query where I want it to only update if there's a number in the update table otherwise leave it alone. For example, in my sample data the only change is actual for Northern Plains, but it would leave the plan number alone.
Monica Jones OP  @Reply  
       
9 months ago

Monica Jones OP  @Reply  
       
9 months ago

Adam Schwanz  @Reply  
           
9 months ago
Can you just use a recordset like this? Or what else does it need to do?

    'ChangeT has the updates
    'ImportT is the original file that is getting changed
    
    Dim rs As Recordset, rs2 As Recordset
    Set rs = CurrentDb.OpenRecordset("ImportT")
        
    'Look through The Import
    While Not rs.EOF
        Set rs2 = CurrentDb.OpenRecordset("Select * From ChangeT Where importdivision='" & rs!importDivision & "' and importmonth='" & rs!importMonth & "'")
        'If Match Then Update to ChangeT Record Stuff
        If Not rs2.EOF Then
            rs.Edit
            rs!ImportExpenseActual = rs2!ImportExpenseActual
            rs!ImportExpensePlan = rs2!ImportExpensePlan
            rs.Update
        End If
    rs.MoveNext
    Wend
    
    rs2.Close
    rs.Close
    Set rs2 = Nothing
    Set rs = Nothing

Raymond Spornhauer  @Reply  
          
9 months ago
Why keep the spreadsheet?

Why not convert the spreadsheet fully over to Access and just use Access?

-Raymond
Monica Jones OP  @Reply  
       
9 months ago
Raymond I wish, but that's outta my hands.
Monica Jones OP  @Reply  
       
9 months ago
Adam I'm not very familiar with RecordSets just yet and was gonna try an update query for all 47 columns (in the first of SIX reports). So this looks WAY better! My only concern is that it looks like it would overwrite everything where Division and month match. My ChangeT is blank if no change is needed, I wouldn't want to erase good numbers. Thank you for your help!
Sami Shamma  @Reply  
             
9 months ago
Monica record sets are really easy, especially when they are explained by Richard.
Start here:Recordsets

And for full details go to developer lessons starting I believe at lesson 15.
Adam Schwanz  @Reply  
           
9 months ago
Yea record sets can be confusing to start out with, but they are also one of the most powerful tools you get to use in Access, especially with manipulating data. So it's good to get some exposure and practice with them.

If ChangeT is blank if no change is needed, then the recordset would do nothing because they would never match, and rs2 would always be EOF. The code would never run.

If ChangeT had a month and a division that matched a month and a division in the importT, then it would apply it over that. I just assumed that was what made it unique. If you have multiple records with the same month and division, it would apply to all of them, in which case you need to use other fields to find something to make it unique.

You could just make a backup of your data, and give that recordset a shot, see if it is changing things that it shouldn't or if it works how you need it, and come back here for more help if needed.
Monica Jones OP  @Reply  
       
9 months ago
Adam The code works a little TOO well. It's just replacing everything for the division with either the corrected number or just erasing the original number. I can do what I need with SQL (see below). The problem is I have 47 columns that may or may not need updates each month and this is just the first of six files.

UPDATE ChangeT INNER JOIN ImportT ON (ChangeT.DIV_NN = ImportT.DIV_NN) AND (ChangeT.Month = ImportT.Month) AND (ChangeT.Year = ImportT.Year) SET ImportT.AHPPH_ACT_FRQ = [ChangeT].[AHPPH_ACT_FRQ] WHERE (((ChangeT.AHPPH_ACT_FRQ) Is Not Null));

I tried adding a WHERE clause to your code from last night, but Access yelled at me.

I went back to SQL and tried Updating actuals of two different elements but unless I'm updating both (unlikely) it updates the expense number and erases the second element 'cause there were no updates.
Monica Jones OP  @Reply  
       
9 months ago
To be clear, the recordset code erases the original number because I have updates to all the divisions, but on different elements. I only have 7 updates for actual expense of 30 divisions.
Adam Schwanz  @Reply  
           
9 months ago
Ah I see, so ChangeT might only have one field entered that needs changed, it doesn't have the full correct record. So you would have to check it then beforehand if there's a value, change it, otherwise don't. So you would do something like the below then.

If you had a lot of fields to check I would probably make a separate function too that checks if there is a value just to simplify the code so you could do something like this which would be easier to read. You could also make it cycle through the fields itself so you don't have to type it a million times if there are a lot of fields.
If IsThereAValue(rs2!ImportExpenseActual) Then rs!ImportExpenseActual = rs2!ImportExpenseActual

    'ChangeT has the updates
    'ImportT is the original file that is getting changed
    
    Dim rs As Recordset, rs2 As Recordset
    Set rs = CurrentDb.OpenRecordset("ImportT")
        
    'Look through The Import
    While Not rs.EOF
        Set rs2 = CurrentDb.OpenRecordset("Select * From ChangeT Where importdivision='" & rs!importDivision & "' and importmonth='" & rs!importMonth & "'")
        'If Match Then Update to ChangeT Record Stuff
        If Not rs2.EOF Then
            rs.Edit
            'Check If Value
            If IsNull(rs2!ImportExpenseActual) or rs2!ImportExpenseActual="" Then
              'There is no value do nothing
            Else
              'There is a value
              rs!ImportExpenseActual = rs2!ImportExpenseActual
            End If
            rs.Update
        End If
    rs.MoveNext
    Wend
    
    rs2.Close
    rs.Close
    Set rs2 = Nothing
    Set rs = Nothing
Monica Jones OP  @Reply  
       
9 months ago
I found another way to do it. It's still going to be a long process, but once it's done, it's done. Thank you for all your assistance.
Dim dbs as DAO.Database
Dim ImportExpenseActual As String, ImportExpensePlan As String
Set dbs = CurrentDb
ImportExpenseActual = "(copy/paste sql)"
ImportExpensePlan = "(copy/paste sql)"
dbsExecute ImportExpenseActual, dbFailOnError
dbsExecute ImportExpensePlan, dbFailOnError

Status "Updates were successfully imported!"

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

Next Unseen

 
New Feature: Comment Live View
 
 

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: 5/6/2026 5:54:57 AM. PLT: 1s