Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2010
    Location
    UK
    Posts
    160

    Unanswered: Loops twice through VBA code

    Hi all,

    I'm trying to write code where if i click on the Description field in expands the field to the entire length of the screen. Then if you click it again to returns to the original height.

    The problem i have is the code gets executed twice??

    So it first goes through the first 'If' statment, updates gbl_int_Clicked by 1 then starts from the top again, see gbl_int_Clicked is 1 so goes to the second 'If' statement!!! arrrrrh. I've tried exit sub at the end of each if statement but no, it insists on executing it twice. Please help :-)

    here's the code


    Private Sub Description_Click()

    If gbl_int_Clicked = 0 Then

    Description.Height = 7420
    Line15.Height = 7420
    Line4.Height = 7420
    Line11.Height = 7420
    Line45.Height = 7420
    Line30.Height = 7420
    gbl_int_Clicked = 1


    ElseIf gbl_int_Clicked = 1 Then

    Description.Height = 400
    Line15.Height = 400
    Line4.Height = 400
    Line11.Height = 400
    Line45.Height = 400
    Line30.Height = 400
    gbl_int_Clicked = 0

    End If

    End Sub

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    1. You can simplify and spare the global variable:
    Code:
    Private Sub Description_Click()
    
        If Me.Description.Height = 400 Then
            Me.Description.Height = 7420
            Me.Line15.Height = 7420
            Me.Line4.Height = 7420
            Me.Line11.Height = 7420
            Me.Line45.Height = 7420
            Me.Line30.Height = 7420
        Else
            Me.Description.Height = 400
            Me.Line15.Height = 400
            Me.Line4.Height = 400
            Me.Line11.Height = 400
            Me.Line45.Height = 400
            Me.Line30.Height = 400
        End If
    
    End Sub
    Have a nice day!

  3. #3
    Join Date
    Sep 2010
    Location
    UK
    Posts
    160
    that's clever thinking :-) it's worked fine

    Out of curiosity is there any reason why my code looped twice?

    Also, when i reduce the field back to its original height. The record still stays at the enlarged height.

    I basically have a continuous form which shows all the record for an order. When the user clicks on the description field it expands, and when you click again it goes back to the original size. The problem is when it expands, it expands all the descriptions field (which is fine) since they go off the bottom of the screen. When i click again, the description field returns to its original height but the record height stays enlarged. I tried a requery after click but that didn't work. Any ideas appreciated
    Last edited by marcusmacman; 01-11-12 at 10:17.

  4. #4
    Join Date
    Sep 2006
    Location
    Surrey, UK
    Posts
    994
    Provided Answers: 2
    Your code looped twice because you changed the state of the control that was referenced in the IF and ELSEIF statements.
    10% of magic is knowing something that no-one else does. The rest is misdirection.

  5. #5
    Join Date
    Sep 2010
    Location
    UK
    Posts
    160
    Hi all,

    Just to complete the thread. I managed to sus out getting the height of the form back to the original size after i increased the height of the description field. Here is the code.

    Private Sub Description_Click()

    If Me.Description.Height = 400 Then
    Me.Description.Height = 7420
    Me.Line15.Height = 7420
    Me.Line4.Height = 7420
    Me.Line11.Height = 7420
    Me.Line45.Height = 7420
    Me.Line30.Height = 7420

    Else
    Me.Description.Height = 400
    Me.Line15.Height = 400
    Me.Line4.Height = 400
    Me.Line11.Height = 400
    Me.Line45.Height = 400
    Me.Line30.Height = 400
    Me.Detail.Height = 400

    End If


    End Sub

Posting Permissions

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