Click to See Complete Forum and Search --> : Dir1_Change() Error


Alex
November 5th, 1998, 04:09 PM
Anyone know whats wrong with my code? I want the user to change the current dir, but I want want him/her to be able to choose C:\ because I want them to use sub directories. Thanks for your help....


Private Sub Dir1_Change()


On Error GoTo DriveHandler

GoTo Line1


Line1:

ChDir Dir1.Path

GoTo Line2

Line2:

If Dir1.Path = "C:\" Then

GoTo Line1

Else

GoTo Line3

Line3:

Form1.lblDir.Caption = Dir1.Path

GoTo Line4

Line4:

Unload Me


DriveHandler:

Dir1.Path = ""

Exit Sub

End Sub

Crazy D
November 6th, 1998, 02:45 AM
First of all, try not to use the goto statement and the labels too much. It's quite uncommon in 'real world applications', and it makes your code quite hard to read.

If I understand what you want to do, you want to the user to select any folder except the root of a drive?

Guess you can't control that in the dir-change event, becasue that event fires when the user selectes another dir, or if you do that in code. But it won't work.

What happens is this:

The user selects the root of the c drive. The if statement is true, so you call again ChDir. But the dir hasn't changed! So I think this will end up in an endless loop.

If I may give you a suggestion, get rid of the code in the dir-change, except where you set the caption of the label.

I assume you have something like an Ok button, which must be pressed to 'apply' the selected path. Just check in the dir-change if the selected folder is a subdirectory (never check just for C:\, or you will not allow them to select another harddrive (since most harddrive's have partitions, it's quite common that D:\ is also a local harddrive) or if it's the root. If it's the root, disable the button, else enable it.

Hope it helps

Crazy D