 |
| View previous topic :: View next topic |
| Author |
Message |
gphillips Newbie
Joined: 13 Oct 2005 Posts: 22
|
Posted: Oct 26th, 2005 06:27 AM Post subject: Export - principles, arrays |
|
|
Guys,
Uisng VB 6.0
Some of this code I don't understand. Basically some kind of export activity to excel - using ascii principles/arrays/textbox array index. Ascii principle may relate to the cell reference of the spreadsheet.
Dim wkbobj as integer
Dim i as integer
Dim j as integer
' Basically a spreadsheet 5 rows, down by 2 column wide
Dim arrdata (1to 5, 1to 2) as string
Don't Understand the below - exactly how the loop is working at each stage - even though I put comments in, would apprecaiate a simple break down as I don't understand.
'Row
For i = 1 to 5
'Columns
For j = 1 to 2
'For every row and column, evaluate the texbox array index
k = (i-1) * 2 + (j-1)
' Store the dta in the arrayData
arrData(i,j) = txtdata(k)
'Export the data in the array arrData to the excel Spreaasheet
wkbobj.worksheets (1).Range(Chr(j+64) & i) = arrData (i, j)
Next j
Next i
wkObj.Save.
End sub
Private Sub Form load()
Set wkobj = GetObject ("h:\xxxxx.xls")
How does tex box array index work don't understand the principles, what do I have to set up in my form for this to export?
Would really apprecaite a simpler break down of the above.
Thanks very much,
G.
[/u][/b] |
|
| Back to top |
|
DoobieKeebler Moderator

Joined: 17 Jun 2005 Posts: 254 Location: 181°15'2.003"W, 93°5'16.956"N
|
Posted: Oct 26th, 2005 09:36 AM Post subject: Re: Export - principles, arrays |
|
|
| gphillips wrote: | | Ascii principle may relate to the cell reference of the spreadsheet. |
That's exactly what it does. I'll explain that in the wkbojb.worksheets(1) line.
| gphillips wrote: | | Don't Understand the below - exactly how the loop is working at each stage - even though I put comments in, would apprecaiate a simple break down as I don't understand. |
No problem. Just to avoid confusion I'll include some basic stuff too.
| gphillips wrote: | 'Row
For i = 1 to 5
'Columns
For j = 1 to 2
'For every row and column, evaluate the texbox array index |
This is just stepping through arrdata in order. It starts with arrdata(1,1) then goes to arrdara(1,2), arradata(2,1), arrdata(2,2), arrdata(3,1), and so forth.
| gphillips wrote: | | k = (i-1) * 2 + (j-1) |
I had to work this out on paper just to be sure, but all this line is doing is generating the numbers 0 to 9 in order.
| gphillips wrote: | ' Store the dta in the arrayData
arrData(i,j) = txtdata(k) |
arrdata(1,1) gets its data from txtdata(0), arrdata(1,2) gets its data from txtdata(1), arrdata(2,1) gets its data from txtdada(20, and so on. I'll explain that below.
| gphillips wrote: | 'Export the data in the array arrData to the excel Spreaasheet
wkbobj.worksheets (1).Range(Chr(j+64) & i) = arrData (i, j) |
Chr(j+64) creates the letters A through E. Chr(65) is A, Chr(66) is B, and so on. So Range(Chr(j+64) & i) is referencing cells A1, A2, B1, B2, C1, and so on. The code puts the info from arrdata(1,1) into cell A1, the info from arrdata(1,2) goes into cell A2, the info from arrdata(2,1) goes into cell B1, and so on. (Hey, Carl Sagan had "billions and billions", I can have "and so on". )
| gphillips wrote: | Next j
Next i
wkObj.Save.
End sub |
Closes the loops, saves the workbook, ends the sub, takes a coffee break, reads some e-mail, (all together now) "and so on".
| gphillips wrote: | | How does tex box array index work don't understand the principles, what do I have to set up in my form for this to export? |
Having an array of text boxes is pretty neat. Instead of 9 distinct text boxes named txtdata1, txtdata2, txtdata3 (audience participation time) "and so on", what you have is kind of an array of type "text box" instead of string, integer, or another variable type. Each text box still has all the properties, methods, and events of a "stand alone" text box. Doing it as an array makes it much easier to get at them.
In the code you posted all we are really interested in is the Text property. We could have taken the long way and said "Cell A1 is whatever txtdata1.Text is". Then we would have said "And cell A2 is whatever txtdata2.Text is". True, we're getting the info from arrData (see my comment below on that though) but that's the general idea.
Before I forget, we can get away with the line:
arrData(i,j) = txtdata(k)
because Text is the default property of a text box. But it's always a good idea to say specifically which property we're talking about. Irregardless of where the code came from, I'd change the line to:
arrData(i,j) = txtdata(k).Text
The easiest way to create the textbox array is to create txtdata. Get it set up the way you want (set all the properties such as Height and Width that all the text boxes will share). Then click the text box on the form. Click Edit > Copy. Your form should be selected. Edit > Paste. You'll get a message box saying:
"You already have a control named 'txtdata'. Do you want to create a control array?"
Click Yes. Your new text box will also be named txtdata, but its Index property will be set to 1. (The original txtdata will have its Index set to 0.) Note that this appears in your drop-down list of Controls (at the top of the Properties list) as txtdata(1). Move txtdata(1) wherever you want. Click the form again to tell VB that you want to paste another text box on the form. Then click Edit > Paste. Since you already have the control array you won't get the message box again. Continue this process until you have all 10 text boxes (txtdata(0) through txtdata(9)). That's all there is to it.
Unless we're going to be doing something else with arrData it's inefficient to create a string array, populate it with the text from the text boxes, and populate the cells with the data from the string array. I'd cut out the middle man and populate the workbook cells from the text boxes directly. Lose the "Dim arrData" line and change:
| Code: | 'For every row and column, evaluate the textbox array index
k = (i-1) * 2 + (j-1)
' Store the data in the arrayData
arrData(i,j) = txtdata(k)
'Export the data in the array arrData to the excel Spreadsheet
wkbobj.worksheets(1).Range(Chr(j+64) & i) = arrData(i,j) |
to this:
| Code: | ' For every row and column, export the data in the textbox array to the excel spreadsheet
k = (i-1) * 2 + (j-1)
wkbobj.worksheets(1).Range(Chr(j+64) & i) = txtdata(k).Text |
_________________ Always take into account what a user would never ever in a million years do, because someone will.
"In theory, there's no difference between theory and practice. In practice, there is." -- Yogi Berra |
|
| Back to top |
|
gphillips Newbie
Joined: 13 Oct 2005 Posts: 22
|
Posted: Oct 26th, 2005 03:28 PM Post subject: |
|
|
Thanks very much, thats greatly appreciated buddy.
Q1.Something that puzzling me is :-
wkobject.worksheets (1). Range(Chr(j+64) & i) = arraDat (i, j)
Apreciate it creates letters A - E, but I thought it would be
1, 1 - a1 - 1down 1 accross
1,2 - a2 - 1 down 2 accross
2,1 - b1 - 2down 1 accross
2,2 - b2 - 2 diown 2 accross
and so on..,
Since there are only 5 rows for i (1TO 5), and 2 columns for j (1TO 2) in the spreadsheet. Am I wrong , if so why. I am not sure if the range part does anything particular to change this.
I assume creating textbox arrays only works on VB 6.0, tried it on VBA but it doesn't work.
I do need to do it on VB6.0 will try soon. Following these step by step, steps
Just to confirm my understanding is correct:-
1. Create a text box
2. name it txtdata(k) - in this case so the code matches do use (k) or do i leave it out I.E txtdata. I asume the k is the tex box array index as per the formula i.e .
k = (i-1) * 2 + (j-1)
but is it required in the data a it is refered to in my code.
3. Edit and copy
4. Edit and paste
5. "You already have a control named txtdata. Do you want to creat a control array.
6. Click yes.
Keep on repeating for all the textbox.
Q3. When you say index, do you mean tab index?
The timing code will be coming soon m8.
Thank you
G. |
|
| Back to top |
|
DoobieKeebler Moderator

Joined: 17 Jun 2005 Posts: 254 Location: 181°15'2.003"W, 93°5'16.956"N
|
Posted: Oct 26th, 2005 05:56 PM Post subject: |
|
|
Q1 > My bad. You're absolutely right. I mixed up i and j.
Q2 > I haven't worked with VBA forms so I'm not sure how that would work. In VB, leave out the (k) part. When you refer to a specific text box using code you'll have to add the (k). But when you make them just call them (the first one really) just txtdata. And your understanding is correct.
Q3 > No, there's a separate Index property. Alphabetically it's just under the HideSelection property for text boxes.
I was just playing around with some things. If you create txtdata and set its Index to 0, when you copy and paste the control VB won't ask you if you want to create a control array. You've already told it that you want one, just by setting the Index property.
Looking forward to the timing code. _________________ Always take into account what a user would never ever in a million years do, because someone will.
"In theory, there's no difference between theory and practice. In practice, there is." -- Yogi Berra |
|
| Back to top |
|
gphillips Newbie
Joined: 13 Oct 2005 Posts: 22
|
Posted: Oct 27th, 2005 07:23 AM Post subject: |
|
|
Thinking about this more, in relation to text box arrays index.
Basically I have an output text box (multiline). The frequency refers to the number of times the user has hit that time. The time is how long it took to enter a random letter.
Time Frequency
0.2 2
0.5 3
Basically I would want to export time in worksheet 1 and frequency in worksheet 2. Is this possible? How does the theory of textbox array index work here. How can this been done what extra code inclusions. |
|
| Back to top |
|
gphillips Newbie
Joined: 13 Oct 2005 Posts: 22
|
Posted: Oct 27th, 2005 07:44 AM Post subject: |
|
|
| Sorry the 2 and 5 should be under the frequency heading , just need to be moved across, please not this one output multiline tex box , but can array theory textbox work here, so I can export the the time taken in worksheet1, frequency in worksheet 2. |
|
| Back to top |
|
gphillips Newbie
Joined: 13 Oct 2005 Posts: 22
|
Posted: Oct 27th, 2005 07:45 AM Post subject: |
|
|
2 and 3 I meant whoops.
Cheers,
Greg. |
|
| Back to top |
|
gphillips Newbie
Joined: 13 Oct 2005 Posts: 22
|
Posted: Oct 27th, 2005 07:45 AM Post subject: |
|
|
2 and 3 I meant whoops.
Cheers,
G. |
|
| Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|