Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Validation Query

Status
Not open for further replies.

Chr1sUK

Technical User
Jan 14, 2004
121
0
0
GB
Hi, I have a couple of newbie questions for you! Not been doing VB for all that long so please bear with me :)

I am working on a program, which includes file handling.

Currently, I have a drop down menu (combo box) which will show the Name of the person that the record is related to (the name and other details are entered on another form).

Clearly, the file is reading correctly, however I need to make it so that when the name is picked from the drop down menu, the rest of the details show on a label below. How should I go about doing this?

Secondly, I want to make it so that if no file is found, the user is alerted and told that a file must exist. Currently, you get the "file not found" vb error message, and the program exits, which is no good. What code would I need to set up basic file validation just to make sure the file exists?

Thanks!
 
OK quick update...I have actually did the validation with:

"On error goto nofile" and set nofile as a message box.

However, even when a valid file is present, i still get the error message???

Any ideas??
 
OK so I'm guessing your setup probably looks something like this:
Code:
Private Sub LoadFile()

Open "C:\myfile.txt" For Input As #1
'Code
'Code
'Code
Close #1

NoFile:
MsgBox "No FileFound"

End Sub

The problem is Just because you have the "NoFile" label does not mean the code is going to exit the sub there. You Have to tell the program to leave the sub when it reaches that point, your code should look something like this(note the Exit Sub):

Code:
Private Sub LoadFile()

Open "C:\myfile.txt" For Input As #1
'Code
'Code
'Code
Close #1

Exit Sub 'If everything went correctly dont run NoFile stuff


NoFile:
MsgBox "No FileFound"

End Sub

If this was not the problem please post your code so that we can better help you!

By the way if you still need help displaying data from the file just let me know and I will try to help you out with that too.
 

While error handling is something important to have (and should put everywhere) you could actually just check to see if the file exists using the dir function before trying to access it...
[tt]
If Dir(FileToCheckFor) = "" Then
'file not found
Else
'file found
End If
[/tt]

Good Luck

 
Ok here is the code that I have so far:

---------------------------------

Private Sub Form_Load()

lblToday = Date

On Error GoTo NoFile

Const cFile = "c:\temp\films.txt"
'this is the file to be read

Dim Title As String
Dim Distributor As String
Dim Year As String
Dim Length As String

Open cFile For Input As #1
'opens the file for input


Do While Not EOF(1)
Input #1, Title, Distributor, Year, Length
cmbFilm.AddItem Title
Loop
'this loops through until end of file, reading all details.

Close #1

Exit Sub 'If everything went correctly dont run NoFile stuff


NoFile:
MsgBox "No File Found"

End Sub

---------------------

OK so I did the no file as suggested, yet all i get now is "file not found" before I can actually get into this sub!

Another question - how can i make it so that if a directory does not exist, its created, but once created it does not keep trying to create?

Thanks.
 
I am suspecting that you have the option: Break on All errors turned on. This means while in debug if there is an error it will break. To fix this problem go to tools -> options -> click the General Tab and make sure break on all errors is not checked.

But the solution that vb5prgrmr suggested is a much better way to do this!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top