Results 1 to 9 of 9
  1. #1
    Ayiramala is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Dec 2014
    Location
    Kerala, India
    Posts
    74

    Question on an If clause

    Hi,
    I am trying to use an If expression on the onclick event of a button, but just cannot get it right.
    This is the situation: There are two text fields. Both are formatted as Currency. What I want to say is, if one text box value is greater than the other, something should happen.

    Like this:

    If Me.txtCreditAmount.Value > Me.txtNetSalary.Value Then something



    Only it doesn't seem to evaluate it. But if I substitute the part after > with a fixed value (like say, 5000) it works. What might be wrong?
    Thanks!

  2. #2
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    What might be wrong?
    1. you don't need .value
    2. have you got 'option explicit' at the top of your module
    3. if not have you spelt the name of your txtnetsalary correctly?
    4. does txtnetsalary have a value (i.e. is not null)
    5. if it does is it less than the value in txtcreditamount?
    6. have you tried debugging to confirm the code is running?
    7. in your button control properties - does it say '[Event Procedure]' against the click event?

  3. #3
    Ayiramala is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Dec 2014
    Location
    Kerala, India
    Posts
    74
    I checked everything, but the problem persists. This is the code:

    Code:
    Private Sub openreport_Click()
    On Error GoTo Err_openreport_Click
    
        Dim stDocName As String
        Dim MyPath As String
        Dim MyFilenamePDF As String
        Dim MyFilenameRTF As String
        
        MyPath = "C:\"
        MyFilenamePDF = "rptBankStatement.pdf"
        MyFilenameRTF = "rptBankStatement.rtf"
        stDocName = "rptBankStatement"
        
        If IsNull(cboMonth) Then
        MsgBox "Please select month"
        Else
        If IsNull(Me.txtNetAmount) Then
        MsgBox "Enter Net Salary"
        Else
        If Me.txtTotal > Me.txtNetAmount Then
        MsgBox "The total amount to be credited is greater than net salary."
        Else
        DoCmd.openreport stDocName, acPreview
        DoCmd.OutputTo acOutputReport, "", acFormatPDF, MyPath & MyFilenamePDF, False
        DoCmd.OutputTo acOutputReport, "", acFormatRTF, MyPath & MyFilenameRTF, False
        MsgBox "Files saved in C:Drive"
        End If
        Exit Sub
    
        End If
        Exit Sub
        End If
        Exit Sub
        
    Exit_openreport_Click:
        Exit Sub
    
    Err_openreport_Click:
        MsgBox Err.Description
        Resume Exit_openreport_Click
        
    End Sub

  4. #4
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    you say you checked everything - so per my point 6 what happened when you debugged? It stepped through the code as you would expect? or it did something else?

    Suggest you get into the habit of insetting in your code - makes it difficult to read otherwise

  5. #5
    Ayiramala is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Dec 2014
    Location
    Kerala, India
    Posts
    74
    Yes, it stepped through the code upto the very end. Nothing happened.

  6. #6
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    which route did it take? Did it step through the docmd.outputto? if so have you checked your C directory for the existence of the file? do you have access rights to add files to this directory?

  7. #7
    Ayiramala is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Dec 2014
    Location
    Kerala, India
    Posts
    74
    I got it right. this thought just occurred to me that it might have something to do with the formatting of the text boxes. I checked and saw that they were not formatted as anything. I set it to currency and it works now.

    Sorry that I bothered you unnecessarily, all because I was a little careless.
    But your help is greatly appreciated. Thanks.

  8. #8
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Not that it hurts, but you only need 1 "Exit Sub". Comment out the three red lines and test.
    Code:
    <snip>
                If Me.txtTotal > Me.txtNetAmount Then
                    MsgBox "The total amount to be credited is greater than net salary."
                Else
                    DoCmd.OpenReport stDocName, acPreview
                    DoCmd.OutputTo acOutputReport, "", acFormatPDF, MyPath & MyFilenamePDF, False
                    DoCmd.OutputTo acOutputReport, "", acFormatRTF, MyPath & MyFilenameRTF, False
                    MsgBox "Files saved in C:Drive"
                End If
                Exit Sub  '<--not needed
    
            End If
            Exit Sub  '<--not needed
    
        End If
        Exit Sub  '<--not needed
    
    Exit_openreport_Click:
        Exit Sub
    
    <snip>

  9. #9
    Ayiramala is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Dec 2014
    Location
    Kerala, India
    Posts
    74
    ssanfu,
    Tested without the three Exit Subs, and worked without a problem.

    Thanks for pointing out this.

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

Similar Threads

  1. Need help with a WHERE clause
    By DB88 in forum Access
    Replies: 10
    Last Post: 06-11-2014, 01:40 PM
  2. WHERE clause
    By fabiobarreto10 in forum Forms
    Replies: 5
    Last Post: 04-12-2012, 02:42 PM
  3. Using the TOP clause
    By WSlepecki in forum Queries
    Replies: 1
    Last Post: 04-08-2011, 06:59 AM
  4. Where clause
    By Amerigo in forum Queries
    Replies: 2
    Last Post: 03-30-2011, 07:34 AM
  5. IIF clause
    By Peljo in forum Queries
    Replies: 2
    Last Post: 02-05-2008, 11:22 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