Else/If statement (1 Viewer)

JPR

Registered User.
Local time
Today, 00:45
Joined
Jan 23, 2009
Messages
192
Hello,

I am having a problem with an IF/ELSE statement, although used many other times.
I have a cmd button on a form which executes or ends a certain code, depending on the value in txtbox1. Thank you for your help

Code:
If Me.txt1 = "partial" Then

MsgBox "Sorry, you cannot perform this action is you have selected partial"
' code should stop running
End If

If Me.txt1 = "total" Then
'''' Code continues running
end if
exit sub
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 02:45
Joined
Feb 28, 2001
Messages
27,148
Code:
If Me.txt1 = "partial" Then

MsgBox "Sorry, you cannot perform this action is you have selected partial"
' code should stop running

ElseIf Me.txt1 = "total" Then
'''' Code continues running
end if

exit sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:45
Joined
Sep 21, 2011
Messages
14,238
So what else can txt1 be? No pun intended :)
 

plog

Banishment Pending
Local time
Today, 02:45
Joined
May 11, 2011
Messages
11,638
1. There's no "else" in that code.

2. You never explained your problem.

3. Your code snippet is so simpllified it does nothing to help us understand the problem.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:45
Joined
Feb 19, 2002
Messages
43,233
There is nothing in your code to make it stop running. You need an exit sub or whatever.

When your code is mutually exclusive, a better structure to use is Select Case:

Code:
Select Case Me.txt1
    Case "partial"
        '''do something
    Case "total"
        ''' do something
    Case Else
        ''' do something
End Select
 

JPR

Registered User.
Local time
Today, 00:45
Joined
Jan 23, 2009
Messages
192
Hello. Sorry for not having simplified my post. I will try to explain it better.

I have a form which exports data to Word using bookmarks. Code should execute depending on the value I enter in textbox (txt1).

If the value in txt1 is "Partial", then I only want a msg box and after that the code should stop executing.

If the value in txt1 is "Total" then the code should execute exporting data to my Word template (the StrPath indicates the templates's path/name). Thank you

Code:
[CODE]On Error Resume Next

Dim objWord As Object
Dim strPath As String
Dim response As String
Dim MSG As String

If Me.txt1 = "partial" Then

MsgBox "Sorry, you cannot send this letter"

ElseIf Me.txt1 = "Total" Then

strPath = DLookup("[TemplateLocation]", "TablePath", "[TemplateID]='" & Me.[txtpathLetter] & "'")
Set objWord = CreateObject("Word.Application")

objWord.Visible = False

objWord.Documents.Add strPath

objWord.ActiveDocument.Bookmarks("Lname").Select
objWord.Selection.Text = Me.LNAME

objWord.ActiveDocument.Bookmarks("Address").Select
objWord.Selection.Text = Me.Address

End if
End sub
[/CODE]
 

ADIGA88

Member
Local time
Today, 10:45
Joined
Apr 5, 2020
Messages
94
I can't see what is the problem here it's should work as you stated. Do you get an error message or the code not behaving as expected when filling the txt1 textbox?
 

JPR

Registered User.
Local time
Today, 00:45
Joined
Jan 23, 2009
Messages
192
Exactly.

When I enter "Partial" in box 1, I get the msgbox and the code stops running.

If, on the other hand, I enter "Total" the code does not execute, meaning data is not exported to my Word template. Thank you
 

Minty

AWF VIP
Local time
Today, 08:45
Joined
Jul 26, 2013
Messages
10,368
When you are debugging remove your error handler.
Your code can be simplified assuming your text box can only have Partial or Total in it
Code:
Sub test()
    
    Dim objWord As Object
    Dim strPath As String
    Dim response As String
    Dim MSG As String
    
    '    On Error Resume Next '  This is a dangerous way to handle all errors it basically will ignore them and keep executing

    If Me.txt1 <> "Total" Then

        MsgBox "Sorry, you cannot send this letter"
        Exit Sub
    End If
    
    strPath = DLookup("[TemplateLocation]", "TablePath", "[TemplateID]='" & Me.[txtpathLetter] & "'")
    Set objWord = CreateObject("Word.Application")

    objWord.Visible = False

    objWord.Documents.Add strPath

    objWord.ActiveDocument.Bookmarks("Lname").Select
    objWord.Selection.Text = Me.LNAME

    objWord.ActiveDocument.Bookmarks("Address").Select
    objWord.Selection.Text = Me.Address

    
End Sub
 

JPR

Registered User.
Local time
Today, 00:45
Joined
Jan 23, 2009
Messages
192
sure, no errors. It was working up until a few days ago although erroneously because in both cases (whichever value I entered in txt1), it exported data to Word.
 

JPR

Registered User.
Local time
Today, 00:45
Joined
Jan 23, 2009
Messages
192
Thank you Minty. Removed the error line, made the changes and... the code works great now.
Appreciate everyone's help.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:45
Joined
Feb 19, 2002
Messages
43,233
Just to summarize, when you write If statements and you only care about one value there is no need to check the other value or to even have an Else clause. Minty simplified the statement to its essence by using a negative to eliminate all cases where you don't want the code to continue. She could have made a positive test but that would have looked confusing or would have required embedding a lot of code inside the if which isn't recommended because the code becomes harder to manage and understand. The positive test would have looked like:
Code:
If Me.txt1 = "Total" Then
Else
    MsgBox "Sorry, you cannot send this letter"
    Exit Sub
End If
'' all the code

OR

Code:
If Me.txt1 = "Total" Then
    '' all the code
Else
    MsgBox "Sorry, you cannot send this letter"
    Exit Sub
End If
 

Users who are viewing this thread

Top Bottom