Results 1 to 14 of 14
  1. #1
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776

    strsql insert into inserting double record into table?

    ProfileAccount -Test.zip
    Hi all,
    I have the code below and for some reason it is putting two records into my table for Credit, and one for Debit. It should only be putting one debit and one credit or just one credit if "IsPayrollDeduct = True"
    But i just cannot understand this at all. I am using the similiar in all my other forms and it works just fine. This form, it puts two credits in no matter what and one debit. It should not do that. I have tried going
    through it and setting up relationships and such with no success. What the heck? Is there something wrong with my code?
    Thanks
    Dave ---- I went ahead and removed necessary info from db and uploaded it for reference. If you go to frmAccountDetail and go to City Of Hominy under account search, that has an unpaid bill in it that you can s
    see what is going on maybe? I am stumped....


    Code:
    Private Sub CmdPostPayment_Click()
        Dim strsql As String
        Dim db As DAO.Database
        Set db = CurrentDb
        
        If Me.IsPayrollDeduct = False Then
            strsql = "INSERT INTO tblAccountLedger (TransactionID, TransTypeID, TransCode, TransDate, AccountID, DescriptionID, TransMethodID, Debit) " & _
                "VALUES (" & TransactionID & ", " & CboTransType & ", '" & TransCode & "', #" & TransDate & "#, " & CboFromAccount & ", " & DescriptionID & ", " & CboTransMethod & ", " & TransAmount & ")"
            Debug.Print strsql
            db.Execute strsql, dbFailOnError
    
    
            strsql = "INSERT INTO tblAccountLedger (TransactionID, TransTypeID, TransCode, TransDate, AccountID, DescriptionID, TransMethodID, Credit) " & _
                "VALUES (" & TransactionID & ", " & CboTransType & ", '" & TransCode & "', #" & TransDate & "#, " & CboToAccount & ", " & DescriptionID & ", " & CboTransMethod & ", " & TransAmount & ")"
            Debug.Print strsql
            db.Execute strsql, dbFailOnError
    
    
    Else
            strsql = "INSERT INTO tblAccountLedger (TransactionID, TransTypeID, TransCode, TransDate, AccountID, DescriptionID, TransMethodID, Credit) " & _
                "VALUES (" & TransactionID & ", " & CboTransType & ", '" & TransCode & "', #" & TransDate & "#, " & CboToAccount & ", " & DescriptionID & ", " & CboTransMethod & ", " & TransAmount & ")"
            Debug.Print strsql
            db.Execute strsql, dbFailOnError
    End If
    
    
            Me.IsPaid = True
        CurrentDb.Execute "UPDATE tblTransBillLedger SET IsPaid = True WHERE TransBillID=" & Forms!frmTransPayment!TransBillID
        db.Execute strsql, dbFailOnError
        
        Me.RecordLock = True
        Call Form_Current
    
    
    End Sub

    Last edited by d9pierce1; 01-25-2022 at 06:27 PM. Reason: upload db

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,428
    for some reason you are executing it twice, once within the if part and again after the if part

    Easy to see if you step through the code

  3. #3
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Thanks, I really dont get this step through code. I open vba editor, go to my code in question, and press F8 or step into and all it does is ding. No nothing, just a bell in background.
    I have reviewed many videos of this of how, but it doesnt appear to me i am doing it correctly and there are no errors so not sure what to even look for.
    Thanks\
    Dave

  4. #4
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Code:
    Private Sub CmdPostPayment_Click()
        Dim strsql As String
        Dim db As DAO.Database
        Set db = CurrentDb
        
        If Me.IsPayrollDeduct = False Then
            strsql = "INSERT INTO tblAccountLedger (TransactionID, TransTypeID, TransCode, TransDate, AccountID, DescriptionID, TransMethodID, Debit) " & _
                "VALUES (" & TransactionID & ", " & CboTransType & ", '" & TransCode & "', #" & TransDate & "#, " & CboFromAccount & ", " & DescriptionID & ", " & CboTransMethod & ", " & TransAmount & ")"
            Debug.Print strsql
            db.Execute strsql, dbFailOnError
    
    
            strsql = "INSERT INTO tblAccountLedger (TransactionID, TransTypeID, TransCode, TransDate, AccountID, DescriptionID, TransMethodID, Credit) " & _
                "VALUES (" & TransactionID & ", " & CboTransType & ", '" & TransCode & "', #" & TransDate & "#, " & CboToAccount & ", " & DescriptionID & ", " & CboTransMethod & ", " & TransAmount & ")"
            Debug.Print strsql
            db.Execute strsql, dbFailOnError
    
    
    Else
            strsql = "INSERT INTO tblAccountLedger (TransactionID, TransTypeID, TransCode, TransDate, AccountID, DescriptionID, TransMethodID, Credit) " & _
                "VALUES (" & TransactionID & ", " & CboTransType & ", '" & TransCode & "', #" & TransDate & "#, " & CboToAccount & ", " & DescriptionID & ", " & CboTransMethod & ", " & TransAmount & ")"
            Debug.Print strsql
            db.Execute strsql, dbFailOnError
    End If
    
    
            Me.IsPaid = True
        CurrentDb.Execute "UPDATE tblTransBillLedger SET IsPaid = True WHERE TransBillID=" & Forms!frmTransPayment!TransBillID
        debug.print strsql    
        db.Execute strsql, dbFailOnError
        
        Me.RecordLock = True
        Call Form_Current
    That red strsql line is still set to whatever it was in the if-else above.
    Add the blue debug.print strsql and surprise!

  5. #5
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    I have somewhat stepped through it I think? Not real sure as doesnt come up with anything but does stop where I put break point.
    starting to figure this step through out....
    When I debug.Print, it only shows the two inserts, all correct info but it puts two credits in ledger table for some reason unknow to me
    and is really driving me nuts. Have spent all day on this one thing. I had this happen once before or similiar and it was due to not having
    a relationship which i went through today and tried all sorts of relationships in the relship window. Nothing helped at all. So please tell me
    why this is putting in two of the same records on the strsql statement? Something weird here and i am at a total loss for solutions.
    Thanks

  6. #6
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Hello davegri
    Thankk you so much.... Wow, I have literally spent all day on this...
    What do I owe ya... Thank you so much
    Dave

  7. #7
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Just keep on truckin'. Thanks for the star!

  8. #8
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,428
    stepping through means the code steps through each line by repeatedly pressing the F8 key - you can check values as you go by hovering over the variable - or do as davegri has done, add a debug line. Looks like you ignored my first sentence, he has pointed out exactly what I pointed out. The problem with just putting the debug line is you still need to know where to put it. But seems you rely heavily of davegri to help you out so I'll butt out and won't bother you again.

  9. #9
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Hi Ajax,
    I really didnt know how to step through the code. I have since practiced now that I have an idea of how to do this. All i got was a ding when i tried it and have watched so many videos on it but just didnt make sense to me.
    In any event, i was able to step through some code yesterday and will continue to practice that..
    Thanks
    Dave

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,933
    Quote Originally Posted by d9pierce1 View Post
    Hi Ajax,
    I really didnt know how to step through the code. I have since practiced now that I have an idea of how to do this. All i got was a ding when i tried it and have watched so many videos on it but just didnt make sense to me.
    In any event, i was able to step through some code yesterday and will continue to practice that..
    Thanks
    Dave
    You really need to start learning then. It is brilliant at finding what I call silly errors, when it should be working.
    I find with forms, you need to set a breakpoint and then click/enter whatever to get the code started.
    You cannot just enter a forms code and press F8. That will give you a 'ding'
    You can with Subs and Functions in modules though.
    I also use the immediate window a fair bit to test oneliners like a Dlookup, Date format etc.
    That is why there is a link for debugging in my signature???

    Here is something I just used to try and help someone on UA
    Attached Thumbnails Attached Thumbnails Capture.PNG  
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  11. #11
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Thank you welshgasman,
    I will copy your link and hopefully it will be more descriptive then some of the vidoes i have watched so far.....
    I really need one for dummies as when doing this, i figured out yesterday i had to run the form to get it to fire but not one video i watched said that.
    I guess that would be common sense now that I look back at it.
    Anyway, I will definately go over your links...
    Thanks you,
    Dave

  12. #12
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Hi welshgasman,
    I watched the first video so far and gave me some insite. I do have a couple of questions.

    First, it was nice to learn that the curser needs to be in code box to use run.... I didnt know this and it always came up run a macro.... Nice bit of info to know
    Also, He has a debug.Print "sSql: " & sSql How does one know what to put after the debug.print as I have shown in red?
    Thanks
    Dave

  13. #13
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725
    Dave,

    Anything within the quotes is simply a string of your choosing.
    Debug.print "This is a message for Dave " . The ampersand concatenates the value of a variable/control..

    Debug.print "A short msg: " & Date

    Could produce: A short msg: 27-Jan-2022 in the immediate window.

  14. #14
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,933
    Quote Originally Posted by d9pierce1 View Post
    Hi welshgasman,
    I watched the first video so far and gave me some insite. I do have a couple of questions.

    First, it was nice to learn that the curser needs to be in code box to use run.... I didnt know this and it always came up run a macro.... Nice bit of info to know
    Also, He has a debug.Print "sSql: " & sSql How does one know what to put after the debug.print as I have shown in red?
    Thanks
    Dave
    If you just debug.print variablename, then you need to know what each is?
    If you add some context to it with "First sql is : " & strSQL1, then you know what you are looking at.?

    Remember you could output a bunch of debug.prints before you hit your next breakpoint.

    If you learn debugging, then you will set breakpoints at various locations, and let the code run between them with F5.
    If you do not learn debugging, then you will struggle forever and forever, and it will only slow development down.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 06-04-2021, 10:34 AM
  2. Help~ Ado Vba - trying to call strsql(INSERT)
    By fluffyvampirekitten in forum Access
    Replies: 4
    Last Post: 11-02-2015, 07:58 PM
  3. Replies: 3
    Last Post: 01-27-2015, 12:25 PM
  4. Replies: 14
    Last Post: 03-19-2013, 12:16 AM
  5. Replies: 2
    Last Post: 05-03-2011, 01:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums