+ Reply to Thread
Results 1 to 4 of 4

Compile Error

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-02-2013
    Location
    Hollywood, Fl
    MS-Off Ver
    Excel 2010
    Posts
    110

    Compile Error

    I'm trying to add a validation on a box within a userform that I am working on. I want to make sure that if the user selects the value Direct within a combo box within the userform, they will be unable to leave the TxtAmt box empty. They will have to enter a numeric value within the box if Direct is selected. I thought if I added
    If Me.cboDirectIndirect.Value = "Direct" Then
          Me.TxtAmt.Text <> ""
    End If
    Users would no longer be able to leave the amount box blank. Unfortunately, I cannot even get to the testing phase, because I encounter a Compile Error: Expected expression, and the <> operator is highlighted. I also tried to write it using
     Me.TxtAmt.Value
    instead of
     Me.TxtAmt.Text
    But that didn't work either. Can anyone tell me what I am doing wrong? I added the if statement at the end of the sub-query. Here's the entire sub-query:
    Private Sub txtAmt_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    
    If Me.txtAmt.Value = "" Then
            Me.OverShortAmt.Caption = ""
            Cancel = True
            MsgBox ("Amount Box cannot be empty! Please enter in a dollar amount in Numeric Form.")
            Exit Sub
        ElseIf Not IsNumeric(Me.txtAmt.Value) And Me.txtAmt.Value <> "" Or Me.txtAmt.Value = " " Then
            Cancel = True
            MsgBox ("Please enter in a valid numeric amount! NO TEXT OR SPACES!")
            Exit Sub
        End If
    
        With Me.OverShortAmt
            If Me.txtAmt.Value < 0 Then
                .BackStyle = fmBackStyleTransparent
                .BackColor = rgbAquamarine
                .ForeColor = vbRed
                .Caption = "Short"
                .Font = Bold
            Else
                .BackStyle = fmBackStyleTransparent
                .BackColor = rgbWhite
                .ForeColor = vbBlack
                .Caption = "Over"
                .Font = Bold
            End If
        End With
    
    
    
    If Me.txtAmt.Value = "" Then
        Me.OverShortAmt.Caption = ""
    End If
    
    
    If Me.cboDirectIndirect.Value = "Direct" Then
        Me.txtAmt.Text <> ""
        
    End If
    
    End Sub
    I appreciate any and all help. Thanks!

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Compile Error

    <> is a comparison operator; = is both a comparison operator and an assignment operator, depending on context, and assignment is what you need here:

    Me.TxtAmt.Text = ""
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Compile Error

    Why are you triggering it as this event?

    txtAmt_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    This line is problematic (and incorrect)

    ElseIf Not IsNumeric(Me.txtAmt.Value) And Me.txtAmt.Value <> "" Or Me.txtAmt.Value = " " Then
    '  maybe do this instead
    ElseIf Not IsNumeric(Me.txtAmt.Value) Or Len(Trim(Me.txtAmt.Value)) = 0 Then
    This isn't really doing much of anything. All you are doing is changing txtAmt.Text. Maybe this:
    If Me.cboDirectIndirect.Value = "Direct" Then
        If Len(Trim(Me.txtAmt.Text)) = 0 Then
            MsgBox ("You must enter a value in txtAmt")
            Exit Sub
        End If
    End If

  4. #4
    Forum Contributor
    Join Date
    04-02-2013
    Location
    Hollywood, Fl
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Compile Error

    Quote Originally Posted by stnkynts View Post
    Why are you triggering it as this event?

    txtAmt_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    This line is problematic (and incorrect)

    ElseIf Not IsNumeric(Me.txtAmt.Value) And Me.txtAmt.Value <> "" Or Me.txtAmt.Value = " " Then
    '  maybe do this instead
    ElseIf Not IsNumeric(Me.txtAmt.Value) Or Len(Trim(Me.txtAmt.Value)) = 0 Then
    This isn't really doing much of anything. All you are doing is changing txtAmt.Text. Maybe this:
    If Me.cboDirectIndirect.Value = "Direct" Then
        If Len(Trim(Me.txtAmt.Text)) = 0 Then
            MsgBox ("You must enter a value in txtAmt")
            Exit Sub
        End If
    End If
    Thanks Stnkynts, that last example worked.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Excel macro (compile error. syntax error.) error
    By salar_younis in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-06-2014, 06:11 AM
  2. [SOLVED] Compile Error in Hidden Module and Compile Error: Can't find project or library
    By Taislin in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-10-2013, 07:03 PM
  3. Syntax Error and Compile Error: Expected Line Number or Label...
    By AnthonyWB in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-31-2011, 09:59 AM
  4. compile error: compile error expected
    By odeno in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-11-2008, 04:30 PM
  5. [SOLVED] VBAProject name compile error, not defined at compile time
    By Matthew Dodds in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-13-2005, 03:20 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1