|
-
Jun 15th, 2010, 10:37 AM
#1
Thread Starter
New Member
Creating a Text File
Hi, just found this forum!! looks to be alot of great info here! looking forward to reading thru and learning more!
So i've been writing a program the last few days. i can create a text file and write to it and stuff. but i'm trying to write an application for logging data. I need to test these boxes in work (power distribution boxes) and i want to write a program to log the data from the test results. I can create a text file but im having trouble with getting it named properly. i want the text file to be named like this
Type, Date time, serial number.
type would be say...K5
for date and time i'm using the "Now" function.
serial number would be something like SN001
so the text file should be saved like this,
K5, 15/06/10 4:25 PM SN001
I appologise if this code looks really messy! i usually try to keep it tidy and readable but i have been cuttin and pastin for the last few hours now so it could prob be tidied up!
Code:
Private Sub Form_Load()
Dim filelocation As String
Dim FilePath As String
Dim Box As String
Dim TestDate As Date
FilePath = "C:\xyz"
End Sub
___________________________________
Private Sub Option1_Click()
Dim FileName As String
Box = "K5"
TestDate = Now
FilePath = "C:\xyz
FileName = Box + ", " + TestDate + ".txt"
filelocation = FilePath + FileName
Open filelocation For Append As #1
Print #1, Text1.Text
Close #1
End Sub
Thats how i tried to write it. i tried changing the code a lot and results were as follows, none were what i was lookin for! I either ended up with these or error 13!
K5, val(TestDate)Date.txt
K5, val(TestDate)Date.txt
K5, 150610.txt
K5, .txt
and a reminder again of what i am aiming for
K5, 15/06/10 4:25 PM SN001
Thanks for those who take the time to read, its very much appreciated! any suggestions are welcome! if any more info is needed let me know!
thanks and Regards,
Dean
-
Jun 15th, 2010, 11:00 AM
#2
Re: Creating a Text File
Slash and Colon are reserved characters so you cannot use those (plus some others). Format your date_time before using it.
Perhaps this will work for you:
Code:
TestDate = Format(Now, mm-dd-yyyy hh-nn-ss")
For more available formats refer to VB6 documentation. If you don't have it installed link to MSDN on-line is in my signature.
-
Jun 15th, 2010, 11:25 AM
#3
Re: Creating a Text File
Welcome to the Forums 
 Originally Posted by DeanM
and a reminder again of what i am aiming for
K5, 15/06/10 4:25 PM SN001
From this, the best you can wind up with is something like this: KS 06152010 1222 PM SN001.txt
Although you can use a comma in a file name, I would recommend against it.
Last edited by Hack; Jun 15th, 2010 at 11:28 AM.
-
Jun 15th, 2010, 11:45 AM
#4
Re: Creating a Text File
Dean
The full list of reserved characters is \ / : * ? " < > |
These cannot be used in a filename
Hence, why you cannot do .... K5, 15/06/10 4:25 PM SN001.txt
you could use hyphen ........... K5, 15-06-10 4-25 PM SN001.txt
or, sim to Hack's suggestion ... K5, 15062010 425 PM SN001.txt
Hack
It seems that comma is actually allowed!
I just tried it.
Spoo
-
Jun 15th, 2010, 12:16 PM
#5
Thread Starter
New Member
Re: Creating a Text File
Hi guys,
Thanks for the responses! much appreciated! I just realised on the way home that those characters wouldnt be allowed! that was about 2hrs wasted! dont know why i didnt realise!
@Hack, yeah your probably right. maybe it would look tidier without the comma.
@RhinoBull, thanks a million! that looks to be exactly what i need i'l give that a try when i get back in tomorrow! should do the trick nicely!!
Thanks again guys!
-
Jun 28th, 2010, 07:13 AM
#6
Thread Starter
New Member
Re: Creating a Text File
Hi again!
I have another issue i'm trying to solve right now. should i start a new thread for it or just ask again here?
I have a development board that uses USB and I'm trying to receive a message from it. I can receive messages but it isn't running very smoothly!
I press a button on the MCU and it prints "Button Pressed". i want that to appear in the VB gui as say either a text box or label. I know the code is this,
vb Code:
Text1.Text = MSComm1.Input
I put it into MSComm1_OnComm but nothing happens. it only comes up when i put it in a command or something.
vb Code:
Private Sub Command1_Click() MSComm1.Output = "1" Beep 5000, 50 Text1.Text = MSComm1.Input End Sub
The code above sends a "1" thru the comm port (virtual comm through usb). The MCU recieves that and switches a relay on and off a couple times with a little delay time in between. then sends back "Test Complete".
The textbox only shows that message the second time its pressed. cos there is nothing in the comm input the first time. so i want to try display the message when it is sent. maybe by activating when taht happens. also, what would be a good way of writing the code so that the Form waits for that message? for example, it sends the "1" and then waits for the command to be returned on the comm port? that way i could write it so that it sends the command, receives the message when test has finished and then loads a form and asks did the test run ok? click yes or no etc.
Thanks for reading,
Regards,
Dean
Last edited by DeanM; Jun 28th, 2010 at 07:18 AM.
-
Jun 28th, 2010, 08:59 AM
#7
Re: Creating a Text File
Dean
Definitely start another thread.
You should be able to cut and paste most of this one there.
Spoo
-
Jun 28th, 2010, 09:17 AM
#8
Thread Starter
New Member
Re: Creating a Text File
cool thanks! think i might have it sorted but will do so anyway and see if it looks ok to others!
Thanks Spoo!
-
Jun 29th, 2010, 01:09 AM
#9
Hyperactive Member
Re: Creating a Text File
 Originally Posted by DeanM
TestDate = Now
FilePath = "C:\xyz"
FileName = Box + ", " + TestDate + ".txt"
filelocation = FilePath + FileName
Open filelocation For Append As #1
Print #1, Text1.Text
Close #1
[/CODE]
Like Rhinobull said, format the date before using it. And I assume you want the complete one. I think this should do it:
Code:
TestDate = Format(Now, "Long Date")
And I believe this one would raise an error:
Code:
Open filelocation For Append As #1
Print #1, Text1.Text
Close #1
It is because of the line filelocation = FilePath + FileName. <Look here.. FilePath = "C:\xyz", FileName = Box + ", " + TestDate + ".txt"
Supposing that Box is "K5" and TestDate is "15-06-10 4-25 PM" (as Spoo recommended) then FileName would be "K5, 15-06-10 4-25 PM.txt"
Now FilePath + FileName would be "C:\xyzK5, 15-06-10 4-25 PM.txt" which will result to error or to an unexpected path and file name.
Maybe you mean
Code:
filelocation = FilePath + "\" + FileName
Lastly, I would recommend using "&" instead of "+" in connecting strings.
Last edited by doctrin13th; Jun 30th, 2010 at 12:01 AM.
-
Jul 6th, 2010, 03:54 AM
#10
Thread Starter
New Member
Re: Creating a Text File
 Originally Posted by doctrin13th
Like Rhinobull said, format the date before using it. And I assume you want the complete one. I think this should do it:
Code:
TestDate = Format(Now, "Long Date")
And I believe this one would raise an error:
Code:
Open filelocation For Append As #1
Print #1, Text1.Text
Close #1
It is because of the line filelocation = FilePath + FileName. <Look here.. FilePath = "C:\xyz", FileName = Box + ", " + TestDate + ".txt"
Supposing that Box is "K5" and TestDate is "15-06-10 4-25 PM" (as Spoo recommended) then FileName would be "K5, 15-06-10 4-25 PM.txt"
Now FilePath + FileName would be "C:\xyzK5, 15-06-10 4-25 PM.txt" which will result to error or to an unexpected path and file name.
Maybe you mean
Code:
filelocation = FilePath + "\" + FileName
Lastly, I would recommend using "&" instead of "+" in connecting strings.
Yeah FilePath = C:\xyz was just as an example! the real path is to a folder in my documents and was saved with the \ at the end of it. so there is no need to add the + "\" cos its already at the end of FilePath. so o suppose i should've made it clearer by typing "C:\xyz\"
Thanks for the advice!
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
|