Page 1 of 3 123 LastLast
Results 1 to 15 of 39
  1. #1
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776

    Submitt Button with Muti options?

    Hi all,
    I have the following code on a submitt button. I am missing something between If statements so that if its not the first IF, then go to second If... and so on? Maybe i need a select case senario or ?????
    Can you help please....

    Code:
    Private Sub CmdSubmit_Click()
    
    
        Me.TxtTransNumber = [TxtRandom]
        
    If Me.TxtTransType = "Deposit" Then
        DoCmd.SetWarnings False
        DoCmd.RunSQL "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Credit) " & _
            "VALUES (" & TxtTransID & ", " & CboToAccount & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "', '" & TxtTransType & " From " & CboCompany.Column(1) & "', '" & TxtMethod & "' , " & TxtTransAmount & ")"
    If Me.ChkDepositRemburseExp = True Then
        DoCmd.RunSQL "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Debit) " & _
            "VALUES (" & TxtTransID & ", " & CboRE & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "',  " & TxtTransDescription & " , '" & TxtMethod & "' , " & TxtTransAmount & ")"
        DoCmd.SetWarnings True
    
    
    
    
    If Me.TxtTransType = "Transfer" Or "Payment" Then
        DoCmd.SetWarnings False
        DoCmd.RunSQL "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Debit) " & _
            "VALUES (" & TxtTransID & ", " & CboFromAccount & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "', '" & TxtTransType & " To " & CboToAccount.Column(1) & "', '" & TxtMethod & "' , " & TxtTransAmount & ")"
        DoCmd.RunSQL "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Credit) " & _
            "VALUES (" & TxtTransID & ", " & CboToAccount & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "', '" & TxtTransType & " From " & CboFromAccount.Column(1) & "', '" & TxtMethod & "' , " & TxtTransAmount & ")"
        DoCmd.SetWarnings True
    
    
    
    
    If Me.TxtTransType = "Purchase" Then
        DoCmd.SetWarnings False
        DoCmd.RunSQL "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Debit) " & _
            "VALUES (" & TxtTransID & ", " & CboFromAccount & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "', '" & TxtTransType & " From " & CboCompany.Column(1) & "', '" & TxtMethod & "' , " & TxtTransAmount & ")"
    If Me.ChkIsRemburseExp = True Then
        DoCmd.RunSQL "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Credit) " & _
            "VALUES (" & TxtTransID & ", " & CboRE & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "',  " & TxtTransDescription & " , '" & TxtMethod & "' , " & TxtTransAmount & ")"
        DoCmd.SetWarnings True
    
    
    
    
    If Me.TxtTransType = "Record Bill" Then
        DoCmd.SetWarnings False
        DoCmd.RunSQL "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Debit) " & _
            "VALUES (" & TxtTransID & ", " & CboToAccount & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "', '" & TxtTransType & " From " & CboCompany.Column(1) & "', '" & TxtMethod & "' , " & TxtTransAmount & ")"
        DoCmd.SetWarnings True
    End If
        End If
            End If
                End If
                    End If
                        End If
    End Sub
    Thanks


    Dave

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    To help, one would have to understand the requirements. You've nested them all, so the first condition must be true, then the next also, then the next also and so on. You are testing for transfer type being different several times yet those tests are nested. If it isn't Deposit, none of the rest of it will execute. If it is, then it cannot also be the other options, so none of those will execute.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Is there a way for if the first conditions isnt true, then it goes to the next condition?

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    You might want to indent correctly, then it is easier to spot issues?
    Code:
    Private Sub CmdSubmit_Click()
    
    
        Me.TxtTransNumber = [TxtRandom]
    
        If Me.TxtTransType = "Deposit" Then
            DoCmd.SetWarnings False
            DoCmd.RunSQL "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Credit) " & _
                         "VALUES (" & TxtTransID & ", " & CboToAccount & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "', '" & TxtTransType & " From " & CboCompany.Column(1) & "', '" & TxtMethod & "' , " & TxtTransAmount & ")"
            If Me.ChkDepositRemburseExp = True Then
                DoCmd.RunSQL "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Debit) " & _
                             "VALUES (" & TxtTransID & ", " & CboRE & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "',  " & TxtTransDescription & " , '" & TxtMethod & "' , " & TxtTransAmount & ")"
                DoCmd.SetWarnings True
    
    
    
    
                If Me.TxtTransType = "Transfer" Or "Payment" Then
                    DoCmd.SetWarnings False
                    DoCmd.RunSQL "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Debit) " & _
                                 "VALUES (" & TxtTransID & ", " & CboFromAccount & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "', '" & TxtTransType & " To " & CboToAccount.Column(1) & "', '" & TxtMethod & "' , " & TxtTransAmount & ")"
                    DoCmd.RunSQL "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Credit) " & _
                                 "VALUES (" & TxtTransID & ", " & CboToAccount & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "', '" & TxtTransType & " From " & CboFromAccount.Column(1) & "', '" & TxtMethod & "' , " & TxtTransAmount & ")"
                    DoCmd.SetWarnings True
    
    
    
    
                    If Me.TxtTransType = "Purchase" Then
                        DoCmd.SetWarnings False
                        DoCmd.RunSQL "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Debit) " & _
                                     "VALUES (" & TxtTransID & ", " & CboFromAccount & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "', '" & TxtTransType & " From " & CboCompany.Column(1) & "', '" & TxtMethod & "' , " & TxtTransAmount & ")"
                        If Me.ChkIsRemburseExp = True Then
                            DoCmd.RunSQL "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Credit) " & _
                                         "VALUES (" & TxtTransID & ", " & CboRE & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "',  " & TxtTransDescription & " , '" & TxtMethod & "' , " & TxtTransAmount & ")"
                            DoCmd.SetWarnings True
    
    
    
    
                            If Me.TxtTransType = "Record Bill" Then
                                DoCmd.SetWarnings False
                                DoCmd.RunSQL "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Debit) " & _
                                             "VALUES (" & TxtTransID & ", " & CboToAccount & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "', '" & TxtTransType & " From " & CboCompany.Column(1) & "', '" & TxtMethod & "' , " & TxtTransAmount & ")"
                                DoCmd.SetWarnings True
                            End If
                        End If
                    End If
                End If
            End If
        End If
    End Sub

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    You might want to indent correctly, then it is easier to spot issues?
    If what you say is true, then the ifs should not be nested, and you shpuld exit the sub when true?

    Code:
    Private Sub CmdSubmit_Click()
    
    
        Me.TxtTransNumber = [TxtRandom]
    
        If Me.TxtTransType = "Deposit" Then
            DoCmd.SetWarnings False
            DoCmd.RunSQL "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Credit) " & _
                         "VALUES (" & TxtTransID & ", " & CboToAccount & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "', '" & TxtTransType & " From " & CboCompany.Column(1) & "', '" & TxtMethod & "' , " & TxtTransAmount & ")"
            If Me.ChkDepositRemburseExp = True Then
                DoCmd.RunSQL "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Debit) " & _
                             "VALUES (" & TxtTransID & ", " & CboRE & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "',  " & TxtTransDescription & " , '" & TxtMethod & "' , " & TxtTransAmount & ")"
                DoCmd.SetWarnings True
    
    
    
    
                If Me.TxtTransType = "Transfer" Or "Payment" Then
                    DoCmd.SetWarnings False
                    DoCmd.RunSQL "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Debit) " & _
                                 "VALUES (" & TxtTransID & ", " & CboFromAccount & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "', '" & TxtTransType & " To " & CboToAccount.Column(1) & "', '" & TxtMethod & "' , " & TxtTransAmount & ")"
                    DoCmd.RunSQL "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Credit) " & _
                                 "VALUES (" & TxtTransID & ", " & CboToAccount & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "', '" & TxtTransType & " From " & CboFromAccount.Column(1) & "', '" & TxtMethod & "' , " & TxtTransAmount & ")"
                    DoCmd.SetWarnings True
    
    
    
    
                    If Me.TxtTransType = "Purchase" Then
                        DoCmd.SetWarnings False
                        DoCmd.RunSQL "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Debit) " & _
                                     "VALUES (" & TxtTransID & ", " & CboFromAccount & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "', '" & TxtTransType & " From " & CboCompany.Column(1) & "', '" & TxtMethod & "' , " & TxtTransAmount & ")"
                        If Me.ChkIsRemburseExp = True Then
                            DoCmd.RunSQL "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Credit) " & _
                                         "VALUES (" & TxtTransID & ", " & CboRE & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "',  " & TxtTransDescription & " , '" & TxtMethod & "' , " & TxtTransAmount & ")"
                            DoCmd.SetWarnings True
    
    
    
    
                            If Me.TxtTransType = "Record Bill" Then
                                DoCmd.SetWarnings False
                                DoCmd.RunSQL "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Debit) " & _
                                             "VALUES (" & TxtTransID & ", " & CboToAccount & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "', '" & TxtTransType & " From " & CboCompany.Column(1) & "', '" & TxtMethod & "' , " & TxtTransAmount & ")"
                                DoCmd.SetWarnings True
                            End If
                        End If
                    End If
                End If
            End If
        End If
    End Sub

  6. #6
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Thank you for pointing that out on the indents and for the Exit Sub....
    Do I put an exit sub after each of the conditions?
    Thanks

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    I would have gone for Select Case on the TranType?

    Once you have run code for the true condition, there is no point checking anymore is there.?, so you may as well exit.?
    If you go the Select Case route then there is no need for an exit sub.
    If you go for the If route for TranType then each needs to be in it's own right, not nested as you have it now.?

    EG: If Trantype is NOT Deposit then the rest of the code is not even entered?

  8. #8
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    I will try out a select case, i wondered if that was the way to go!
    Thank you

  9. #9
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Gasman beat me to it but I was mocking up the code.
    You could really clean up your code with select case and using a db variable or currentdb to eliminate turning warnings on and off.

    something along these lines

    Code:
    Private Sub CmdSubmit_Click()
    
    Dim strSql As String
    Dim db As DAO.Database
    Set db = CurrentDb
       
        Me.TxtTransNumber = [TxtRandom]
    
        Select Case Me.TxtTransNumber
    
        Case "Deposit"
        
            If Me.ChkDepositRemburseExp = False Then
            
                strSql = "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Credit) " & _
                         "VALUES (" & TxtTransID & ", " & CboToAccount & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "', '" & TxtTransType & " From " & CboCompany.Column(1) & "', '" & TxtMethod & "' , " & TxtTransAmount & ")"
            Else
    
                strSql = "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Debit) " & _
                         "VALUES (" & TxtTransID & ", " & CboRE & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "',  " & TxtTransDescription & " , '" & TxtMethod & "' , " & TxtTransAmount & ")"
    
            End If
    
        Case "Transfer", "Payment"
    
        Case "Purchase"
    
        Case "Record Bill"
    
        Case Else
    
        End Select
        
        db.Execute strSql, dbFailOnError
        
        Set db = Nothing
        
      End Sub
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  10. #10
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Is there a way for if the first conditions isnt true, then it goes to the next condition?
    Sub DoStuff()
    - do some test and if not true
    Exit Sub
    End Sub

    I would probably do the Select Case block too, but as I've said, to me the requirements are not clear.
    I mean if the first test of TxtTransType is not "Deposit" do we go on to If Me.ChkDepositRemburseExp or not?
    If that's an independent test, then AFAIK, you cannot test two conditions like that in one Case.
    Last edited by Micron; 12-04-2020 at 02:58 PM. Reason: code correction
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  11. #11
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Wow, this is really close. Sorry i didnt be more clear.

    So, I actually want to have it insert both conditions, Basically i am crediting one acccount and debiting a different account If the Me.ChkDepositRemburseExp Condition is True, if not, then just do nothing for that condition.

  12. #12
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Then I think you have it in post 9?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  13. #13
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    "IN THIS FIRST CONDITION I WANT IT TO INSERT AS LONG AS ITS A DEPOSIT, AND THE SECOND CONDITION TO INSERT IF TRUE"
    I am trying to get if deposit to insert the first condition, and also insert the second condition if its true. It looks to me as if the chk is False, then it will insert first condition, but if true then insert the second condition?

    Me.TxtTransNumber = [TxtRandom]

    Select Case Me.TxtTransNumber

    Case "Deposit"

    If Me.ChkDepositRemburseExp = False Then

    strSql = "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Credit) " & _
    "VALUES (" & TxtTransID & ", " & CboToAccount & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "', '" & TxtTransType & " From " & CboCompany.Column(1) & "', '" & TxtMethod & "' , " & TxtTransAmount & ")"
    Else

    strSql = "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Debit) " & _
    "VALUES (" & TxtTransID & ", " & CboRE & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "', " & TxtTransDescription & " , '" & TxtMethod & "' , " & TxtTransAmount & ")"

    End If

  14. #14
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Can I write the code as this?

    Code:
    Private Sub CmdSubmit_Click()
    
    
    Dim strSql As String
    Dim db As DAO.Database
    Set db = CurrentDb
       
        Me.TxtTransNumber = [TxtRandom]
    
    
        Select Case Me.TxtTransNumber
    
    
        Case "Deposit"
        
            If Me.ChkDepositRemburseExp = False Then
            
                strSql = "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Credit) " & _
                         "VALUES (" & TxtTransID & ", " & CboToAccount & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "', '" & TxtTransType & " From " & CboCompany.Column(1) & "', '" & TxtMethod & "' , " & TxtTransAmount & ")"
            Else
                strSql = "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Credit) " & _
                         "VALUES (" & TxtTransID & ", " & CboToAccount & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "', '" & TxtTransType & " From " & CboCompany.Column(1) & "', '" & TxtMethod & "' , " & TxtTransAmount & ")"
                strSql = "INSERT INTO AccountLedgerTbl (TransID, AccountID, TransDate, TransNumber, Description, Method, Debit) " & _
                         "VALUES (" & TxtTransID & ", " & CboRE & ", #" & TxtTransDate & "#, '" & TxtTransNumber & "',  " & TxtTransDescription & " , '" & TxtMethod & "' , " & TxtTransAmount & ")"
    
    
            End If
    
    
        Case "Transfer", "Payment"
    
    
        Case "Purchase"
    
    
        Case "Record Bill"
    
    
        Case Else
    
    
        End Select
        
        db.Execute strSql, dbFailOnError
        
        Set db = Nothing
        
      End Sub

  15. #15
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    So the first Insert does not need to be within an IF.?

Page 1 of 3 123 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Case Select Question for muti forms
    By d9pierce1 in forum Programming
    Replies: 2
    Last Post: 09-15-2019, 06:19 PM
  2. How to hide Access Options button?
    By ezz in forum Access
    Replies: 3
    Last Post: 11-24-2014, 11:58 AM
  3. Muti Value Export to Excel
    By bboudreau in forum Programming
    Replies: 1
    Last Post: 01-16-2012, 06:43 PM
  4. Access Options Button
    By 95DSM in forum Programming
    Replies: 1
    Last Post: 08-16-2010, 03:05 PM
  5. Toggle Button Options
    By Matthieu in forum Forms
    Replies: 2
    Last Post: 11-23-2009, 04:05 PM

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