Author Topic: A bit of newbie help  (Read 1451 times)

0 Members and 1 Guest are viewing this topic.

TimSpangler

  • Water Moccasin
  • Posts: 2010
  • CAD Naked!!
A bit of newbie help
« on: October 30, 2006, 12:51:03 PM »
OK I have been playing around with VBA for about a week just writing small bits just to see what I can and can't do.  I got this little snippet from the help files and I modified it to suit my needs.

Code: [Select]
Public Sub GetFolderList(BlockLibrary As String)

Dim FileName As String
Dim FileList() As Variant
Dim Counter As Single

' Display the names of the files that represent directories.
FileName = Dir(BlockLibrary, vbDirectory)    ' Retrieve the first entry.
Counter = 0
ReDim FileList(Counter)

Do While FileName <> ""    ' Start the loop.
    ' Ignore the current directory and the encompassing directory.
    If FileName <> "." And FileName <> ".." Then
        ' Use bitwise comparison to make sure FileName is a directory.
        If (GetAttr(BlockLibrary & FileName) And vbDirectory) = vbDirectory Then
            Debug.Print FileName    ' Display entry only if it
            FileList(Counter) = FileName
            ReDim Preserve FileList(Counter + 1)
            Counter = (1 + Counter)
        End If    ' it represents a directory.
    End If
    FileName = Dir    ' Get next entry.
Loop

End Sub

However I have a question.

Does it look OK
Can I use this sub inside of a function to fill a listbox?

Also I have tried several ways to take this code, pass it an extension also to get a list of that file type.

Any help would be appreciated.

Thanks
ACA 2015 - Windows 7 Pro
All Comments and Content by TimSpangler, Copyright 2016

Guest

  • Guest
Re: A bit of newbie help
« Reply #1 on: October 30, 2006, 01:28:06 PM »
Hey Vicious D. Timothy Slither:
I've used the following code.

Code: [Select]
Option Explicit

Public DirectoryListArray() As String

Public Sub Main()
    Dim MyFile As String
    Dim Counter As Long
   
    ReDim DirectoryListArray(1000)
   
    MyFile = Dir$("c:\temp\*.dwg")
    Do While MyFile <> ""
        DirectoryListArray(Counter) = MyFile
        MyFile = Dir$
        Counter = Counter + 1
    Loop
   
    ReDim Preserve DirectoryListArray(Counter - 1)
End Sub

Then use the following to load a list box.
Code: [Select]
ListBox1.List = DirectoryListArray

TimSpangler

  • Water Moccasin
  • Posts: 2010
  • CAD Naked!!
Re: A bit of newbie help
« Reply #2 on: October 31, 2006, 12:12:32 PM »
Thanks Matt (aka Trick Magnet M. Flava)  8-)

The code sample goes along way with helping me move from LISP to VB(a)

I've been writing little module triing to do little thing that were easy in lisp.
 New Q.  Should I add module when I create a new sub or just one big one?

Thanks Again
ACA 2015 - Windows 7 Pro
All Comments and Content by TimSpangler, Copyright 2016

Guest

  • Guest
Re: A bit of newbie help
« Reply #3 on: October 31, 2006, 12:20:29 PM »
Quote
Thanks Matt (aka Trick Magnet M. Flava)  8-)
You're welcome.

Quote
New Q.  Should I add module when I create a new sub or just one big one?
I don't know if there really is a RIGHT way to do it.
I've seen it done 3 different ways:
1) All subs lumped into one module (I don't really care for that)
2) Separate modules for each sub (also don't really care for that)
3) Group subs together in various modules (which is what I try to do). For example, all subs for registry code would be in one module; all subs for file manipulation would be in another module; so on and so forth.

I suppose there's a "recommended" way to do.  What it is, I have no idea.  I like option #3 and I'll stick to that since it's been working for me.

TimSpangler

  • Water Moccasin
  • Posts: 2010
  • CAD Naked!!
Re: A bit of newbie help
« Reply #4 on: October 31, 2006, 12:30:37 PM »
I have started to do option 3. all layer functions in one, all file folder function in another, etc  it seems to work out cleanly.

I've been writing my lisp in that manner. all in one file but I break the file up into sections.

Just seems cleaner.


Thanks
ACA 2015 - Windows 7 Pro
All Comments and Content by TimSpangler, Copyright 2016

mmelone

  • Guest
Re: A bit of newbie help
« Reply #5 on: October 31, 2006, 01:05:56 PM »
One thing to keep in mind when deciding where to put code (in terms of modules) is the scope of the variables / functions contained within it.  You need to know when the module will be "loaded" and "unloaded" in order to figure out when things like global variables contained in the module with "go away".  It doesn't make that big a difference when you are talking about modules.  But it does when you are talking about classes.

CmdrDuh

  • Automatic Duh Generator
  • King Gator
  • Posts: 4039
Re: A bit of newbie help
« Reply #6 on: October 31, 2006, 01:08:20 PM »
I use option 3 as well.  I make separate dvb files for Layers, Text, Titleblocks etc, then use modules for different subs, and finally group similiar subs in 1 module.  I hope that makes sense
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second