Author Topic: Learning VBA  (Read 14024 times)

0 Members and 1 Guest are viewing this topic.

nivuahc

  • Guest
Learning VBA
« on: October 03, 2003, 09:41:57 AM »
Since I haven't really used VBA in a very long time I've decided that I'm gonna teach myself, from scratch, to use it with AutoCAD.

I'll document what I do and how I do it here so that you can follow along with my progress or, if you already know VBA, point me in the right direction.

This will be open to anyone so feel free to tell me what you think.

To start off, I'll be using a couple of things as a reference.

AfraLisp has a great section on VBA as it pertains to AutoCAD and I highly recommend it.

They have a great article called An Introduction to VB and VBA that you should check out if you're new to VBA.

As my project, I'm going to be creating a VBA application in AutoCAD that takes the place of my companies 'Details' book.

So I'll start by explaining what I hope to accomplish.

The menu I'm using is in a folder called

C:\clc\acadmenu

Under this directory I have a folder called DETAILS which contains sub-folders of my details broken into categories.

We're an electrical engineering firm so our categories are as such:

COMM - Communications Details
LIGHTS - Lighting Details
MECH - Mechanical Details
MISC - Miscellaneous Details
POWER - Power Details

All of my details are named as such:

Comm0001.dwg
Comm0002.dwg
Comm0003.dwg
Comm0004.dwg
Comm0005.dwg
Comm0006.dwg
etc...

and

Lite0001.dwg
Lite0002.dwg
Lite0003.dwg
Lite0004.dwg
Lite0005.dwg
Lite0006.dwg
etc...

So my thinking, at this point, is this:

I have created a text file in the base directory called DETAILS.TXT which contains a list of directories (categories) with a description separated by the pipe (|) symbol

COMM|Communications Details
LIGHTS|Lighting Details
MECH|Mechanical Details
MISC|Miscellaneous Details
POWER|Power Details

In each subdirectory I have a text file, named for the folder, containing a similar list of the detail drawings along with a description such as

Comm0001|Sample Sound System Conduit Schedule
Comm0002|Ceiling Smoke Detector Mounting Detail
Comm0003|Wall Mounted Data/Telephone Outlet Detail
Comm0004|Data/Voice/Video Outlets Detail
Comm0005|Antenna Mounting Detail
etc...

My hope s that I can have the application load the information from the first text file allowing the user to select which type of detail he intends to view/insert.

Once selected, load the information from the corresponding text file and display the description of each type of detail. When the user selects the description they intend to view/insert, a large preview of the drawing will be shown and they have the option of inserting the drawing into AutoCAD.

I'll be using the AcPreview ActiveX Control module for the preview (more on control modules later) and I'll work through the process step by step so, if you like, you can create the same project yourself.

The way I figure it, if I ever want to add another detail or change the description of one it will simply mean modifying the text file pertaining to the category I wish to add/remove from.

And, as always, if you think I might be on the wrong track feel free to say so. Have a better idea? I'm open to suggestion.

So I'm off to read that introduction to VBA and I'll return here and post my thoughts on it when I'm done.

nivuahc

  • Guest
Learning VBA
« Reply #1 on: October 03, 2003, 11:06:15 AM »
Well, if you've read the introduction linked above you now know that Visual Basic for Applications (as well as VB) is an 'Event Driven Application'. In other words, it waits for something to happen before it does anything. That something can be a number of different 'events' like clicking the mouse, pressing a button, or whatever else you decide to put into your application.

The events in our project will be triggered by the objects that we create in our application.

The objects I have in mind are

The Form (A form is the window or dialog box in this project. We're going to draw our controls on this form. It's also going to trigger an event. When it loads, I'll want it to read the contents of my first text file.)

A ComboBox (Usually referred to as a 'pull-down', this is where we'll have the user select the detail category. According to the VBA Reference library a ComboBox combines the features of a ListBox and a TextBox. The user can enter a new value, as with a TextBox, or the user can select an existing value as with a ListBox. Since I don't want them typing in anything, I just want them selecting it maybe this isn't the way to go. I don't know yet.)

A ListBox (Displays a list of values and lets you select one or more. This is where we'll display the description of the details and allow the user to select the one they wish to view/insert)

Command Buttons (Two, actually. One to exit the application an another to insert the selected detail into AutoCAD)

A Preview Box (I intend on showing a large preview of the selected detail since some of our details are similar. This should make it easier for the user to select the right one. This one is special. I'll describe how to get it later.)

More to come...

daron

  • Guest
Learning VBA
« Reply #2 on: October 03, 2003, 11:15:14 AM »
Well that makes it all easier. Now, you double-click the form tool, i.e. checkbox and up pops a window for you to code. Time for the harder part, knowing what to put in.

daron

  • Guest
Learning VBA
« Reply #3 on: October 04, 2003, 02:55:16 AM »
Well, I've gone through this again. Either from the fact that it's almost midnight or their's too much information from the get-go, I don't fully know where this is going. As I read, I feel it's better suited for the Lisp application, since you are using lists. I'm sure that's just the lisper in me. One thing I do know, pseudo-code. Could you write some pseudo-code for the order in which things should happen, including if, then, else, and for each, so we can make this code modular. Also, what's in the drawings that will be inserted and can you post some of the info that will be accessed in the text files? The reason I ask may or may not be relevant, but I have a lisp that goes through a text file that has the same name as the drawing it inserted. It performs a read-file on six lines at a time. If the first line returns true, it processes each of the next five lines, if nil, it reads the next six until it finds a match or reaches the end of the file. I'm not sure how we're going to do this in VBA, but I guess that's why we learn it, right.

nivuahc

  • Guest
Learning VBA
« Reply #4 on: October 06, 2003, 08:39:35 AM »
Quote
I'm not sure how we're going to do this in VBA, but I guess that's why we learn it, right.


Exactly.

This is what I intend to have happen:

1. The form opens with a pull-down menu where the user selects the detail category.
I'd have the form, on load, read the text file DETAILS.TXT that I have listed above

2. Once a category is selected, the list of detail descriptions in that category are displayed in a ListBox.
Each of the entries in DETAILS.TXT corresponds to a directory containing detail drawings. Each of those directories also contain a text file with the name of each drawing followed by a description as shown above.

3. When the user selects a description from the ListBox it shows a preview of the drawing using the AcPreview ActiveX control module.


4. The click the 'Insert Detail' button and it inserts the drawing into the current AutoCAD drawing.


My reasons for using VBA in this instance, instead of LISP, are simple:

First, I need to re-learn VBA. Second, I really like that AcPreview control and I think it's perfect for this type of application. :)

Hopefully, I'll get to spend some time on this today.

Trev

  • Guest
Learning VBA
« Reply #5 on: October 06, 2003, 09:58:40 AM »
Hey nivuahc  I started (a while ago) working on something very similar to what you have described.
A basic block manager, containing an option to select desired service ie:Electrical then option buttons to select either lighting, power, comms etc
upon selection a list box would display all the blocks relating to selection ie:power. also showing a preview of highlighted block. I have also added an option to add in addition blocks to the library which would then copy the block to the appropriate folder and place it into the listbox.

I made a sample type version of this to show a company I worked with a tool that they could do with, Since theyre symbols library etc was so crap.
I didn't get much response so I never got around to finishing the program off. It would be a very handy tool.

nivuahc

  • Guest
Learning VBA
« Reply #6 on: October 06, 2003, 11:40:31 AM »
Trev,

Would you mind, terribly, posting it here? I'm sure it would help me develop this one.

In fact, that's how I first learned LISP. I would take a routine that I sorta liked and go through it tweaking it to my liking.

And I'm sure it would be helpful to anyone else who's trying to get a handle on this! :)

hendie

  • Guest
Learning VBA
« Reply #7 on: October 17, 2003, 07:39:19 AM »
Quote from: nivuahc

A ComboBox (Usually referred to as a 'pull-down', this is where we'll have the user select the detail category. According to the VBA Reference library a ComboBox combines the features of a ListBox and a TextBox. The user can enter a new value, as with a TextBox, or the user can select an existing value as with a ListBox. Since I don't want them typing in anything, I just want them selecting it maybe this isn't the way to go. I don't know yet.)


just set the Styleproperty of the combobox to suit:
Quote

The settings for fmStyle are:

fmStyleDropDownCombo 0   
The ComboBox behaves as a drop-down combo box. The user can type a value in the edit region or select a value from the drop-down list (default).
fmStyleDropDownList 2   
The ComboBox behaves as a list box. The user must choose a value from the list.


alternatively, you could zap the user with a "Daily quote from Dent" if the value typed in isn't in your list !

Trev

  • Guest
Learning VBA
« Reply #8 on: October 17, 2003, 07:46:35 AM »
Yep No problems nivuahc
I have a few things to tidy it up a bit to make it a little more functional
then I'll post what I've got.

nivuahc

  • Guest
Learning VBA
« Reply #9 on: October 17, 2003, 09:14:00 AM »
I've spent the little amount of spare time I have trying to figure out how to take the information in the TXT files and put it on the form making it so that when the user selects something it sets the value as a variable.

I'm having very little luck. :(

daron

  • Guest
Learning VBA
« Reply #10 on: October 17, 2003, 10:25:00 AM »
Well Chuck, if it's any consolation, I've been reading a book on VBA for Excel. A friend gave it to me. Anyway, I tried making a vba function the other day and it bombed. I'm still trying to figure out the difference between a module, a class module, and the objects folder that contains thisdrawing as a default. How do you know which one to use and when.

hendie

  • Guest
Learning VBA
« Reply #11 on: October 20, 2003, 04:14:16 AM »
Quote from: nivuahc
I've spent the little amount of spare time I have trying to figure out how to take the information in the TXT files and put it on the form making it so that when the user selects something it sets the value as a variable.

I'm having very little luck. :(


which bit are you having problems with ?

I assume you have dim(med) the variable ? in which case, when the button is pressed or whatever event triggers it,
Code: [Select]

myVar = theComboBox.value

hendie

  • Guest
Learning VBA
« Reply #12 on: October 20, 2003, 04:26:41 AM »
and might I offer a suggestion ?
instead of using a text file ~ an Access database may be a more useful / versatile tool.


have you pseudo-coded your proposed app ? and listed all the functions you want it to perform ?
a mock-up of the interface is also useful to have

Trev

  • Guest
Learning VBA
« Reply #13 on: October 20, 2003, 08:56:07 AM »
Actually Hendie thats a good idea, easy to edit an existing entry etc. etc.
Just spark a whole load of new ideas in my head     :shock:
Damn you.................
 :P

nivuahc

  • Guest
Learning VBA
« Reply #14 on: October 20, 2003, 10:22:51 AM »
Well my thinking on the TXT file, as opposed to a DB is that

1. My company is cheap. I have Access but I'm the only one.
2. If, for any reason, someone else wants to add to the list at a later time, using a TXT file is the easiest way for anyone to do it. Like say, for instance, I stop working here.

And the problem I'm having is this:

I've got the TXT file (formatted as it is above).

I've got a form.

How do I get the data in the TXT file displayed in the form (either in the combo box or the list box)?

Form opens, reads the contents of DETAILS.TXT and lists the descriptions in the combo box.

A user selects a description and that sets a variable to POWER or COMM depending on the description they chose.

With the format being

POWER|Power Details
COMM|Communications Details
etc...

as single lines of text in the DETAILS.TXT file.

So let's say the user selects 'Communication Details' from the list.

That sets a variable (let's call it VAR1 for now) to COMM and the program looks for a folder called COMM containing a TXT file called _COMM.TXT (named that way so that it's always at the top of the list when I open the folder).

Inside that TXT file is a list of details and descriptions like this:

Comm0001|Sample Sound System Conduit Schedule
Comm0002|Ceiling Smoke Detector Mounting Detail
Comm0003|Wall Mounted Data/Telephone Outlet Detail
Comm0004|Data/Voice/Video Outlets Detail
Comm0005|Antenna Mounting Detail

The program lists all of the descriptions in the list box, not the 'Comm0001' bit, and the user selects 'Wall Mounted Data/Telephone Outlet Detail' from the listbox.

A variable (let's call it VAR2 for now) is set to 'Comm0003' and the drawing 'Comm0003' is displayed in the extra large preview window.

Should the user select the button labelled 'Insert Detail into AutoCAD' the drawing is inserted into the current drawing.

Should the user change their mind and select a different description from the list, say 'Sample Sound System Conduit Schedule', the variable VAR2 would be changed to 'Comm0001' and so on.

Should the user decide that what they wanted was actually a POWER detail and the select that from the combo box the value of VAR1 changes to 'POWER' and so on.


That make any sense?

hendie

  • Guest
Learning VBA
« Reply #15 on: October 20, 2003, 10:32:27 AM »
Quote from: nivuahc
1. My company is cheap. I have Access but I'm the only one.

as long as the MBD is networked, users don't need Access installed, they just query the database through Acad using the MS JET engine.

Do you really want other users to access the text file ? someone can really muck things up if they aren't careful ! (again, if required, users can add records to the MDB using the same process)

hendie

  • Guest
Learning VBA
« Reply #16 on: October 20, 2003, 10:39:45 AM »
Quote from: nivuahc

How do I get the data in the TXT file displayed in the form (either in the combo box or the list box)?


check out Kenny's site ... here is a good intro to VBA and external text files.. http://www.afralisp.com/vba/extext.htm

nivuahc

  • Guest
Learning VBA
« Reply #17 on: October 20, 2003, 10:48:13 AM »
Quote from: hendie
as long as the MBD is networked, users don't need Access installed, they just query the database through Acad using the MS JET engine.

Do you really want other users to access the text file ? someone can really muck things up if they aren't careful ! (again, if required, users can add records to the MDB using the same process)



Actually, yeah, I do.

I make backups of every night so if a user mucks up the TXT file I can always restore the one from the previous day.

I'm also thinking that using TXT files would make this easy for anyone to use, not just us, and it would be helpful to have. Not everyone has MS Access available to them but everyone can edit a TXT file.

And, if I get really worried about someone mucking it up or if it gets to be a problem, I can always put the details and TXT files on the network and limit them to READ-ONLY for everyone but myself.

One of the perks of being the Network Administrator :D

Thanks for the link, I'll scour through it now. :)

hendie

  • Guest
Learning VBA
« Reply #18 on: October 20, 2003, 10:52:53 AM »
Okay, fair enough.
Once you've checked out Kenny's tutorial, have a go and let us know how you get on. any problems... give a shout.

hendie

  • Guest
Learning VBA
« Reply #19 on: October 20, 2003, 10:55:37 AM »
Quote from: Trev
Actually Hendie thats a good idea, easy to edit an existing entry etc. etc.
Just spark a whole load of new ideas in my head     :shock:
Damn you.................
 :P


heh

it might be worth starting a new thread if you decide to act on this, since nivuahc's going with the text file.
I suppose it will be good to have the two methods detailed so other users can make a comparison

Trev

  • Guest
Learning VBA
« Reply #20 on: October 20, 2003, 09:25:58 PM »
TO read an external file and place the contents into a list box

change the following line to suit the  location of your text file.
TextListFile = "c:\temp\testlistfile.txt"

also create a form with a combobox control named ComboBox1


Code: [Select]

Option Explicit
Dim TagList As New Collection
Dim TagListVal As Variant
Dim ArrayList() As String
Dim TextListFile As String


Sub Get_List()

On Error GoTo ERR_HANDLER
Dim iCounter As Integer
Dim sTemp As String
Dim nFile As Integer

    TextListFile = "c:\temp\testlistfile.txt"

    iCounter = 0
'   get the next free file number
    nFile = FreeFile
    ComboBox1.Clear
'   open the text file
    Open TextListFile For Input As #nFile
'   read value of each line in file, loop until the end of file
    While Not EOF(nFile)
        Line Input #nFile, sTemp
        ReDim Preserve ArrayList(iCounter)
        iCounter = iCounter + 1
        ComboBox1.AddItem sTemp
        TagList.Add sTemp
    Wend
    Close #nFile

Exit Sub

ERR_HANDLER:

MsgBox Err.Number & " " & Err.Description
Err.Clear
Exit Sub

End Sub



To input data into an external file
Code: [Select]

Sub Add2_TextFile()
On Error GoTo ERR_HANDLER
Dim myObject
Dim lay, i


'   get the next free file number
    nFile1 = FreeFile

'   open the file to append to
    Open TextListFile For Output As #nFile1

    lay = ComboBox1.List
    For i = LBound(lay) To UBound(lay)
        ReDim Preserve ArrayList(i)
        TagList.Add lay(i, 0)
'       write to the file
        If Not lay(i, 0) = "" Then
            Print #nFile1, lay(i, 0)
        End If
    Next
    Print #nFile1, TagListVal
'   close the file
    Close #nFile1

Exit Sub

ERR_HANDLER:
MsgBox Err.Number & " " & Err.Description
Err.Clear
Exit Sub

End Sub



Create a text file named testlistfile.txt and place the following lines into it.
Code: [Select]

This is a Sample Text List File
LINE 1
Line 2
line 3



Hopefully I got it all.
I also have one which allows for multi column arrays
eg: the text file contains info in the following format.
Electrical,LayerName,Description,LayerName,7,CONTINUOUS

this is good for adding description of items etc.
unfortunately I need a little fixing of my file.
my main file got corrupted and is no longer readable and I've slowly
but surely been rewriting it, (backup huh! whats a back up?)
Well my original was written in lisp and I was in the conversion process of making it a VBA program. (thats my excuse) I'll try and sort it out this arvo and post it later on.

Trev

  • Guest
Learning VBA
« Reply #21 on: October 20, 2003, 09:35:39 PM »
Yeah I'll do that Hendie.
It's definately worth doing the database version.
Like you say the user does not need ms access on their pc

nivuahc

  • Guest
Learning VBA
« Reply #22 on: October 21, 2003, 08:40:24 AM »
Quote from: Trev


To input data into an external file
Code: [Select]

Sub Add2_TextFile()
On Error GoTo ERR_HANDLER
Dim myObject
Dim lay, i


'   get the next free file number
    nFile1 = FreeFile

'   open the file to append to
    Open TextListFile For Output As #nFile1

.......







If you're Appending to the list, shouldn't that be

Code: [Select]
Open TextListFile For Append As #nFile1


Also, the variable nFile1 isn't declared anywhere (or am I missing something?).

Trev

  • Guest
Learning VBA
« Reply #23 on: October 21, 2003, 10:16:21 AM »
oops
I cut & pasted out of my program so I may have missed a couple of things
so if there are any other variables not declared just add them in.

Dim nFile1 As Integer

also you could try your suggestion and see how it works
Open TextListFile For Append As #nFile1

I know the method I used works, it does append to the end of the text file.
if both work all right, then take your pick. :)

nivuahc

  • Guest
Learning VBA
« Reply #24 on: October 21, 2003, 10:51:52 AM »
What actually led me to that was that I tried your method and it removed all of the lines from my file.

And, according to what I've read, Append would be the proper usage, no?

Remember, I'm no expert. :P

Trev

  • Guest
Learning VBA
« Reply #25 on: October 21, 2003, 07:47:07 PM »
Ok now I remember (I think)
I had a number of items in a listbox and what I probably did
was take all items in the listbox and replace whatever was in the existing
file. As I had a provision to saveas a different file. That way you could create a number of different text files, I also had a button to select your text file, so if you had multiple files you could just select the one you required.
Otherwise if you only have the one file and appending an item to the file then yes use 'Append'
Sorry about that.    :oops:
There was a method to my madness.

Ben

  • Guest
Learning VBA
« Reply #26 on: October 30, 2003, 10:54:49 PM »
Yep, perfect sense.

Here's a little function that you could use for reding the info into your form.

Option Explicit 'always a good habit to get into, forces you to define your variables
Sub GetTextInfo() 'This is the main sub heading

   Dim MyLine as String
   Dim MyVars() as String
   Dim VarCount as Integer
   Dim Cntr as Integer
   Dim SingleDig as String

'First, open your text file so that we can get stuff out of it.
   Open("path to your text file") for Input as #1
   VarCount = 0
   Do While Not EOF#1
        Input#1, MyLine
'Now to split the line up into it's two parts and put them into an array
        For Cntr = 0 to Len(MyLine) -1
              SingleDig = Right(MyLine, Cntr)
              If SingleDig = "|" Then
                   ReDim Preserve MyVar(0 to VarCount, 0 to 1)
                   MyVar(VarCount, 0) = Left(MyLine, Len(MyLine)-(Cntr + 1))
                   MyVar(VarCount, 1) = Right(MyLine, Cntr)
              End If
        Next Cntr
   Loop
End Sub

That will give you a two dimensional array that contains all the contents of your text file broken down into the two halves.

If that doesn't work (hey, who knows :) ) or you need further help in getting that info into a combobox, or listbox, then let me know.

nivuahc

  • Guest
Learning VBA
« Reply #27 on: November 03, 2003, 12:24:50 PM »
Thanks Ben!

Code: [Select]
Sub GetTextInfo()

Dim MyLine As String
Dim MyVars() As String
Dim VarCount As Integer
Dim nFile As Integer
Dim Cntr As Integer
Dim SingleDig As String

VarCount = 0
nFile = FreeFile

Open ("C:\clc\acadmenu\DETAILS\DETAILS.txt") For Input As #nFile

Do While Not EOF(nFile)
    Input #nFile, MyLine

    For Cntr = 0 To Len(MyLine) - 1
    SingleDig = Right(MyLine, Cntr)
    If SingleDig = "|" Then
        ReDim Preserve MyVars(0 To VarCount, 0 To 1)
        MyVars(VarCount, 0) = Left(MyLine, Len(MyLine) - (Cntr + 1))
        MyVars(VarCount, 1) = Right(MyLine, Cntr)
    End If
    Next Cntr
Loop

Close nFile

End Sub



Okay, I made a couple of changes to the above code (mainly adding the nFile[/i] variable and using FreeFile[/i] then closing the file at the end) and, I admit, this is where I'm having my main problems at the moment.

First of all, in the snippet you posted, at what point does the variable VarCount ever change to anything other than 0?

And I'm going to try to take this code line by line and explain it so that any of you reading this can see if I understand what's going on.

Please, please, please correct me when I'm wrong:

Code: [Select]
Sub GetTextInfo()

Dim MyLine As String
Dim MyVars() As String
Dim VarCount As Integer
Dim nFile As Integer
Dim Cntr As Integer
Dim SingleDig As String


Simple enough, I'm naming this routine GetTextInfo and I'm declaring the variables and their type.

Code: [Select]
VarCount = 0
nFile = FreeFile

Open ("C:\clc\acadmenu\DETAILS\DETAILS.txt") For Input As #nFile


I'm setting the value of VarCount to 0 and I'm setting the value of nFile to the next available free file number. Then I open my text file for input and tell the system that the file identified as nFile is in fact the text file I specified.

Code: [Select]
Do While Not EOF(nFile)
    Input #nFile, MyLine


Here I'm telling the system to keep doing what I tell it to until it gets to the end of my text file then I'm reading my text file for input. I'm setting the variable MyLine to equal the first line of text in the text file.

Code: [Select]
   For Cntr = 0 To Len(MyLine) - 1

Get the length (or number of characters) of the variable and set the variable Cntr to equal that number. Set a factor of -1 to be the increment that the variable Cntr changes each time it steps through the For loop.

Code: [Select]
   SingleDig = Right(MyLine, Cntr)
    If SingleDig = "|" Then
        ReDim Preserve MyVars(0 To VarCount, 0 To 1)
        MyVars(VarCount, 0) = Left(MyLine, Len(MyLine) - (Cntr + 1))
        MyVars(VarCount, 1) = Right(MyLine, Cntr)
    End If
    Next Cntr


Set the variable SingleDig to equal the last character in the variable MyLine, our line of text from our text file. If the character encountered is the pipe ("|") then this is where I begin to get confused.

According the the VBA help file concerning the ReDim statement:

Quote
The subscripts argument uses the following syntax:

[lower To] upper [,[lower To] upper] . . .

When not explicitly stated in lower, the lower bound of an array is controlled by the Option Base statement. The lower bound is zero if no Option Base statement is present.


Also

Quote
The ReDim statement is used to size or resize a dynamic array that has already been formally declared using a Private, Public, or Dim statement with empty parentheses (without dimension subscripts).

You can use the ReDim statement repeatedly to change the number of elements and dimensions in an array. However, you can't declare an array of one data type and later use ReDim to change the array to another data type, unless the array is contained in a Variant. If the array is contained in a Variant, the type of the elements can be changed using an As type clause, unless you’re using the Preserve keyword, in which case, no changes of data type are permitted.

If you use the Preserve keyword, you can resize only the last array dimension and you can't change the number of dimensions at all. For example, if your array has only one dimension, you can resize that dimension because it is the last and only dimension. However, if your array has two or more dimensions, you can change the size of only the last dimension and still preserve the contents of the array. The following example shows how you can increase the size of the last dimension of a dynamic array without erasing any existing data contained in the array.



Okay, so in english, what does the next line of our code say? I have no idea.

But, putting that aside I'll assume, for the moment, that it does something spectacular. The next line, I believe, sets the first element in the variable MyVars to equal anything to the left of the pipe symbol. If that is, in fact what it's doing then the next line should be setting the second element in the MyVars variable to anything to the right of the pipe symbol. I could be wrong though.

Being stuck on what's supposed to be happening with that ReDim statement has my brain twisted in knots.

That being said, it appears that the For loop continues on to the proceeding character, working it's way backwards, in the line of text if the character it's reading right now isn't the pipe symbol.

Code: [Select]
Loop

Close nFile

End Sub


Not the end of our file yet? Loop back and do this all again until it is. Once finished, close the text file we opened and end our subroutine.

How's that so far?

nivuahc

  • Guest
Learning VBA
« Reply #28 on: November 03, 2003, 04:16:02 PM »
Okay, using Debug.Print I figured out that I'd need to change the code around a little bit. I renamed some of the variable because of personal preference and I changed some of the math around following that ReDim statement (still confused about that bit).


I made a form with a combobox labeled ComboBox1 on it and I added this code to it.


Code: [Select]
Private Sub UserForm_Initialize()

Dim MyLine As String
Dim MyVars() As String
Dim VarCount As Integer
Dim nFile As Integer
Dim CharCount As Integer
Dim Delimeter As String

VarCount = 0
nFile = FreeFile

Open ("C:\clc\acadmenu\DETAILS\DETAILS.TXT") For Input As #nFile

Do While Not EOF(nFile)
    Input #nFile, MyLine
    For CharCount = 0 To Len(MyLine) - 1
    Delimeter = Right(MyLine, CharCount)
    If Left(Delimeter, 1) = "|" Then
        ReDim Preserve MyVars(0 To VarCount, 0 To 1)
        MyVars(VarCount, 0) = Left(MyLine, Len(MyLine) - (CharCount))
        MyVars(VarCount, 1) = Right(MyLine, CharCount - 1)
        ComboBox1.AddItem MyVars(VarCount, 1)
    End If
    Next CharCount
Loop
End Sub



Seems to work great! But it's a bit deceiving:



Code: [Select]
Debug.Print VarCount
Debug.Print MyVars(VarCount, 0)
Debug.Print MyVars(VarCount, 1)



Addin that to the end of the routine, just before the End Sub bit will return the following:

Quote
0
POWER
Power Details


It seems that the pesky old VarCount is rearing it's ugly head. Putting on my LISP cap I add the following right after the ComboBox.AddItem' line:

Code: [Select]
VarCount = VarCount + 1

Which gives me a 'Subscript out of range (Error 9)'

What am I doing wrong and why?

Anyone?

SomeCallMeDave

  • Guest
Learning VBA
« Reply #29 on: November 03, 2003, 05:58:53 PM »
I think that error is caused because redim can only redimension the last dimension of an array.  Your code is trying to dimension the first of two dimensions.

One way (probably not the best way) would be to read your 2 strings into a single 2 element array and then store that array in another array.

Something like
Code: [Select]

Private Sub FileReadTest()

Dim MyLine As String
Dim SingleLine(0 To 1) As String 'new array
Dim MyVars() As Variant  'changed data type
Dim VarCount As Integer
Dim nFile As Integer
Dim CharCount As Integer
Dim Delimeter As String
Dim i As Integer ' new variable

VarCount = 0
nFile = FreeFile

Open "C:\testMe.txt" For Input As #nFile

Do While Not EOF(nFile)
    Input #nFile, MyLine
    For CharCount = 0 To Len(MyLine) - 1
    Delimeter = Right(MyLine, CharCount)
    If Left(Delimeter, 1) = "|" Then
        ReDim Preserve MyVars(0 To VarCount)
        SingleLine(0) = Left(MyLine, Len(MyLine) - (CharCount))
        SingleLine(1) = Right(MyLine, CharCount - 1)
        MyVars(VarCount) = SingleLine
        'ComboBox1.AddItem MyVars(VarCount)(1)  'remove comment to write to the combobox
        VarCount = VarCount + 1
    End If
    Next CharCount
Loop
Close nFile

'check to see if it worked

For i = 0 To UBound(MyVars)
  Debug.Print MyVars(i)(0) & " --- " & MyVars(i)(1)
Next i

End Sub


The debug.print shows how to access the individual elements.

Hope this helps.  Again, not the best way,  but a way

SomeCallMeDave

  • Guest
Learning VBA
« Reply #30 on: November 03, 2003, 06:13:16 PM »
Oh yea,  you might want to check out the VBA command  split  (it may not be in all versions).

It will handle separating a string based on a delimiting character very nicely.

Code: [Select]

   Dim varTestLine as Variant
    varTestLine = Split(MyLine, "|")

Ben

  • Guest
Learning VBA
« Reply #31 on: November 03, 2003, 09:42:07 PM »
Ahh, you're right there.

Sorry about that.

Actually, I got a little mixed up in my variables there.  Instead of VarCount, it should have been CharCount.

Split would do wonders for this, but that is only in AutoCAD 2000i and above. (It's a VB6 command, and AutoCAD 2000 and lower used VB5)

ReDim is used when you aren't sure of the number of items that will be in an array, and allows you to increment it as needed.  ReDim Preserve is used, because ReDim alone would dump all of the info that was already in the array when it redimensioned it.

Hmmmmm, guess it's been a while since I played with multi dimensional arrays.  Forgot about preserve only working for redim'ing the last portion of the array.

CB_Cal_UK

  • Guest
Re: Learning VBA
« Reply #32 on: November 04, 2003, 05:05:45 AM »
Thanks for starting this thread. This kind of explanation of the code was discussed in another thread and everybody thought it was a great idea.

Just to clarify

Quote from: nivuahc
The menu I'm using is in a folder called

C:\clc\acadmenu

Under this directory I have a folder called DETAILS which contains sub-folders of my details broken into categories.


Does this mean you used to have all your details accessible from a set of drop down menu's within AutoCAD but now you require additional information to be displayed during selection (which isn't possible)

Could you use the content explorer as an easier alternative or do you need to show more information than that also?

Chris

nivuahc

  • Guest
Learning VBA
« Reply #33 on: November 04, 2003, 09:26:56 AM »
Actually my intention is to provide a simple (riiight) tool for my users that would allow them to browse through and select details to insert into Autocad in a simpler fashion than what they do now.

My vision is to have a combobox with the different types of details listed, a listbox with the different details under each type listed and a very large preview window than will allow the user to look at the details quickly and easily to determine if it is the one that they want.

Many of our details are similar. For instance: The detail we have showing a typical receptacle connection is identical to the detail we have showing a typical isolated ground receptacle connection except for, you guessed it, the isolated ground. And looking at a thumbnail of each of those details just doesn't cut it.

Right now, what we do, is flip through a book that has most of our details printed out on 8-1/2 x 11 sheets with the names of the drawings written on the bottom. When they find the detail they want they insert it into Autocad by selecting the drawing name from the appropriate folder. So finding a detail and inserting it into a drawing can take, sometimes, 10 minutes.

I'd like to cut that time down.

So, basically, I'm going for an electronic version of our detail book that can be updated/added to/modified easily.

Ben

  • Guest
Learning VBA
« Reply #34 on: November 05, 2003, 09:06:32 PM »
Actually, CB_Cal hit upon something there.

The Design Center within AutoCAD provides much of what you are looking for.

(Of course you could look at a tool called the LaunchPad, which comes with software similar to what you are looking for, without the folder restrictions.)

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16716
  • Superior Stupidity at its best
Learning VBA
« Reply #35 on: November 22, 2003, 09:48:18 AM »
One of the cool things about a personalized block manager, is that you could also effectively set other parameters that are needed based on your design practices. For example, you could set a scaling parameter for the block that defined how the block should be inserted based on the scale of the drawing. You could also create a lineweight or layer designation to force the blocks to conform to your standards when inserted.

While this is pushing it a bit to the envelope of what we are discussing here, it is admirable that nivuahc wishes to improve upon an existing concept.

If I could offer one point that you might not have thought about. In your drawing preview, rather than simply utilize the acpreview function, why not test to see if the user has voloview installed and use the voloview drawing preview if it is installed, this will give the user the ability to see a much larger and crisper preview of the drawing,  unless I am missing something.

Oh, I guess I owe my first post here to the amiable dent, for informing me that I was the last of a dying breed on the cadalog forum.

Cheers....

K
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Dent Cermak

  • Needs a day job
  • Posts: 6914
  • Clinton, Mississippi
Learning VBA
« Reply #36 on: November 22, 2003, 02:34:54 PM »
GREAT!! Now I get the blame!! Glad to see that you finally made it over Kieth !
If you are looking to me for answers, you really have problems. Carlson Survey 2016 w/ Embedded AutoCad

t-bear

  • Guest
Learning VBA
« Reply #37 on: November 22, 2003, 11:58:27 PM »
It's about time, Keith!   Glad to "see" your smiling face! :lol:

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16716
  • Superior Stupidity at its best
Learning VBA
« Reply #38 on: November 23, 2003, 01:43:30 PM »
:):):):)
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

hendie

  • Guest
Learning VBA
« Reply #39 on: November 24, 2003, 08:44:27 AM »
Quote from: SomeCallMeDave
Oh yea,  you might want to check out the VBA command  split  (it may not be in all versions).

It will handle separating a string based on a delimiting character very nicely.


Kenny has a "SPLIT" function over at http://www.afralisp.com