Click to See Complete Forum and Search --> : Files (still...)
Lorna
July 16th, 2001, 09:18 AM
Me again
1. If I want to programatically create a new file, and it ALREADY Exists - is there a way/function to test for it?
2. How do I limit a user's reply to an Input box - eg must only reply with 2 char?
Thanks again
Ghost308
July 16th, 2001, 09:30 AM
I'm not really certain about your first question so I'll have to skip it for now, but as for your second question regarding the data retreived frm an input box..
I would put some error checking code in after the input box is called.
retval = InputBox "Enter some data: ","Title bar"
If len(retval) > 2 then
InputBox "Invalid Entry, Please try again: ", "Title bar"
End If
Hope that helps!
Iouri
July 16th, 2001, 09:31 AM
4 ways to find if file exists
1.
Private Function FileExistsWithDir(ByVal filename As String)
Dim file_name As String
On Error Resume Next
file_name = Dir$(filename)
FileExistsWithDir = (file_name <> "")
End Function
'----
FileExists = Not (Dir(filespec)="")
'// there! filespec is the filename with full path. If the file exists then
'// FileExits = True, if it does not exist, then FileExists = False.
'// Simple really.
2.
Private Function FileExistsWithOpen(ByVal filename As String)
Dim fnum As String
fnum = FreeFile
On Error GoTo FileDoesntExist
Open filename For Input As fnum
Close fnum
FileExistsWithOpen = True
Exit Function
FileDoesntExist:
FileExistsWithOpen = False
End Function
3,
Private Function FileExistsWithFileLen(ByVal filename As String)
Dim length As Long
On Error GoTo FileDoesntExist
length = FileLen(filename)
FileExistsWithFileLen = True
Exit Function
FileDoesntExist:
FileExistsWithFileLen = False
End Function
4.
Private Function FileExistsWithAttrib(txt)
Dim lRetVal As Long
On Error GoTo FileDoesntExist:
lRetVal = GetAttr(txt)
FileExistsWithAttrib = True
Exit Function
FileDoesntExist:
FileExistsWithAttrib = False
End Function
>>How do I limit a user's reply to an Input box - eg must only reply with 2 char?
You can enter info not to the Inputbox but Text box with Max = 2
Iouri Boutchkine
iouri@hotsheet.com
Cakkie
July 16th, 2001, 09:33 AM
#1 Checking if a file already exists:
dim strFile as string
strFile = Dir("c:\myfile.dat")
If strFile <> "" then
' file exists
else
' file does not exist
end if
#2 Max 2 input
private Sub text1_Change()
If len(Text1.Text) > 2 then
Beep ' sound a beep
Text1.text = Left(Text1.Text,2)
End If
End Sub
Tom Cannaerts
slisse@planetinternet.be
Programming today is a race between software engineers striving to build bigger and better idot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
TH1
July 16th, 2001, 09:33 AM
1:If you are talking about a text file etc then the following will create it if it doesn't exist and append to it if it does
nSifFile = FreeFile
Open "C:/mytest.txt" for binary Access Write lock Read Write as #nSifFile
seek #nSifFile, LOF(nSifFile) + 1
Put #nSifFile, , "th"
If you are talking about a word/excel file you can use GetObject which will generate an error which you can trap if the file doesn't exist and then createObject to create the file
2:Create your own form to mimic the inputbox and then you can restrict the textbox to whatever you like
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.