Results 1 to 20 of 20

Thread: [RESOLVED] Access Variable anywhere from the Form

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Location
    Toronto, Canada
    Posts
    76

    Resolved [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.

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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.

  4. #4
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Location
    Toronto, Canada
    Posts
    76

    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?

  6. #6

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Location
    Toronto, Canada
    Posts
    76

    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.
    Last edited by r-max; Feb 12th, 2009 at 04:31 PM.

  8. #8

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Location
    Toronto, Canada
    Posts
    76

    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.

  10. #10
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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

  11. #11
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Access Variable anywhere from the Form

    Code:
    ReDim Preserve DDD(TotXLin, 16) As Integer
    Remove the bold part.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  12. #12

  13. #13
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  14. #14

  15. #15
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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... I think I need to sleep now, it is past 7AM.

    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
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Location
    Toronto, Canada
    Posts
    76

    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.

  17. #17
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Access Variable anywhere from the Form

    Two rules you should follow:
    1. Always use Option Explicit
    2. Always Dim/Public/Private all your variables. If you do the first you'll be forced to do the second.

    To fix your problem:
    1. Add a code module to your project (if you don't have one already).
    2. Put Public DDD() As Integer as the first line in that module (or the second line if you use Option Explicit)

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Location
    Toronto, Canada
    Posts
    76

    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,

  19. #19
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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.

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Location
    Toronto, Canada
    Posts
    76

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width