Results 1 to 11 of 11

Thread: [RESOLVED] else if (uh boy)

  1. #1

    Thread Starter
    Addicted Member habenero's Avatar
    Join Date
    Aug 2015
    Posts
    217

    Resolved [RESOLVED] else if (uh boy)

    here is my "code"

    what changes to I have to make to either

    make one txt file or two or three?

    Private Sub cmd_exit_Click()
    Dim strfilename As String
    Dim strfilename1 As String
    If Option1 = True Then
    strfilename = "c:\lotto diary\OCR_8_2020_lottoPIK4" & Format(Date, "MM-DD-YYYY") & ".txt"
    Open strfilename For Output As #1
    Close #1
    End If
    ElseIf Option2 = True Then
    strfilename1 = "c:\lotto diary\OCR_8_2020_lottoPIK4" & Format(Date, "MM-DD-YYYY") & "a" & ".txt"
    Open strfilename1 For Output As #2
    Close #2
    End If
    ElseIf Option3 = True Then
    strfilename = "c:\lotto diary\OCR_8_2020_lottoPIK4" & Format(Date, "MM-DD-YYYY") & "b" & ".txt"
    Open strfilename For Output As #3
    Close #3
    End If
    End
    End Sub

    I know its so simple (again). Right now if I select the 3rd opt button I only get the 2nd text file

    TIA Jeff

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,664

    Re: else if (uh boy)

    You don't want to say, that this compiles, right? I see an If with an End If followed by an ElseIf

    Put the options-button in a control-array and use Select Case
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  3. #3

    Thread Starter
    Addicted Member habenero's Avatar
    Join Date
    Aug 2015
    Posts
    217

    Re: else if (uh boy)

    nope it doesnt compile - if I select option 3 all I get is the 2nd text file

    the compiler tells me Else with out if - an the option #2 button line

    BTW I really like your health line about unformatted code

  4. #4
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,402

    Re: else if (uh boy)

    Habenero, I completely rewrote the code you posted:

    Code:
    Private Sub cmd_exit_Click()
    Dim FileH As Integer
    Dim PathV As String
    
    
       PathV = "c:\lotto diary\OCR_8_2020_lottoPIK4" & Format(Date, "MM-DD-YYYY")
       FileH = FreeFile()
    
       Select Case True
          Case Option1.Value
             Open PathV & ".txt" For Output Lock Read Write As FileH
          Case Option2.Value
             Open PathV & "a.txt" For Output Lock Read Write As FileH
          Case Option3.Value
             Open PathV & "b.txt" For Output Lock Read Write As FileH
       End Select
    
       Close FileH
    
    Unload Me
    End Sub
    The above is much shorter and should do the same thing. See? A few tips:
    1. Don't use the End statement unless you absolutely have to. It is better to use something like "Unload Me".
    2. I don't recommend hardcoding a path. If you're writing to your application's directory use App.Path to retrieve its location. Note: I have heard App.Path can give you trouble with unicode characters in a path.
    3. You should give your controls (Option1, Option2, and Option3) meaningful names. Right now it is hard to tell what their purpose is from looking at your code.
    4. Use code tags to enclose your code in your forum posts. These tags can be inserted by clicking the "#" button in the toolbar above the message you're about to post.

    yours,
    Peter Swinkels
    Last edited by Peter Swinkels; Aug 23rd, 2020 at 10:18 AM. Reason: Improved my own code.

  5. #5

    Thread Starter
    Addicted Member habenero's Avatar
    Join Date
    Aug 2015
    Posts
    217

    Re: else if (uh boy)

    AOK

    I thank you two so much for the solutions - am sure both will work - I know hard coding path names is very bad but I do have support apps that take of that down the line

    will try the "case" option

    and your code...

    thanks so much Resolved

    Jeff

  6. #6
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,402

    Re: [RESOLVED] else if (uh boy)

    You're welcome! :-)

  7. #7
    Hyperactive Member
    Join Date
    Aug 2017
    Posts
    380

    Re: else if (uh boy)

    Quote Originally Posted by Peter Swinkels View Post
    The above is much shorter and should do the same thing.
    It can be made even more concise:

    Code:
    Private Sub cmd_exit_Click()
        Dim FN As Integer, strFileName As String
    
        strFileName = "C:\lotto diary\OCR_8_2020_lottoPIK4" _
                    & Format$(Date, "MM-DD-YYYY") _
                    & Switch(Option2, "a", Option3, "b") _
                    & ".txt"
    
        FN = FreeFile
        Open strFileName For Output As FN
        Close FN
    
        Unload Me
    End Sub

  8. #8

    Thread Starter
    Addicted Member habenero's Avatar
    Join Date
    Aug 2015
    Posts
    217

    Re: [RESOLVED] else if (uh boy)

    thanks so much for your effort



    Be safe have a great sunday

    Jeff

  9. #9
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    6,765

    Re: [RESOLVED] else if (uh boy)

    Yeah, personally, I just never use the ElseIf. I'm aware it exists, but I just always go to a Select Case (maybe even a Select Case True) when I've got more than two conditions to test. To my eyes, it's just cleaner.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  10. #10
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,402

    Re: [RESOLVED] else if (uh boy)

    Using Select Case can also save you a lot of typing and reduce the chance of mistakes.

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,734

    Re: [RESOLVED] else if (uh boy)

    The problem with your code in the OP is you have to many end if statements. The correct structure is
    Code:
    If Something Then
        Do Something
    ElseIf SomethingElse then
        Do Something else
    ElseIf Someother thing then
        Do someother thing
    End If
    Also that End statement should be removed and as a general rule should never be used.

Posting Permissions

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



Click Here to Expand Forum to Full Width