Else/If statement

JPR

Registered User.
Local time
Today, 15:42
Joined
Jan 23, 2009
Messages
223
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
 
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
 
So what else can txt1 be? No pun intended :)
 
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.
 
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]
 
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?
 
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
 
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
 
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.
 
Thank you Minty. Removed the error line, made the changes and... the code works great now.
Appreciate everyone's help.
 

Users who are viewing this thread

Back
Top Bottom