|
-
Jan 29th, 2000, 04:24 AM
#1
Thread Starter
Hyperactive Member
Hi Everyone! I found this code in the archives and can't make it work. It's an evaluation timer.I don't know much about creating files and I get a Path not found error in the Open for Random line.I know the path doesn't exist because I'm trying to create it. Any Help? Here's the code:
------------------- 'declare inside a module
Type Record
FirstDate As Date
MaxDate As Date
Count As Integer
End Type
-------------------
Dim rec As Record
Dim strFile As String
Dim blnSuccess As Boolean
blnSuccess = False
strFile = "c:\windows\desktop\rnd\a.txt"
If (Dir(strFile) = "") Then 'not found then create a new one automatically
'''Here's where I get the error
Open strFile For Random As #1 Len = Len(rec)
rec.Count = 0
rec.FirstDate = Date
rec.MaxDate = Date
Put #1, , rec
blnSuccess = True
Close #1
Else
Open strFile For Random As #1 Len = Len(rec)
Get #1, , rec
Close #1
Open strFile For Random As #1 Len = Len(rec)
If (rec.MaxDate <= Date) Then
rec.MaxDate = Date
If (Date <= DateAdd("d", 30, rec.FirstDate)) Then
If (rec.Count < 9) Then
rec.Count = rec.Count + 1
blnSuccess = True
Else
MsgBox ("Exceeded 10 times")
End If
Else
MsgBox ("Over 30 days already")
End If
Put #1, , rec
Else
MsgBox ("Max date can not be later than today, file has been changed")
End If
Close #1
End If
If (blnSuccess) Then
MsgBox ("Put your main module here")
End If
-
Jan 29th, 2000, 04:27 AM
#2
PowerPoster
Does the path that is in the code exist?
e.g. strFile = "c:\windows\desktop\rnd\a.txt"
If it doesn't, you will get that error. Change it to a txt file that does exist
Good luck,
------------------
- Chris
[email protected]
If it ain't broke - don't fix it 
-
Jan 29th, 2000, 04:29 AM
#3
PowerPoster
Oops, I didnt read your question properly. That method cannot create directories (you presumably already have C:\Windows\Desktop), but you may not have \rnd. You need to use the MkDir function to make the \rnd directory, then create the file.
Hope that helps,
------------------
- Chris
[email protected]
If it ain't broke - don't fix it 
-
Jan 29th, 2000, 04:41 AM
#4
Thread Starter
Hyperactive Member
I understand why I can't create it now but I'm hoping you or someone else might show me an example of the MkDir function that might make it clearer on how to do this.I thought about creating the file to C:// but I wasn't sure If some folks might have a different drive letter. Obviously I'm still a bit new to this stuff.
Thanks,
Joey O.
------------------
-
Jan 29th, 2000, 06:56 AM
#5
PowerPoster
I have dealt with a fair few computers, and all of them have had the hard disk at C:\. You don't get much choice, the BIOS assigns drive letters for bootable devices such as floppy and Hard disk, then Windows assigns left-over drives (such as a CD-ROM) with subsequent drive letters. You can change letters Windows assigns though.
So unless someone has a really obscure BIOS, everything should be cool.
With regard MkDir (finally ), you need to do this
MkDir "C:\Whatever"
Word of warning - MkDir will only make new directories in ones that already exist, e.g If you had a folder called "Whatever" on your hard disk, and you tried this
MkDir "C:\Whatever\Wow\Cool"
it would fail because "Wow" does not exist. However, this would work:
MkDir "C:\Whatever\Wow"
because "Whatever" does exist, and it would make "Wow" in "Whatever". Understand?
To get around this you have to write your own function that will make any directory, anywhere on your hard disk, whether previous ones exist or not.
To do this, copy the following to the Declarations section of a module:
---------------
Public Sub MakeDirectory(Path As String)
Dim strCheck As String
Dim strPath As String
Dim intPosition As Double
Dim intCheckPosition As Double
If Right(Path, 1) <> "\" Then
strPath = Path & "\"
Else
strPath = Path
End If
intPosition = 4
intCheckPosition = 1
Do While intCheckPosition <= Len(strPath)
Do While Right(strCheck, 1) <> "\"
strCheck = Left(strPath, intPosition)
intPosition = intPosition + 1
Loop
strCheck = Left(strCheck, Len(strCheck) - 1)
intPosition = intPosition + 1
If Dir(strCheck, vbDirectory) = "" Then
MkDir strCheck
End If
intCheckPosition = intCheckPosition + 1
Loop
End Sub
---------------
Now, if you want to make a directory anywhere, call the function using this code:
---------------
MakeDirectory "C:\Whatever\Wow\Cool"
---------------
Hope that helps 
Good luck
------------------
- Chris
[email protected]
If it ain't broke - don't fix it 
-
Jan 29th, 2000, 07:03 AM
#6
Thread Starter
Hyperactive Member
Thanks Chris!
I feel like I really learned something today!Thanks for taking the time to help. I hope others can benefit from this posting also.
Joey o
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
|