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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a dummy file, and connecting a check box to a command button 1

Status
Not open for further replies.

ntauzin

MIS
May 24, 2002
11
US
OK, I am teaching myself VB, I am not really much of a programmer and so my apologies if this is a less than intelligent question, but here goes...

I am writing an app to delete files. There are several directories that contain the files. I want to give the user the option of deleting files from any or all directories, so I have check boxes (4 of them) with captions like "Delete Files From Dir A", "Delete Files From Dir B", etc.

Now, what I want to set up is a command button after the check boxes that will perform the deletions ( I am using the Kill statement), according to the selected check boxes.

How do I make the command button check the input of the check boxes before deleting files?

Also, in my testing I get a runtime -53 error if there are no files in the dir, so should I first (after clicking the command button) create a dummy text file in each dir, so it can process the Kill statement?

Thanks in advance for your help!
 
insert...


if checkbox1.value=1 then
kill "dir1"
end if

if checkbox2.value=1 then
kill "dir2"
end if


and so on....
the code can get dirtier if needed if this isnt enough let me know...
 
or you could start an array.... and the use case select.. soemhtign like this...


For i = 0 To 3
If Check1(i).Value = 1 Then
Select Case i
Case 0
Label1 = Label1 + "1"
Case 1
Label1 = Label1 + "2"
Case 2
Label1 = Label1 + "3"
Case 3
Label1 = Label1 + "4"
End Select
End If
Next i


cept chance the label1 = crap to the command you want to execute...
 
Great! That works perfectly! I tried your first suggestion. The array thing makes me dizzy.

New question: I want to use 3 forms in my project, and have a "Next" command button on the first 2, then a "Delete" button on the third form.

What is the code to have the command button (the Next) close the current form, open the next one and remember any selections that were made?

 
well you could have it just HIDE the forms... im not sure if it would hold the info still... give me an example of whats on the forms.. or more or less what will be done in the end... with the info..

either way...


to hide form

'in first next button
form1.visible = false
form2.show

'in second one
form2.visible = false
form3.show

for delete... depends how the information is being held...



then to pass the information you could use

'this being on the third form
text1 = form1.text1

.... you could also pass all the data from form one.. to form 2(have it hidden) then close form one.. and same with form 2 to form 3.... give me more info to work with and i can come up with a deffinate solution but what im saying will work just as well :) arrays might make doing this easyer.. ive accually never worked with arrays much.. that exaple i gave you was the first one i ever really made with use.. and now im finding all tons of uses for them hehe its funny how you think you dont know or understand somehtign untill you try it out... anyways there you go gimme more info and ill be glad to help. in the begining man created code.
in the end code will create man.
clones are coming only matter of time.
examples?
 
if you would like explanation on the array thing i dont mind explaining what i did that anf how that code will cut down on alot of coding if you have alot of work to do. either way umm yeah hehe :) in the begining man created code.
in the end code will create man.
clones are coming only matter of time.
examples?
 
I would slightly change this code:

if checkbox1.value=1 then
kill "dir1"
end if

To this:

if checkbox1.value=vbChecked then
kill "dir1"
end if

It makes it a little more readable to use Microsoft's ready-made constant so that you don't have to remember that the number 1 represents the value of a checked checkbox.

Also, a dummy file should kill that error, and such a sequential access file would not slow the program very much. Another option would be to put "On Error Resume Next" at the beginning of your subroutine. That would do the trick, but here's a caveat. It would make your program jump over ANY errors, which could result in a buggy program and your not knowing why it's buggy. If you're really sure you've zapped all the bugs other than that -53 one, you can use that. Or you could rem out the "On Error Resume Next" when debugging.

One thought I had was to make your program ignore only the -53 one. I tried this:

If Err.Number = -53 Then
Resume Next
End If

For some reason it didn't work. Maybe someone else here has an idea why. I simulated the error with "Err.Raise (-53)". I also tried this:

If Err.Number = -53 Then GoTo JumpOver

Then you presumably would put "JumpOver:" at the spot just after the error-causing code. Again, I ran into problems. I'm not sure what I'm doing wrong. The idea, anyway, is to have your program skip over an error if it's a specific type. That way other errors aren't hidden from you. Anyone have any ideas?

I wasn't sure if you were asking how to make a dummy file. Just in case you were, here's how:

Const strDummy As String = "This is a dummy file."

Open App.Path & "\dummyfile.dat" For Append As #1
Write #1, strDummy
Close #1
 
ok for this... just insert a file box... and have it set to invisible... and change the directorys to what you want...

Private Sub Command1_Click()
For i = 0 To 3
If Check1(i).Value = vbChecked Then
Select Case i
Case 0
File1.Path = "c:\test a"
If File1.ListCount = 0 Then
MsgBox "No Files Deleted From: " & File1.Path
Else
Kill "c:\test a\*.*"
MsgBox File1.ListCount & " files deleted from '" & File1.Path & "'"
End If
Case 1
File1.Path = "c:\test b"
If File1.ListCount = 0 Then
MsgBox "No Files Deleted From: " & File1.Path
Else
Kill "c:\test b\*.*"
MsgBox File1.ListCount & " files deleted from '" & File1.Path & "'"
End If
Case 2
File1.Path = "c:\test c"
If File1.ListCount = 0 Then
MsgBox "No Files Deleted From: " & File1.Path
Else
Kill "c:\test c\*.*"
MsgBox File1.ListCount & " files deleted from '" & File1.Path & "'"
End If
Case 3
File1.Path = "c:\test d"
If File1.ListCount = 0 Then
MsgBox "No Files Deleted From: " & File1.Path
Else
Kill "c:\test d\*.*"
MsgBox File1.ListCount & " files deleted from '" & File1.Path & "'"
End If
End Select
End If
Next i

End Sub in the begining man created code.
in the end code will create man.
clones are coming only matter of time.
examples?
 
Private Sub Command2_Click()
MkDir "c:\test a"
MkDir "c:\test b"
MkDir "c:\test c"
MkDir "c:\test d"
End Sub


put that in another command button... to test out the code.... if you wish hehe.. the only thing is... the the directory isnt there it will error... but i assume you will have them premade..if you want error checking for that also let me know ill see what i can scrape up.. in the begining man created code.
in the end code will create man.
clones are coming only matter of time.
examples?
 
ok here it is... with full error check... i even have this with an error in it.... the last command... case 4... so you can see how this works... hope you like it!!


Private Sub Command1_Click()
On Error GoTo errr
Dim DirPath As String

For i = 0 To 3

If Check1(i).Value = vbChecked Then

Select Case i

Case 0
DirPath$ = "c:\test a"
File1.Path = "c:\test a"

If File1.ListCount = 0 Then
MsgBox "No Files Deleted From: " & File1.Path
Else
Kill "c:\test a\*.*"
MsgBox File1.ListCount & " files deleted from '" & File1.Path & "'"
End If

Case 1
DirPath$ = "c:\test b"
File1.Path = "c:\test b"

If File1.ListCount = 0 Then
MsgBox "No Files Deleted From: " & File1.Path
Else
Kill "c:\test b\*.*"
MsgBox File1.ListCount & " files deleted from '" & File1.Path & "'"
End If

Case 2
DirPath$ = "c:\test c"
File1.Path = "c:\test c"

If File1.ListCount = 0 Then
MsgBox "No Files Deleted From: " & File1.Path
Else
Kill "c:\test c\*.*"
MsgBox File1.ListCount & " files deleted from '" & File1.Path & "'"
End If

Case 3
DirPath$ = "c:\test d"
File1.Path = "c:\test d"

If File1.ListCount = 0 Then
MsgBox "No Files Deleted From: " & File1.Path
Else
Kill "c:\test d\*.*"
MsgBox File1.ListCount & " files deleted from '" & File1.Path & "'"
End If

Case 4
DirPath$ = "c:\test z"
File1.Path = "c:\test z"

If File1.ListCount = 0 Then
MsgBox "No Files Deleted From: " & File1.Path
Else
Kill "c:\test z\*.*"
MsgBox File1.ListCount & " files deleted from '" & File1.Path & "'"
End If

End Select
End If
Next i

errr:
Select Case Err.Number
Case 0
Exit Sub
Case 76
makeit = MsgBox("Create: " & DirPath, vbYesNo, "Folder not found:")
If makeit = vbYes Then
MkDir DirPath
MsgBox DirPath & " was created.", vbOKOnly, "Success"
End If
Case Else
MsgBox Err.Description, vbCritical, "error: " & Err.Number
End Select
End Sub in the begining man created code.
in the end code will create man.
clones are coming only matter of time.
examples?
 
File1.Path = "c:\test a"
If File1.ListCount = 0 Then
MsgBox "No Files Deleted From: " & File1.Path
[bold]File1.Refresh[/bold]
Else
Kill "c:\test a\*.*"
MsgBox File1.ListCount & " files deleted from '" & File1.Path & "'"
[bold]File1.Refresh[/bold]
End If





you might want to add those in.... this way if they click it 2x... it wont error out saying file not found... :) in the begining man created code.
in the end code will create man.
clones are coming only matter of time.
examples?
 
Wow, OK, thanks for all the help!!

I am trying out the code in the last thread you posted. I'll let you know what happens.

 
Can you explain the array a little? I am not clear on the case select concept.
 
ok with the array...

what i did was copyed and pasted a checkbox .. 3 times.. so now i had like chkbox(0), chkbox(1), chkbox(2), chkbox(3)..... ok the (#) mean it is a array.. im not to much with technical stuff i just know how to do it hehe.. but this is made for an easy method of changing all the properties at once... ill give and example..

example.. ok i want to change the value of all the check boxes.. an dill show you how the array comes in hand for large amount of arrays..

without an array...
chkbox1.value = vbChecked
chkbox2.value = vbChecked
chkbox3.value = vbChecked
chkbox4.value = vbChecked

ok thats not to bad looking correct.. well if you change the names.. that would be alot of typing..
ie.
filecheckc.value...
harddrivechk.value...

ok well i dont wanna type all that out but im sure you can see where that would get tedious...

ok that was with out a array... so in those you would have to write out each control and stuff and if you have alot going on that can take some time... now here it is with an array.... note... arrays start with (0) not (1)...

for i = 0 to 3
chkbox([bold]i[/bold]).value = vbChecked
next i

ok what that is doing.. is its going to loop aroudn there.. replacing the i with a number... that number will = the array index... and as you can see.. just for 4 arrays it is one line shorter.. so imagine.. 15? 20? yeah arrays can get up there.. i have a calculator project showing off some array things and case select stuff if you would like to see it.. here ill give and example of 15.. just to show how it would be done though im sure you already know..

for i = 0 to [bold]14[/bold] 'this goes upto 15..
chkbox([bold]i[/bold]).value = vbChecked
next i

ok now ill explain [bold]select case[/bold].. and then ill put both methods together...


ok this is also a BIG time saver.. i LOVE my select case's..
well this would be used to replace the 'if' 'then' command..
haha like the array and now with this when i am tryign to explain i forget how or why i would use them haha... ok ill give you and example with something you could type into a text box....

example: i am not sure why you would want to do this.. im pulling this type of example from the way i work with winsock and sorting out the incoming data... but ill make it so the text box is the "incoming data" here we go...

ok this is without select case... this would perhaps be put in a combo peraps to see if the right words were entered.. maybe a secret area or easter egg a log in idea who knows?

ok here i go...

if text1 = "test1" then
label1 = "You may enter"
end if

if text1 = "i dont fly high" then
label1 = "in the sky i will be forever"
end if

if text1 = "cows drink milk"
label1 = "eat more chicken"
end if


ok once again im lazy and dont want to type out tons of stuff hehe once again that would be put in a chat command button... select case is used to check the value of somehting for many diffrent answers as the abod did but with outall the extra writing... here is example of the about...

select case [bold]text1[/bold]
'as you see select case is lettign you knwo your startign the select case... and text one is basically saying 'if text1 = ' ....

[bold]case[/bold] "test1" 'case i like saying ='s
label1 = "You may enter" 'then what happens goes here..
[bold]case[/bold] "i dont fly high" then
label1 = "in the sky i will be forever"
[bold]case[/bold] "cows drink milk"
label1 = "eat more chicken"
end select 'this closes the select case...

arg i gtg ill finish up the rest later if you need it an example of them together.. hope htis helps..
in the begining man created code.
in the end code will create man.
clones are coming only matter of time.
examples?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top