Results 1 to 12 of 12

Thread: [RESOLVED] Representing a Control With a Variable

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    23

    Resolved [RESOLVED] Representing a Control With a Variable

    Hello all. I have a series of flexgrids that I am trying to set up for text entry. I am using Osborne's VB From The Ground UP as my reference manual, and I am using the style described in this book to float a textbox over the flexgrid. However, this style is designed for one and only one flexgrid.

    I have one textbox (txtFloater) and 12 flexgrids (flxFullAP, flxFullA, flxFullAM, etc.). I want to use a variable to point to the correct flexgrid, so that the code for all the flexgrid methods is the same except for the first line of flxXXX_EnterCell (where I would set the 'pointer' variable to represent the current flexgrid in use). My instinct is to do the following:

    VB Code:
    1. Private ThisFlx As Control 'This variable will represent whichever flxgrid we are using.
    2.  
    3. Private Sub PositionFloater()
    4. txtFloater.Left = ThisFlx.CellLeft + ThisFlx.Left
    5. txtFloater.Top = ThisFlx.CellTop + ThisFlx.Top
    6. txtFloater.Width = ThisFlx.CellWidth
    7. txtFloater.Height = ThisFlx.CellHeight
    8. txtFloater.Visible = True
    9. txtFloater.SetFocus
    10. End Sub
    11.  
    12. Private Sub flxFullAP_EnterCell()
    13. ThisFlx = flxFullAP 'This line would change for each _EnterCell() method
    14. txtFloater.Text = ThisFlx.Text
    15. PositionFloater
    16. End Sub
    17.  
    18. Private Sub Form_Load()
    19. ThisFlx = flxFullAP 'This sets the top-left flexgrid as the jumping-off point...
    20. Set txtFloater.Font = ThisFlx.Font
    21. Show
    22. ThisFlx.Col = 0
    23. ThisFlx.Row = 0
    24. PositionFloater
    25. End Sub
    26.  
    27. Private Sub txtFloater_Change()
    28. ThisFlx.Text = txtFloater.Text
    29. End Sub
    30.  
    31. Private Sub txtFloater_KeyDown(KeyCode As Integer, Shift As Integer)
    32. 'This allows implementation of Excel-standard cell navigation
    33. Select Case KeyCode
    34.    Case Is = vbKeyUp:      ThisFlx.Row = ThisFlx.Row - 1 Mod ThisFlx.Rows
    35.    Case Is = vbKeyDown:    ThisFlx.Row = ThisFlx.Row + 1 Mod ThisFlx.Rows
    36.    Case Is = vbKeyRight:   ThisFlx.Col = ThisFlx.Col + 1 Mod ThisFlx.Cols
    37.    Case Is = vbKeyLeft:    ThisFlx.Col = ThisFlx.Col - 1 Mod ThisFlx.Cols
    38. End Select
    39. End Sub
    Obviously, this is isn't working for me. There are several other ways that I could do it, but I'm sure there's a way this approach can work. I simply don't know the correct syntax for representing controls with variables. So my question is:

    How do I represent a control with a variable.
    The key line here is ThisFlx = flxFullAP. When this line executes, I get the following error: Object variable or With block variable not set.

    Any help would be greatly appreciated. Thanks everybody!

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Representing a Control With a Variable

    Dim ThisFlx as Flexgrid (or whatever it is)

    set ThisFlx = flxFullAP (or whichever you want)
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    23

    Re: Representing a Control With a Variable

    Thanks for the quick reply! I now get the following error:
    Run-Time Error 13: Type Mismatch.

    I get this error after the form_load event completes:

    VB Code:
    1. 'Step-Through of Form_Load:
    2.  
    3. Private Sub Form_Load()
    4.    Set ThisFlx = flxFullAP
    5.    Set txtFloater.Font = ThisFlx.Font
    6.    Show
    7.    ThisFlx.Col = 0 'CALLS EnterCell CALLS PositionFloater
    8.    ThisFlx.Row = 0 'CALLS EnterCell CALLS PositionFloater
    9.    PositionFloater 'Appears to be redundant
    10. End Sub 'ERROR FIRES UPON COMPLETION OF THIS LINE

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    23

    Re: Representing a Control With a Variable

    One more piece of info:
    I don't know if it's important or not, but this form is being called modally from another form. When the Type Mismatch error occurs, the program's 'debug line' hangs on the modal call in the startup form, not in the actual form where the flexgrids are:
    VB Code:
    1. Private Sub Command1_Click()
    2. [HL]formRateSheet.Show , vbModal[/HL]
    3. End Sub

  5. #5

  6. #6
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Representing a Control With a Variable

    formRateSheet.Show , vbModal
    The second argument to the Show method is the Owner Form. Get rid of the comma.

    formRateSheet.Show vbModal

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    23

    Re: Representing a Control With a Variable

    Quote Originally Posted by brucevde
    The second argument to the Show method is the Owner Form. Get rid of the comma.
    AHA! I'm not exactly sure what your explanation means, so I'll have to review the formX.Show method. But after your suggestion I searched through all my code and found that my other formX.Show calls DID NOT include a comma. So that did the trick.... sort of.

    Now I'm receiving the following error:
    Run-Time Error 400: Form already displayed; can't show modally.
    Once again, this error occurs after formRateSheet.Form_Load() completes.

    Thanks again for all the quick help everybody!

  8. #8
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Representing a Control With a Variable

    when you have a form vbModal
    any breaks will jump to that line because in a way.. vb is looping endlessly on that line untill the form is closed.
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

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

    Re: Representing a Control With a Variable

    Quote Originally Posted by ufdigga
    AHA! I'm not exactly sure what your explanation means, so I'll have to review the formX.Show method. But after your suggestion I searched through all my code and found that my other formX.Show calls DID NOT include a comma. So that did the trick.... sort of.

    Now I'm receiving the following error:
    Run-Time Error 400: Form already displayed; can't show modally.
    Once again, this error occurs after formRateSheet.Form_Load() completes.

    Thanks again for all the quick help everybody!
    When you show a form using the vbModal parameter you are telling VB that you want the user to be locked into that form and not be able to go to any other form until the modal form is closed or it is no longer being shown modally. Since you are already showing one form modally, the error is telling you that you can't show a second one modally until you finish with the first.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    23

    Re: Representing a Control With a Variable

    OK, after doing a search for that run-time error, it appears that I'm trying to show the form twice: once with formRateSheet.Show() in the startup form, and again in the Form_Load() method of formRateSheet.

    So I removed the Show from Form_Load(). Now I get an invalid procedure error when I try to set focus to txtfloater within Form_Load (this first occurs when the line
    VB Code:
    1. ThisFlx.Col=0 CALLS EnterCell CALLS PositionFloater CALLS txtFloater.SetFocus.
    I've run into this problem before when trying to set focus within the Form_Load event. How do I avoid this problem?

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    23

    Re: Representing a Control With a Variable

    Alright everybody, I'm all set. I've fixed the problem by eliminating the following lines from the Form_Load event:
    VB Code:
    1. Set ThisFlx = flxFullAP 'This sets the top-left flexgrid as the jumping-off point...
    2. Set txtFloater.Font = ThisFlx.Font
    3. 'Show                     -> REMOVED
    4. 'ThisFlx.Col = 0         -> REMOVED
    5. 'ThisFlx.Row = 0        -> REMOVED

    Thanks for everybody's help!

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

    Re: Representing a Control With a Variable

    Unless there is other code to handle it, this code will cause errors.

    VB Code:
    1. Select Case KeyCode
    2.    Case Is = vbKeyUp:      ThisFlx.Row = ThisFlx.Row - 1 Mod ThisFlx.Rows
    3.    Case Is = vbKeyDown:    ThisFlx.Row = ThisFlx.Row + 1 Mod ThisFlx.Rows
    4.    Case Is = vbKeyRight:   ThisFlx.Col = ThisFlx.Col + 1 Mod ThisFlx.Cols
    5.    Case Is = vbKeyLeft:    ThisFlx.Col = ThisFlx.Col - 1 Mod ThisFlx.Cols
    6. End Select
    For example when you are already in the last row the ThisFlx.Row = ThisFlx.Row + 1 Mod ThisFlx.Rows in the vbKeyDown code will result in a row number equal to the number of rows, which is invalid since the rows start at 0. Similarly if you are already at the top row ThisFlx.Row = ThisFlx.Row - 1 Mod ThisFlx.Rows in the vbKeyUp code will result in -1 which of course is also an invalid row.

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