|
-
Jan 14th, 2005, 06:19 PM
#1
Thread Starter
Admodistrator
Saving to a text file:
First off, sorry for buggin you guys with these stupid "noob" posts, thanks for your help-im just a beginner
okay, heres my code:
Code:
Private Sub Command1_Click()
Dim hFile As Long
Dim sFilename As String
sFilename = (App.Path & "\demo.txt")
'obtain the next free file handle from the
'system and and save the text box contents
hFile = FreeFile
Open sFilename For Output As #hFile
Print #hFile, Text1.Text, Text2.Text
Close #hFile
End Sub
works good, but i want it so that if there is something already saved to demo.txt that it will not over write it..
i cant figure out if i should be like
if demo.txt = full then
, Print #hFile, Text1.Text, Text2.Text
hope you understand..thanks for all your help
-
Jan 14th, 2005, 06:34 PM
#2
Addicted Member
Re: Saving to a text file:
you want to check if it is empty or if it has something in it already?
I so use the LOF(SFileName).
Cant remember the code off hand but it might go something like this:
VB Code:
if LOF(SFileName) > 0 then
msgbox "data in file"
else
msgbox "File Empty"
end if
Things fall apart which the center cannot hold...
-
Jan 14th, 2005, 06:49 PM
#3
Re: Saving to a text file:
or
Code:
If Dir(sFilename) = vbNullString Then
'open file and save data
End If
Woof
-
Jan 14th, 2005, 06:53 PM
#4
Re: Saving to a text file:
Code:
Open sFilename For Append As #hFile
Will automatically start writing at the end of your file, leaving the current file informationt intact. You don't have to check to see if it is empty. If you want to overwrite it, just change APPEND back to OPEN
-
Jan 14th, 2005, 08:23 PM
#5
Re: Saving to a text file:
Just a little tip not that it will make a great deal of difference. hFile should be an integer and use FreeFile().
VB Code:
Dim hFile As Integer
Dim sFilename As String
'obtain the next free file handle from the
'system and and save the text box contents
hFile = FreeFile()
Or you could use a CommonDialog and
VB Code:
CommonDialog1.Flags = cdlOFNOverwritePrompt Or cdlOFNPathMustExist
CommonDialog1.ShowSave
Last edited by Keithuk; Jan 14th, 2005 at 08:26 PM.
Keith
I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
-
Jan 15th, 2005, 12:03 AM
#6
Thread Starter
Admodistrator
Re: Saving to a text file:
@ dglienna...THANKS! works good, need to figure out how to make it read the dates from the .txt, and if the date is today make a message box..
-
Jan 15th, 2005, 01:24 AM
#7
Thread Starter
Admodistrator
Re: Saving to a text file:
anyone know how to do that?
ive heard :time: and :date: work, but not here
-
Jan 15th, 2005, 01:36 AM
#8
Re: Saving to a text file:
you would have to open your file for input, read a line or the whole file or whatever is apporpriate, find the component that is the date you want
isolate that and compare it with todays date, using the Date function.
hope this helps pete
-
Jan 15th, 2005, 01:45 AM
#9
Thread Starter
Admodistrator
Re: Saving to a text file:
okay i found this code:
Code:
Private Sub Form_Load()
Dim InptStr As String
Dim CompStr As String
Open (App.Path & "\demo.txt") For Input As #(1)
While Not EOF(1)
Line Input #1, InptStr
If Left(InptStr, Len(CompStr)) = CompStr Then
' Now you have a Line starting with the string you've been looking for
End If
Wend
Close #1
End Sub
now how would i have the textbox appear with, if there is an event,the event name the time and date?
-
Jan 15th, 2005, 01:57 AM
#10
Re: Saving to a text file:
I think you want the MSGBOX function.
answer=msgbox("The data is " & compSTR & " !", vbOK)
look it up. there are quite a few options as to what flags you use, like YesNo, Critical, Ok, Cancel. Whatever you want gets returned, and you can check if the answer is VbYES for instance.
-
Jan 15th, 2005, 01:57 AM
#11
Re: Saving to a text file:
Private Sub Form_Load()
Dim InptStr As String
Dim CompStr As String
Open (App.Path & "\demo.txt") For Input As #(1)
While Not EOF(1)
Line Input #1, InptStr
If Left(InptStr, Len(CompStr)) = CompStr Then
' Now you have a Line starting with the string you've been looking for
End If
Wend
Close #1
End Sub
line input reads 1 line from your file if the date you want is onevery line a the beginning, the left function, will get it, but depending how it is formatted in the file as to the length you need for the left, if the date is elswhere on the line isolating it would be slightly different
you need to post a sample from your file
the compstr would be the date you want to compare to, but as it is a date it also would have to be done differently, again depending how the date is formatted in the file.
to place the information in a textbox you would add text1.text = Inptstr
again this might need to formatted to make some sort of sense to the user
as this is in a loop that goes to the end of the file the textbox would be overwritten each time todays date is found, if it could be in the file more than once, in which case you may need to use an array of text boxes to display all the results
p.
-
Jan 15th, 2005, 02:01 AM
#12
Thread Starter
Admodistrator
Re: Saving to a text file:
okay i meant msgbox, sorry and thankyou, ill look over and get back to you
-
Jan 15th, 2005, 02:06 AM
#13
Thread Starter
Admodistrator
Re: Saving to a text file:
okay, heres an example, the demo.txt it creates posts this:
Code:
2/9/05 2:30 Birthday
and i changed my values according to what you said (or i interpreted),
Code:
Private Sub Form_Load()
Dim InptStr As String
Dim CompStr As String
Open (App.Path & "\demo.txt") For Input As #(1)
While Not EOF(1)
Line Input #(1), InptStr
If Left(InptStr, Len(":Date:")) = (":Date:") Then
answer = MsgBox("The data is " & CompStr & " !", vbOK)
' Now you have a Line starting with the string you've been looking for
End If
Wend
Close #1
End Sub
if thats all okay, then i gotta move onto the messagebox
Last edited by |2eM!x; Jan 15th, 2005 at 02:09 AM.
-
Jan 15th, 2005, 02:23 AM
#14
Re: Saving to a text file:
If Left(InptStr, Len("  ate:")) = ("  ate:") Then
answer = MsgBox("The data is " & CompStr & " !", vbOK)
' Now you have a Line starting with the string you've been looking for
End If
VB Code:
Private Sub Form_Load()
Dim InptStr As String
'Dim CompStr As String 'not used
Dim mydate as Date
Open (App.Path & "\demo.txt") For Input As #(1)
While Not EOF(1)
Line Input #(1), InptStr
mydate = Left(inptstr,Len(Date))
If mydate = Date Then
answer = MsgBox("The data is " & CompStr & " !", vbOK)
' Now you have a Line starting with the string you've been looking for
End If
Wend
Close #1
End Sub
you will have to change the bit about the date
answer= msgbox(Inptstr)
p.
-
Jan 15th, 2005, 02:31 AM
#15
Re: Saving to a text file:
-
Jan 15th, 2005, 02:45 AM
#16
Thread Starter
Admodistrator
Re: Saving to a text file:
okay got to see how it prints the date itself, with the word "now"
printed "1/15/2005 1:40:14 AM"
but i still cant get the darn textbox to popup with the event of the day
this is what im trying
Code:
Open (App.Path & "\demo.txt") For Input As #(1)
While Not EOF(1)
Line Input #(1), InptStr
If Left(InptStr, Len(Now)) = (Now) Then
answer = MsgBox("The data is " & CompStr & " !", vbOK)
' Now you have a Line starting with the string you've been looking for
End If
Wend
Close #1
End Sub
any ideas?
-
Jan 15th, 2005, 02:54 AM
#17
Re: Saving to a text file:
you will have to change the bit about the date
answer= msgbox(Inptstr)
using Now won't give you the correct result because of the way the information is in the file, between the dae and the time is a gap, of unknown characters, probably spaces or tabs, using Date worked for me. if you need to use Now you will have to trim out the characters between the date and the time.
change the msgbox line, so that it displays the information you want
p.
-
Jan 15th, 2005, 02:57 AM
#18
Re: Saving to a text file:
you have to build the date string.
Code:
Str = format(now, "MM") & "/" & format(now,"DD") & "/" & format(now,"YY")
I'd bet that your string won't ever equal the current date and time. just use the date the way that I have it. if you use anything else, you could end up with 1 character strings. I had a filename end up as 1105 instead of 010105.
I save the date in the filename itself.
-
Jan 15th, 2005, 03:01 AM
#19
Thread Starter
Admodistrator
Re: Saving to a text file:
yeah your right, all i wanted was the date to be looked for, the time would be a pain..my friend told me to do split(now)(0) and it worked but you guys probably know better..
heres all my forms check them out and tell me whats wrong, im really tired..
http://s18.yousendit.com/d.aspx?id=0...801F0Y4K4ID0GB
-
Jan 15th, 2005, 03:03 AM
#20
Re: Saving to a text file:
Code:
Dim arr() As String
If instr(InptStr, " ") Then
Do
InptStr = Replace(InptStr, " ", " ")
Loop Until instr(InptStr, " ")= 0
endif
arr() = split(InptStr, " ")
will give you arr(0), arr(1), and arr(2) for date, time, and event.
that will help, no doubt.
EDIT: You link does not work.
-
Jan 15th, 2005, 03:10 AM
#21
Re: Saving to a text file:
david,
i tested it, pasting his sample from file to a str variable, then changed the numbers to todays date.
i actually didn't expect it to work right away as my local date is dd/mm/yy
and his file though it didn't say i took to be mm/dd/yy, though as todays date 15th is above 12 date sorts it out if it had been the 10th it may have been a problem. so yes using you method will fix that problem but you will need to give him a more complete example
p.
-
Jan 15th, 2005, 03:16 AM
#22
Re: Saving to a text file:
Just switch DD with MM and it should be fine.
Are you talking about removing the spaces?
-
Jan 15th, 2005, 03:37 AM
#23
Re: Saving to a text file:
david,
our posts keep crossing
i didn't see your previous post when i posted
as he only wants the date, only need to read the first part.
shouldn't have to change the way the date is formatted, whichever way it is done it should work for all locales, i have struggled with this before, and is another of those hard to test things if all your machines have the same locale set.
p.
-
Jan 15th, 2005, 12:45 PM
#24
Thread Starter
Admodistrator
Re: Saving to a text file:
Im So Confused!
i dont understand what you guys mean at all
-
Jan 15th, 2005, 02:31 PM
#25
Re: Saving to a text file:
post what you have, and describe the problem
-
Jan 15th, 2005, 03:31 PM
#26
Thread Starter
Admodistrator
Re: Saving to a text file:
Code:
Private Sub Command1_Click()
Dim hFile As Long
Dim sFilename As String
sFilename = (App.Path & "\demo.txt")
'obtain the next free file handle from the
'system and and save the text box contents
hFile = FreeFile
Open sFilename For Append As #hFile
Print #hFile, Text1.Text, Text2.Text, Text3.Text
Close #hFile
End Sub
Private Sub Command2_click()
Dim hFile As Long
Dim sFilename As String
sFilename = (App.Path & "\demo.txt")
If vbYes = MsgBox("Are you SURE you want to clear all??", _
vbQuestion + vbYesNo, _
"Clear all?") Then
'obtain the next free file handle from the
'system and and save the text box contents
hFile = FreeFile
Open sFilename For Output As #hFile
Print #hFile, " "
Close #hFile
End If
End Sub
Private Sub Form_Load()
Dim InptStr As String
Dim CompStr As String
Dim strdate As String
strdate = Split(Now)(0)
If strdate = Now Then
MsgBox (Now("There is an event scheduled"))
Open (App.Path & "\demo.txt") For Input As #(1)
While Not EOF(1)
Line Input #(1), InptStr
If Left(InptStr, Len(Now)) = (Now) Then
answer = MsgBox("The data is " & CompStr & " !", vbOK)
' Now you have a Line starting with the string you've been looking for
End If
Wend
Close #1
End If
End Sub
this is my entire form 1, there are 3 textbox's,and 2 buttons
everything works good except i cant get the program to read thru demo.text (on my desktop) and make a msgbox "EVENT FOUND: <"Event Name"/Now>,
even though it writes to the demo.text, it cant read it..
im really not good at this, so ' notes would be really appreciated
-
Jan 15th, 2005, 04:52 PM
#27
Re: Saving to a text file:
Code:
Option Explicit
Private Sub Command1_Click()
Dim arr() As String
Dim Str As String
Dim hFile As Long
Dim sFilename As String
' Assuming that the first textbox is the Date "MM/DD/YY" but it
' doesn't really matter which way it is, As Long As it matches.
' You could also just Print it out without checking the date.
sFilename = (App.Path & "\demo.txt")
hFile = FreeFile
Open sFilename For Append As #hFile
arr() = Split(Text1.Text, "/")
Str = arr(0) & "/" & arr(1) & _
"/" & arr(2)
Print #hFile, Str, Text2.Text, Text3.Text
Close #hFile
End Sub
Private Sub Command2_click()
Dim hFile As Long
Dim sFilename As String
sFilename = (App.Path & "\demo.txt")
If vbYes = MsgBox("Are you SURE you want To clear all??", _
vbQuestion + vbYesNo, _
"Clear all?") Then
'obtain the Next free file handle from the
'system And and save the text box contents
hFile = FreeFile
Kill sFilename
End If
End Sub
Private Sub Form_Load()
Dim answer As String
Dim InptStr As String
Dim CompStr As String
Dim strdate As String
Dim Str As String
Dim ff As Integer
Dim arr() As String
On Error GoTo FileError
ff = FreeFile()
Open (App.Path & "\demo.txt") For Input As #(ff)
On Error GoTo 0
Do While Not EOF(ff)
Line Input #(ff), InptStr
If InStr(InptStr, " ") Then
Do
InptStr = Replace(InptStr, " ", " ")
Loop Until InStr(InptStr, " ") = 0
End If
arr() = Split(InptStr, " ")
Str = Format(Now, "MM") & "/" & Format(Now, "DD") & _
"/" & Format(Now, "YY")
If arr(0) = Str Then
answer = MsgBox("The data is " & arr(2) & " !", vbOK)
' Now you have a Line starting with the String you've been looking For
End If
If arr(0) = Str Then
MsgBox ("There is an Event scheduled")
End If
Loop
Close #ff
Exit Sub
FileError:
MsgBox "File Not found"
On Error GoTo 0
End Sub
should get you started on the right track.
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
|