[RESOLVED] Access Variable anywhere from the Form
Hello,
I have a Sub,
Public Sub C-Main_Click()
‘
‘ Lines of code.
‘
Then I declare this and open the file:
Code:
ReDim DDD(TotXLin, 16) As Integer
sFilename = FtR
nFileNum = FreeFile
Open sFilename For Binary As #nFileNum
Get #nFileNum, , DDD
Close #nFileNum
‘
‘ more code,
‘
For NbRow = 1 To 16
Sstr = Sstr & " " & DDD(NbLin, NbRow)
Next NbRow……. and so ….more code…. all this works fine.
I have 3 other subs under the same Form and I must access this previously open DDD
and work on the same declared array.
How can I access all ready opened DDD in this Public Sub without opening the file every time
in every other sub and declaring the array when activated by a click.
I have tried place the array declaration and the opening part in the module, call it,
but that does not seems to work for me. Declaring dynamic array every time create me a problem.
Any suggestion would be appreciated.
Re: Access Variable anywhere from the Form
Try REDIM PRESERVE instead when you have declared it in a module of in a form's declaration section.
Re: Access Variable anywhere from the Form
Quote:
Originally Posted by dee-u
Try REDIM PRESERVE instead when you have declared it in a module of in a form's declaration section.
In case the typo is confusing what dee-u meant was...
Try REDIM PRESERVE instead when you have declared it in a module or in a form's declaration section.
Re: Access Variable anywhere from the Form
Quote:
Originally Posted by MartinLiss
In case the typo is confusing what dee-u meant was...
Try REDIM PRESERVE instead when you have declared it in a module or in a form's declaration section.
Oooppsss. :blush:
Re: Access Variable anywhere from the Form
Hi Guys, Thanks for replay,
Changing the code or declaring it as:
Code:
ReDim Preserve DDD(TotXLin, 16) As Integer
sFilename = FtR
nFileNum = FreeFile
Open sFilename For Binary As #nFileNum
Create me an error, Variable not defined.
What I am missing here?
Re: Access Variable anywhere from the Form
Which variable? Is it defined? If so, where and how? (Dim?, Public?, Private?)
Re: Access Variable anywhere from the Form
The idea was to open the file as I indicated in my 1st statement
and have this ready for further use. All works fine there.
To reapet the process from the different subs, I must go through the same declaration
and opening procedures which I want to cut off.
I thought adding “preserve” in to the code will take care of the problem,
but now the error confuses me.
Re: Access Variable anywhere from the Form
If you answer the questions about which variable I asked in my pervious post then maybe I can help.
Re: Access Variable anywhere from the Form
OK, It is this single line of code.
ReDim DDD(TotXLin, 16) As Integer
Nothing is missing at this point, no errors are being detected.
Process continue and finishes.
I am changing the same line adding what was suggested.
ReDim Preserve DDD(TotXLin, 16) As Integer
Now error is being detected, and now I do not know what to do with it.
To be honest, I do not know what missing variable I should look for?
This preserve function I am not familiar with and perhaps this create all this misunderstanding.
Re: Access Variable anywhere from the Form
Let me try one more time. Do you have any of the following statements anywhere in your program? And if so, where?
Dim RRR() As Integer
Public RRR() As Integer
Private RRR() As Integer
Re: Access Variable anywhere from the Form
Code:
ReDim Preserve DDD(TotXLin, 16) As Integer
Remove the bold part.
Re: Access Variable anywhere from the Form
That should make no difference.
Re: Access Variable anywhere from the Form
Quote:
Originally Posted by MartinLiss
That should make no difference.
That will result in one less error since the IDE won't accept that. :)
Re: Access Variable anywhere from the Form
Sure it will.
Code:
Private Sub Form_Load()
Dim DDD() As Integer
ReDim Preserve DDD(5, 3) As Integer
End Sub
Re: Access Variable anywhere from the Form
Quote:
Originally Posted by MartinLiss
Sure it will.
Code:
Private Sub Form_Load()
Dim DDD() As Integer
ReDim Preserve DDD(5, 3) As Integer
End Sub
Argggedd... :blush: I think I need to sleep now, it is past 7AM. :D
I actually wrongly tried it this way.
Code:
Dim DDD() As String
Private Sub Form_Load()
ReDim Preserve DDD(5, 3) As Integer
End Sub
Re: Access Variable anywhere from the Form
To answer Matins’ question,
“ Let me try one more time.
Do you have any of the following statements anywhere in your program?
And if so, where? “
Dim RRR() As Integer ( DDD)
Public RRR() As Integer
Private RRR() As Integer
No, I do not have, none of this.
The very first time I do declare it in this form, “ReDim DDD(TotXLin, 16) As Integer”
it is just before I open the file in the “Public Sub C-Main_Click()”
From this moment the parameters in this dynamic array are being change constantly.
Re: Access Variable anywhere from the Form
Two rules you should follow:
- Always use Option Explicit
- Always Dim/Public/Private all your variables. If you do the first you'll be forced to do the second.
To fix your problem:
- Add a code module to your project (if you don't have one already).
- Put Public DDD() As Integer as the first line in that module (or the second line if you use Option Explicit)
Re: Access Variable anywhere from the Form
Thanks, Martin,
Yes, I always declare all in Option Explicit , can’t do the other way.
I my case I had to declare this dynamic array ( the way I did)
and of course Option Explicit will not take it in this format.
That was the reason placing it where I did.
Now, when you showed me the form of declaration, I also see the way I should deal with it
using the module which I all ready use a lot in this arrays nested program.
Best Regards,
Re: Access Variable anywhere from the Form
Not sure I understand but if you have solved the problem then you can help us by pulling down the Thread Tools menu and selecting the Mark Thread Resolved item.
Re: Access Variable anywhere from the Form
Solving it now.
In a few moments I should have this done and check mark will appear.
Thanks again.