Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   Index   Templates   Seminars   TechHelp   Forums   Help   Contact   Join   Order   Logon  
 
Home > TechHelp > Directory > Access > Track Changes < Form Backgrounds | Command Line >
Back to Track Changes    Comments List
Upload Images   Link   Email  
Issue with VBA
Glenn Kraniske 
    
3 years ago
Hello All,
I watched a video that Rick posted about logging database changes to another table.  See below;

https://www.youtube.com/watch?v=3sIHMGxugL0&t=313s

It works great right up until I try to add a new record.  I get a message that says:

Rintime Error '3075'
Syntax error (missing operator) in query expression 'FishingPoleID='

Here's the VBA code I used:

Private Sub Form_BeforeUpdate(Cancel As Integer)

    DoCmd.SetWarnings False
    DoCmd.RunSQL "INSERT INTO dbo_FishingPoleChangeLog SELECT * FROM dbo_FishingPoles " & _
    "WHERE FiishingPoleID=" & FishingPoleID
    DoCmd.SetWarnings True
    
End Sub


Any help would be appreciated.  Oh and by the way, I'm using a SQL Server backend.  Thanks in advance.

Glenn
Alex Hedley 
            
3 years ago
MsgBox FishingPoleID
What do you get?
Kevin Robertson 
          
3 years ago
You have a mis-spelling in this line

"WHERE FiishingPoleID=" & FishingPoleID
Glenn Kraniske OP 
    
3 years ago
Forgive me but I'm new at this.  I saw this mis-spelling just now but rest assured, there isn't one in the database.  I'm just using fishing poles so I don't have to divulge the actual db name.  Not sure what to do with the MsgBox thing.
Adam Schwanz 
             
3 years ago
Is FishingPoleID a short text field by chance? You may need Double Double Quotes
Glenn Kraniske OP 
    
3 years ago
It's an autonumber in Access, an INT in sql server
Glenn Kraniske OP 
    
3 years ago
Still tearing my hair out.  I had to put a TIMESTAMP column in the table just so I could add and change records.  I was getting a write conflict error which is why I had to add the TIMESTAMP column.  I've searched all over the internet for VBA code to add to my database to get it to add the changed data to the appropriate table but all I'm getting is errors.  Here's the code if anyone could possible tweak it for me.  Thanks again.

Private Sub Form_Dirty(Cancel As Integer)
    DoCmd.SetWarnings False
    DoCmd.RunSQL "SELECT * INTO FiishingTemp FROM dbo_FishingPoles"
    DoCmd.RunSQL "ALTER TABLE FishingTemp DROP COLUMN TMP"
    DoCmd.RunSQL "SELECT * FROM FishingTemp"
    DoCmd.RunSQL "DROP TABLE FishingTemp"
    DoCmd.RunSQL "INSERT INTO dbo_FishingPoleChangeLog SELECT * FROM FishingTemp " & _
    "WHERE FishingPoleID=" & FishingPoleID
    DoCmd.SetWarnings True
End Sub
Alex Hedley 
            
3 years ago
If you hardcore the ID and run the SQL manually in a Query window does it work?
Glenn Kraniske OP 
    
3 years ago
Yes - I only tried this portion of it but it worked.

DoCmd.RunSQL "SELECT * INTO FiishingTemp FROM dbo_FishingPoles"
DoCmd.RunSQL "ALTER TABLE FishingTemp DROP COLUMN TMP"
DoCmd.RunSQL "SELECT * FROM FishingTemp"
DoCmd.RunSQL "DROP TABLE FishingTemp"
Kevin Robertson 
          
3 years ago
It looks like you are referring to a table (FishingTemp) in your INSERT Into statement that has already been deleted.
Glenn Kraniske OP 
    
3 years ago
Yeah when I ran it in the SQL Management Studio it errored out so I moved the DROP TABLE statement to the end.  It works if I run it in SQL Management Studio but I'm not sure what the syntax is in VBA.
Glenn Kraniske OP 
    
3 years ago
I only want the changed records to get copied to the ChangeLog table
Alex Hedley 
            
3 years ago
I think you're trying too much at once
Break it down into single steps.

Step 1
Can you insert a single record into a table

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

Try this in the Query window of Access manually.
Glenn Kraniske OP 
    
3 years ago
Alex,
Thanks for your help, I truly appreciate it.  Yes, that worked just fine.
Alex Hedley 
            
3 years ago
Great, next step is

INSERT INTO dbo_FishingPoleChangeLog SELECT * FROM dbo_FishingPoles WHERE FishingPoleID=1

Swap 1 for a valid id.
Glenn Kraniske OP 
    
3 years ago
Alex,
No that doesn't work.  The TMP field is a TIMESTAMP type field in the SQL database.  I had to add it so I could insert more records.  When I started messing around with the code I got a write conflict error.  I'll upload a screen shot.
Glenn Kraniske OP 
    
3 years ago

Alex Hedley 
            
3 years ago
Is TMP a datetime field?
Update the below with a new Date() or Now() etc

INSERT INTO dbo_FishingPoleChangeLog SELECT *, 'TIMESTAMP' AS TMP FROM dbo_FishingPoles WHERE FishingPoleID=1
Kevin Robertson 
          
3 years ago
Could this be the reason for the unknown field error?
Kevin Robertson 
          
3 years ago

Glenn Kraniske OP 
    
3 years ago
I'm not sure.  Like I said I had to add that column in the SQL table just so I could enter and modify records.  Without it I get a write conflict.  In the SQL table it's a TIMESTAMP data type, however, in Access it shows up as BINARY and I don't think Access knows how to deal with it which is why I dropped the column in the VBA code.  I'm definitely open to recommendations.
Alex Hedley 
            
3 years ago
Did the next step work?
Glenn Kraniske OP 
    
3 years ago
Everything worked as long as I didn't include the BINARY column in the SQL query.  In VBA I'm not sure where the breakdown occurred.
Alex Hedley 
            
3 years ago
Not following,
The second piece of SQL I asked you to try (INSERT INTO) did that work?
Glenn Kraniske OP 
    
3 years ago
No, it errored out on the TMP column
Alex Hedley 
            
3 years ago
What SQL did you try and what was the error?

(Also is it worth creating the table in Access and transferring it to MS SQL instead of creating it directly in MS SQL, although when you pull it down to Access it should understand it, does it show the datetime correctly in a Table view or Form?)
Glenn Kraniske OP 
    
3 years ago
Hello again everybody,

Thank you all for your help, I just wanted to give you all an update.  I was able to accomplish this task using a SQL Server "Trigger."  It's a work in progress but it's coming along.  If you want to see the code let me know and give me a way to contact you so I don't have to publicly post it.  Have a great day!!

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

 
 
 

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 2025 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 2/15/2025 1:01:49 AM. PLT: 1s