By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,830 Members | 682 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,830 IT Pros & Developers. It's quick & easy.

Evaluating result of a variable from multiple checkboxes

P: 14
I have a routine that searches a directory structure for files. Right now the file pattern is simply *.* and when it is finished, I have a cleanup routine that strips out the file extensions that I don't want (*.m3u, and files which the routine will flag as 'unknown')

What I would like to do is to populate a variable with the acceptable file extensions based on the users choice.

For example, I setup four checkboxes, each a different type of file (i.e., audio, video, etc.)

Each type has a checkbox. How can I populate a variable (ScanExt) based on the slection of checkboxes. So if the user chose only audio, the variable might read *.mp3, *.wma, and if they chose audio and video (then it would read *.mp3, *.wma, *.wmv, *.avi.

Does anyone have any suggestions on how I might acheive this?

Thanks,
CJ
Dec 31 '08 #1
Share this Question
Share on Google+
12 Replies


DonRayner
Expert 100+
P: 489
Put this at the top of your forms module

Expand|Select|Wrap|Line Numbers
  1. public myvar as string
  2.  
put this in the on change event for each checkbox

Expand|Select|Wrap|Line Numbers
  1. Call VarString
  2.  
Add this to the bottom of the forms module

Expand|Select|Wrap|Line Numbers
  1. Public Sub VarString ()
  2. If cb1 then
  3.     myvar = "*.mp3"
  4. Else
  5.     myvar = ""
  6. End if
  7.  
  8. if cb2 then
  9.     if myvar <> "" then
  10.         myvar = myvar & ", *.wma"
  11.     else
  12.         myvar = "*.wma"
  13.     end if
  14.  
  15. if cb3 then
  16.     if myvar <> "" then
  17.         myvar = myvar & ", *.wmv"
  18.     else
  19.         myvar = "*.wmv"
  20.     end if
  21.  
  22. if cb4 then
  23.     if myvar <> "" then
  24.         myvar = myvar & ", *.avi"
  25.     else
  26.         myvar = "*.avi"
  27.     end if
  28. End If
  29. End Sub
  30.  
Replace the cb1, cb2, cb3, cb4 with the actual names of your checkboxes.
Dec 31 '08 #2

P: 14
I see what this is trying to achieve and it is working but only partially.
I get lots of errors on the fourth check box about being null, and it doesn't quite work if the audio option gets turned off.

I have the code below:
Expand|Select|Wrap|Line Numbers
  1. Public Sub MediaString()
  2.  
  3. If chkAudio Then
  4.     If ScanExt <> "" Then
  5.         ScanExt = ScanExt & "*.mp3, *.wma"
  6.     Else
  7.         ScanExt = ""
  8.     End If
  9. End If
  10.  
  11. If chkVideo Then
  12.     If ScanExt <> "" Then
  13.         ScanExt = ScanExt & ", *.avi"
  14.     Else
  15.         ScanExt = "*.mp3,*.wma"
  16.     End If
  17. End If
  18.  
  19. If chkImages Then
  20.     If ScanExt <> "" Then
  21.         ScanExt = ScanExt & ", *.jpg, *.gif"
  22.     Else
  23.         ScanExt = "*.avi"
  24.     End If
  25. End If
  26.  
  27. If chkKaraoke Then
  28.     If ScanExt <> "" Then
  29.         ScanExt = ScanExt & ", *.CDG, *.KAR"
  30.     Else
  31.         ScanExt = ", *.jpg, *.gif"
  32.     End If
  33. End If
  34.  
  35. If chkAudio And chkVideo And chkImages And chkKaraoke Then
  36.     ScanExt = "*.*"
  37. End If
  38.  
  39. End Sub
I included the last piece so that if they are all left on at the default, then it would simply include all files.
I am still struggling with this and trying to polish it.

Any combo of the four checkboxes could be on or off, and being a file filter the format is as your code suggests, *.mp3, *.wav, *.avi, etc.

The only thing I have not tried, and perhaps I missed this in your code, was having a seperate checkbox for each extension, but that would many more checkboxes than I have now, about 9 off them if I split this out into a seperate checkbox for each extension possible. Then adding extensions would require additional checkboxes.

What do you think?
Dec 31 '08 #3

P: 14
I had to tweak the first one, to try and get the formatting right, but that doesn't seem to to the trick either.
Dec 31 '08 #4

DonRayner
Expert 100+
P: 489
Try it like this

Expand|Select|Wrap|Line Numbers
  1. Public Sub MediaString()
  2. If chkAudio Then
  3.  ScanExt = "*.mp3, *.wma"
  4. Else
  5.  ScanExt = ""
  6. End If
  7.  
  8. If chkVideo Then
  9.  If ScanExt <> "" Then
  10.   ScanExt = ScanExt & ", *.avi"
  11.  Else
  12.   ScanExt = "*.avi"
  13.  End If
  14. End If
  15. If chkImages Then
  16.  If ScanExt <> "" Then
  17.   ScanExt = ScanExt & ", *.jpg, *.gif"
  18.  Else
  19.   ScanExt = "*.jpg, *.gif"
  20.  End If
  21. End If
  22. If chkKaraoke Then
  23.  If ScanExt <> "" Then
  24.   ScanExt = ScanExt & ", *.CDG, *.KAR"
  25.  Else
  26.   ScanExt = "*.CDG, *.KAR"
  27.  End If
  28. End If
  29. If chkAudio And chkVideo And chkImages And chkKaraoke Then
  30. ScanExt = "*.*"
  31. End If
  32. End Sub
  33.  
Jan 1 '09 #5

DonRayner
Expert 100+
P: 489
oops, add the following between lines one and two of my previous post.

Expand|Select|Wrap|Line Numbers
  1. scanext = ""
  2.  
you can then delete lines 4,5 or leave as is.
Jan 1 '09 #6

P: 14
Thank you for sending this, I see where I got the intention of your original suggestion built with a couple mistakes. I still get the error on chkKaraoke, but I think I can put in some handling.

This looks like it will work great.

THANKS.!!!!! I appreciate your help.

CJ
Jan 1 '09 #7

P: 14
I seem to have found a pretty effective workaround. What seems to be happening is that the check on chkKaraoke isn't taking, and the logic was having trouble with the fact that it was Null, so I added the following code for this and seem to have a workaround, that stops generating errors, you just don't get the check mark,.

If IsNull(chkKaraoke) Then
GoTo MediaString_Exit
End If

While not ideal, if this is the worst thing that my code has to deal with for this function, then it works for me.

Thanks again for your help. Basic, to the point, and it gets the job done.
Now I will take that variable and can use it in the file scan module for the project.

PROGRESS!!!!!! I Love it!
Jan 1 '09 #8

DonRayner
Expert 100+
P: 489
That's great, good luck on the rest of your project. You can fix the error you're getting by setting the default value of your fields to false.
Jan 1 '09 #9

P: 14
I will actually have to use the other one, because the default is to scan all files.
But you are right, and either way should be perfectly functional.
Jan 1 '09 #10

P: 14
Don,

OK, I am adding to this one, because what this solution resolved, lead to another issue. I suspect you will probably have a fast answer.
I am using the AllenBrowne code of "filldirlisttotable"

The code works great. Only one issue, I cannot find anywhere on the net any notes about using the filespec option.
By default it uses *.*, and I can make it work by specifying only one extension, such as *.jpg, but it becomes useless when I specify multiple extensions such as *.jpg, *.gif, *.png.

Obviously that must be the wrong way to provide the filespec.
Have you used this function? I am providing part of it below.
There may even be a better way of doing this, but I have written substantial code around this.

I am kind of thinking that unless this code simply canot handle the multiple extensions regardless of how they are provided, then I may have to re-write this section of code. This is what the previous issue is feeding into to allow the user to search only certain types of files.
Expand|Select|Wrap|Line Numbers
  1. Call FillDirToTable(colDirList, strpath, strFileSpec, bIncludeSubfolders)
This goes to:
Expand|Select|Wrap|Line Numbers
  1. Private Function FillDirToTable(colDirList As Collection _
  2.     , ByVal strFolder As String _
  3.     , strFileSpec As String _
  4.     , bIncludeSubfolders As Boolean)
  5.  
  6.     'Build up a list of files, and then add add to this list, any additional folders
  7.     On Error GoTo Err_Handler
  8.  
  9.     Dim strTemp As String
  10.     Dim colFolders As New Collection
  11.     Dim vFolderName As Variant
  12.     Dim strSQL As String
  13.  
  14.     'Add the files to the folder.
  15.     strFolder = TrailingSlash(strFolder)
  16.     strTemp = Dir(strFolder & strFileSpec)
  17.     Do While strTemp <> vbNullString
  18.          gCount = gCount + 1
  19.          SysCmd acSysCmdSetStatus, gCount
  20.          strSQL = "INSERT INTO zzLibScan " _
  21.           & " (MediaFileName, MediaPath) " _
  22.           & " SELECT """ & strTemp & """" _
  23.           & ", """ & strFolder & """;"
  24.          CurrentDb.Execute strSQL
  25.         colDirList.Add strFolder & strTemp
  26.         strTemp = Dir
  27.     Loop
  28.  
  29.     If bIncludeSubfolders Then
  30.         'Build collection of additional subfolders.
  31.         strTemp = Dir(strFolder, vbDirectory)
  32.         Do While strTemp <> vbNullString
  33.             If (strTemp <> ".") And (strTemp <> "..") Then
  34.                 If (GetAttr(strFolder & strTemp) And vbDirectory) <> 0& Then
  35.                     colFolders.Add strTemp
  36.                 End If
  37.             End If
  38.             strTemp = Dir
  39.         Loop
  40.         'Call function recursively for each subfolder.
  41.         For Each vFolderName In colFolders
  42.             Call FillDirToTable(colDirList, strFolder & TrailingSlash(vFolderName), strFileSpec, True)
  43.         Next vFolderName
  44.     End If
  45.  
  46. Exit_Handler:
  47.  
  48.     Exit Function
  49.  
  50. Err_Handler:
  51.     strSQL = "INSERT INTO zzLibScan " _
  52.     & " (MediaFileName, MediaPath) " _
  53.     & " SELECT ""  ~~~ ERROR ~~~""" _
  54.     & ", """ & strFolder & """;"
  55.     CurrentDb.Execute strSQL
  56.  
  57.     Resume Exit_Handler
  58. End Function
  59.  
  60. Public Function TrailingSlash(varIn As Variant) As String
  61.     If Len(varIn) > 0& Then
  62.         If Right(varIn, 1&) = "\" Then
  63.             TrailingSlash = varIn
  64.         Else
  65.             TrailingSlash = varIn & "\"
  66.         End If
  67.     End If
  68. End Function
What do you think?
Jan 2 '09 #11

DonRayner
Expert 100+
P: 489
Airtech, sorry I'm so slow getting back to you, I was away over the weekend. I'll take a look at this later today and see what's up with.
Jan 5 '09 #12

DonRayner
Expert 100+
P: 489
Airtech, can you post the actual line of code that you are using to call this function.
Jan 6 '09 #13

Post your reply

Sign in to post your reply or Sign up for a free account.