Results 1 to 23 of 23

Thread: How do you create multiple receipts?

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2018
    Posts
    4

    Question How do you create multiple receipts?

    I am creating a program which will take in the name, total cost and delivery code of a users purchase. I want to be able to save a receipt to a specified folder on my computer, I know how to save 1 file but I do not know how to not have the next receipt overwrite the original one and so on.

    This is the code I currently have:

    Code:
    Dim filename As String
    filename = "D:\Programming\Yearbook Program\Receipts\receipts.txt"
    FileOpen(1, filename, OpenMode.Output)
    PrintLine(1, "The order under the name of " & name2)
    PrintLine(1, "The total comes to £" & cost2)
    If DCode3 = "D" Then
        PrintLine(1, "You will get the Yearbooks will delivered to your house")
    Else
        PrintLine(1, "You have chosen to collect the yearbooks from the school office")
    End If
    FileClose(1)
    Thanks in advance for those that can help me!
    Last edited by dday9; Oct 12th, 2018 at 02:38 PM. Reason: Added Code Tags

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: How do you create multiple receipts?

    This looks like legacy Visual Basic code, are you sure that you're using Visual Basic .NET (2005 on higher)?

    If you're positive that you're using .NET, use the IO.File.WriteAllLines method. Aside from that, simply change the name of the text file if the file already exists. Take a look at this example:
    Code:
    'Declare a static variable to keep track of the current file iteration
    Static iteration As Integer = 0
    
    'Get the file name by building the path
    Dim filename As String = IO.Path.Combine({"D:", "Programming", "Yearbook Program", "Receipts", $"receipts{iteration}.txt"})
    
    'Write the desired lines to the text file
    IO.File.WriteAllLines(
        filename, {
        "The order under the name of " & name2,
        "The total comes to £" & cost2,
        If(DCode3 = "D",
            "You will get the Yearbooks will delivered to your house",
            "You have chosen to collect the yearbooks from the school office"
        )}
    )
    
    'Increment the iteration
    iteration += 1
    Last edited by dday9; Oct 12th, 2018 at 03:40 PM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How do you create multiple receipts?

    Using DDays code, you increment iteration by 1 after writing a receipt, so you'd have sequentially numbered receipts and no overwriting...

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: How do you create multiple receipts?

    As for your original code, using Append, rather than Output would append to the file, rather than overwrite it.
    FileOpen(1, filename, OpenMode.Append)

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: How do you create multiple receipts?

    As for your original code, using Append, rather than Output would append to the file, rather than overwrite it.
    FileOpen(1, filename, OpenMode.Append)

  6. #6
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,042

    Re: How do you create multiple receipts?

    Quote Originally Posted by passel View Post
    As for your original code, using Append, rather than Output would append to the file, rather than overwrite it.
    FileOpen(1, filename, OpenMode.Append)
    did you Append this Post #4 and #5 ?

    only jokeing, wonder when this -double posting- is going to be fixed.

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: How do you create multiple receipts?

    Quote Originally Posted by ChrisE View Post
    wonder when this -double posting- is going to be fixed.
    We're hoping the bug causing it will be fixed soon... it's rather annoying for us moderators, as we spend a lot of time deleting the dupes.

    Unfortunately we have to leave fixing the bug to the technical people behind the scenes, but one of the moderators did give them some good info to help with fixing it.

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: How do you create multiple receipts?

    I usually have been able to avoid the double posting. I'm a bit surprised that one slipped through. I'm not sure how I messed up my standard practice to prevent double posting.
    The post times are well more than 30 seconds apart, so I'm thinking when I hit the back arrow to return to an earlier page after some interval I must have retriggered the post, so it wasn't the "classic" double posting that we've been enjoying lately.

  9. #9
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How do you create multiple receipts?

    Quote Originally Posted by si_the_geek View Post
    We're hoping the bug causing it will be fixed soon... it's rather annoying for us moderators, as we spend a lot of time deleting the dupes.

    Unfortunately we have to leave fixing the bug to the technical people behind the scenes, but one of the moderators did give them some good info to help with fixing it.
    Interestingly enough when I post it never double posts. It almost always gives me a message that indicates it tried to double post but the second post does not go through. I am using the latest Firefox under Windows 7 SP1. Don't know if that may be a hint as to the source of the issue or not.

    The message I get says something like "you must wait at least 30 seconds between posts"

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How do you create multiple receipts?

    Quote Originally Posted by DataMiser View Post
    Interestingly enough when I post it never double posts. It almost always gives me a message that indicates it tried to double post but the second post does not go through. I am using the latest Firefox under Windows 7 SP1. Don't know if that may be a hint as to the source of the issue or not.

    The message I get says something like "you must wait at least 30 seconds between posts"
    That's the same for all but moderators, who don't have to wait 30 seconds between posts like the rest of us. Most experienced users realise that, when they get that message, their post actually has been submitted and they don't post again. Many new users don't think too hard about the situation and just submit again when the 30 seconds is up.

  11. #11

    Thread Starter
    New Member
    Join Date
    Oct 2018
    Posts
    4

    Re: How do you create multiple receipts?

    Quote Originally Posted by passel View Post
    As for your original code, using Append, rather than Output would append to the file, rather than overwrite it.
    FileOpen(1, filename, OpenMode.Append)
    This works but would there be anyway to create another file?

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How do you create multiple receipts?

    Quote Originally Posted by CraigWright View Post
    This works but would there be anyway to create another file?
    You're missing the point, which is to ditch all of your code from your original post as there have been some changes since that code was relevant 20 years ago...

  13. #13
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,042

    Re: How do you create multiple receipts?

    Quote Originally Posted by CraigWright View Post
    This works but would there be anyway to create another file?
    take the advice .paul has gave you


    here a sample to get you started...
    I hardcoded the Filename, you can exchange it to a Textbox for the Filename

    Code:
    Imports System.IO
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            TextBox1.Text = "xyz 1"
            TextBox2.Text = Now
            TextBox3.Text = "whatever"
            'etc...
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim path As String = "E:\"
            Dim Filename As String = "LogF.txt" 'you can use a Textbox for Filename
            Path = String.Concat(Path, Filename)
            Call SaveFileForAppend(path)
            Process.Start(path)
        End Sub
    
        Private Sub SaveFileForAppend(ByVal Filename As String)
            Dim sw As System.IO.StreamWriter
    
            Try
                'add Text to the File E:\LogF.txt
                If System.IO.File.Exists(Filename) Then
                    sw = System.IO.File.AppendText(Filename)
    
                Else
                    'if E:\LogF.txt does not exist then
                    'create it and write to a new File
                    sw = System.IO.File.CreateText(Filename)
                End If
                With sw
                    .Write(TextBox1.Text & System.Environment.NewLine)
                    .Write(TextBox2.Text & System.Environment.NewLine)
                    .Write(TextBox3.Text & System.Environment.NewLine)
                    .Flush()
                    .Close()
                End With
            Catch ex As IOException
                MessageBox.Show(ex.Message.ToString())
            End Try
        End Sub
    End Class
    depending what you want Append or write to a new File

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  14. #14
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,042

    Re: How do you create multiple receipts?

    Quote Originally Posted by si_the_geek View Post
    We're hoping the bug causing it will be fixed soon... it's rather annoying for us moderators, as we spend a lot of time deleting the dupes.

    Unfortunately we have to leave fixing the bug to the technical people behind the scenes, but one of the moderators did give them some good info to help with fixing it.
    that -moderator- should get a pizza every Day from the -technical people- until it is fixed.

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  15. #15
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: How do you create multiple receipts?

    I guess I misunderstood your first post.
    I guess you don't want to append, you want to create a separate file for each receipt.

    To do that you need to change the file name each time. Now you have to decide how you want to generate that file name.
    Think about that and give us an idea of what you would like the names of the files to be, i.e. just an incremented number, or perhaps the name and an incremented number, or just the name, and appending receipts within the file for a given name.

  16. #16

    Thread Starter
    New Member
    Join Date
    Oct 2018
    Posts
    4

    Re: How do you create multiple receipts?

    Quote Originally Posted by passel View Post
    I guess I misunderstood your first post.
    I guess you don't want to append, you want to create a separate file for each receipt.

    To do that you need to change the file name each time. Now you have to decide how you want to generate that file name.
    Think about that and give us an idea of what you would like the names of the files to be, i.e. just an incremented number, or perhaps the name and an incremented number, or just the name, and appending receipts within the file for a given name.
    Yeah so basically just something like: receipt1, receipt2, receipt3. Or something like that

  17. #17
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: How do you create multiple receipts?

    Quote Originally Posted by CraigWright View Post
    Yeah so basically just something like: receipt1, receipt2, receipt3. Or something like that
    Yeah, so basically follow my suggestion back in post #2.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  18. #18
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: How do you create multiple receipts?

    The drawback to post #2 is that it only manages the increment for one run, which is a simple example.
    You'll either need something that can scan the directory at startup to figure out the last number used, or I think I would just track the number in another file to save a bit on startup if you have a large number of receipt files in the directory.
    Since you already now how to write and read to a single file, using a file to store the number whenever you increment it should be easy enough for you to do, as well as reading the file to get the number when you start the program up.

    You can then build on dday9's example to modify the file name each receipt save.

  19. #19
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How do you create multiple receipts?

    I agree with what passel said except i'd use a My.Settings variable instead of an external file...

  20. #20
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: How do you create multiple receipts?

    My.Settings was my first thought, but If you have multiple users then each would have their own settings, so may clobber files depending on where the files are.
    Also, using My.Settings will be a different settings file if you move the executable to a different directory, or if you run the program from the IDE vs running outside the IDE.

    Keeping the value in a co-located file with the rest of the files I thought may be best for those reasons, but it is your choice. I do use My.Settings quite a bit, knowing that running in the IDE will not be the same saved data as when running outside the IDE, so deal with that.
    Last edited by passel; Oct 16th, 2018 at 01:28 PM.

  21. #21

    Thread Starter
    New Member
    Join Date
    Oct 2018
    Posts
    4

    Re: How do you create multiple receipts?

    Quote Originally Posted by dday9 View Post
    This looks like legacy Visual Basic code, are you sure that you're using Visual Basic .NET (2005 on higher)?

    If you're positive that you're using .NET, use the IO.File.WriteAllLines method. Aside from that, simply change the name of the text file if the file already exists. Take a look at this example:
    Code:
    'Declare a static variable to keep track of the current file iteration
    Static iteration As Integer = 0
    
    'Get the file name by building the path
    Dim filename As String = IO.Path.Combine({"D:", "Programming", "Yearbook Program", "Receipts", $"receipts{iteration}.txt"})
    
    'Write the desired lines to the text file
    IO.File.WriteAllLines(
        filename, {
        "The order under the name of " & name2,
        "The total comes to £" & cost2,
        If(DCode3 = "D",
            "You will get the Yearbooks will delivered to your house",
            "You have chosen to collect the yearbooks from the school office"
        )}
    )
    
    'Increment the iteration
    iteration += 1
    With this I get two errors which I can't figure out how to fix.

    The $ is said to not be a valid character and if changed to & the original error is replace by 'expression expected'

    'filename' which I have underlined and emboldened is said to not be declared. I will really appreciate it if you could help fix these errors.


    'Declare a static variable to keep track of the current file iteration
    Static iteration As Integer = 0

    'Get the file name by building the path
    Dim filename As String = IO.Path.Combine({"D:", "Programming", "Yearbook Program", "Receipts", $"receipts{iteration}.txt"})

    'Write the desired lines to the text file
    IO.File.WriteAllLines(
    filename, {
    "The order under the name of " & name2,
    "The total comes to £" & cost2,
    If(DCode3 = "D",
    "You will get the Yearbooks will delivered to your house",
    "You have chosen to collect the yearbooks from the school office"
    )}
    )

    'Increment the iteration
    iteration += 1

  22. #22
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: How do you create multiple receipts?

    The dollar sign symbol only became available in Visual Basic 14 and higher, see String Interpolation. Since you don't have that capability, just use the traditional version of concatenating Strings by using the & symbol:
    Code:
    "receipts" & iteration.ToString() & ".txt"
    The reason for the error on the filename variable is because of the original String Interpolation issue; the filename variable is erroring out before being declared. Once you change it to utilize the & symbol, it will fix the filename error.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  23. #23
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: How do you create multiple receipts?

    Quote Originally Posted by ChrisE View Post
    that -moderator- should get a pizza every Day from the -technical people- until it is fixed.

    regards
    Chris
    boudin would be more appropriate
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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