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

problem with storing data in an array

Status
Not open for further replies.

mfm

Programmer
Jul 9, 2001
16
FR
Hi guys,
I've got a problem on how to store a set of data in a simple array. I am using the Sub Form_KeyDown(Keycode as Integer, Shift as Integer) API function, and my intention is to store the kestroke into an array. So basically hit one key then the keycode is stored in array(1), then hit another key, the second keycode will store in array(2), however this doesnt happen because it will try to store into array(1).
Do you guys have any idea, I can show the syntax roughly if you want.
thx a lot
 
what you want is something like the following. Where MyArr is a module level variable.


Form_KeyDown(Keycode as Integer, Shift as Integer)
dim lCurValue as long

lCurValue = ubound(MyArr)

redim MyArr( lCurValue + 1 )
MyArr(lCurValue + 1) = Keycode

Hope this helps,

Chris Dukes
 
If you want to save what's already in the array, you will need to make that:

Redim Preserve MyArr( lCurValue + 1 )


Robert
 
Yep,

In my haste to reply, I forgot that.

Chris Dukes
 
Thx for the reply. Appreciate that. Btw, I'm new to VB so I'm not sure the concept yet, but I know C quite comfortable. So when you say MyArr is a module level variable, I presume you need to add module and declare your array variable MyArr in the module. Can you tell me how this module being called from the main form? (This is actually my main problem). How the code will be in the module? Is it something like this?

Public Sub arrStorage()

Dim I As Integer
'assume to store keycodes for each key on the 105 normal keyboard
Dim MyArr(1 to 105) As Long

I = 1
Do While (MyArr(I) <> 0)
I=I+1
Loop

If (MyArr(I) = 0)
MyArr(I) = frmMain.KeyCode
End If

End Sub

Tell me If I'm wrong. However, why do I got message 'Invalid ReDim' when I run the program?

Any idea? Thx a lot.
 
i think you should make the array a global declared in the main code module. if you make the array local to the routine that you are assigning the values to the array will not survive when you exit the routine that logs the keystrokes. the following should help

the array declare in the master code module declare section :


public dim myarray(1 to 100,1 to 2) as long

place this declaration in the main code module, not the form.

on the form load, call sub main

form frm_load()
...
main() -- the call to sub main to initialize the array
...
end frm_load

in the master code module,usually where you declare the global vars and any api calls, declare sub main and do it to it

public sub main()
...
for ii=1 to 100
for jj=1 to 2
myarray(ii,jj)=0 -- this is where you initialize myarray
next jj
next ii
...
end sub

the array initializes when you load the main form. if you initialize in a code module, you wont have to worry about the scope rules involved when declaring on the form. hope this helps...

you can re-dim the myarray whenever you need more than
100 x 2 array elements. a good rule of thumb is


...
newsize = oldsize + oldsize\2
redim preserve myarray(newsize,2)

 
Redim Preserve will only work on the last element in an array:

newsize = oldsize + oldsize\2
redim preserve myarray(newsize,2)

,will not work.

You would have to do it like:

Redim Preserve myarray(2,newsize)

As well as adjusting the rest of the code to account for the changed element locations.

Robert
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top