-
Oct 12th, 2018, 02:31 PM
#1
Thread Starter
New Member
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
-
Oct 12th, 2018, 02:48 PM
#2
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.
-
Oct 12th, 2018, 03:42 PM
#3
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 13th, 2018, 01:38 AM
#4
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)
-
Oct 13th, 2018, 01:40 AM
#5
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)
-
Oct 13th, 2018, 01:48 AM
#6
Re: How do you create multiple receipts?
Originally Posted by passel
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.
-
Oct 13th, 2018, 06:20 AM
#7
Re: How do you create multiple receipts?
Originally Posted by ChrisE
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.
-
Oct 13th, 2018, 04:24 PM
#8
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.
-
Oct 13th, 2018, 07:34 PM
#9
Re: How do you create multiple receipts?
Originally Posted by si_the_geek
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"
-
Oct 13th, 2018, 09:43 PM
#10
Re: How do you create multiple receipts?
Originally Posted by DataMiser
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.
-
Oct 14th, 2018, 06:22 AM
#11
Thread Starter
New Member
Re: How do you create multiple receipts?
Originally Posted by passel
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?
-
Oct 14th, 2018, 06:52 AM
#12
Re: How do you create multiple receipts?
Originally Posted by CraigWright
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 14th, 2018, 07:38 AM
#13
Re: How do you create multiple receipts?
Originally Posted by CraigWright
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.
-
Oct 14th, 2018, 07:46 AM
#14
Re: How do you create multiple receipts?
Originally Posted by si_the_geek
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.
-
Oct 14th, 2018, 11:20 AM
#15
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.
-
Oct 15th, 2018, 04:35 AM
#16
Thread Starter
New Member
Re: How do you create multiple receipts?
Originally Posted by passel
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
-
Oct 15th, 2018, 08:16 AM
#17
Re: How do you create multiple receipts?
Originally Posted by CraigWright
Yeah so basically just something like: receipt1, receipt2, receipt3. Or something like that
Yeah, so basically follow my suggestion back in post #2.
-
Oct 15th, 2018, 04:32 PM
#18
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.
-
Oct 15th, 2018, 04:46 PM
#19
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 16th, 2018, 09:30 AM
#20
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.
-
Oct 16th, 2018, 10:22 AM
#21
Thread Starter
New Member
Re: How do you create multiple receipts?
Originally Posted by dday9
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
-
Oct 16th, 2018, 10:34 AM
#22
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.
-
Oct 16th, 2018, 10:36 AM
#23
Re: How do you create multiple receipts?
Originally Posted by ChrisE
that -moderator- should get a pizza every Day from the -technical people- until it is fixed.
regards
Chris
boudin would be more appropriate
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|