Results 1 to 10 of 10

Thread: Can someone check my VBA code???

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    484

    Can someone check my VBA code???

    I am not familiar with VBA, but seems like my code is wrong, please advise me how to fix these codes.

    VB Code:
    1. Dim cbBox As Control
    2.    
    3.     For Each cbBox In [Form_Barclaycard Ladies Entry - Images_v2]
    4.       Dim intAttempt As Integer
    5.       Dim intAttemptPrevious As Integer
    6.       Dim intAttemptFuture As Integer
    7.       For intAttempt = 1 To 10
    8.          If cbBox.Name = "Attempt" & CStr(intAttempt) & "Com" Then
    9.                If cbBox.Text = "" Then
    10.                   cbBox.Locked = False
    11.                   cbBox.BackColor = -2147483643
    12.                Else
    13.                   For intAttemptPrevious = intAttempt To 1
    14.                      cbBox.Locked = True
    15.                      cbBox.BackColor = 16777164
    16.                   Next
    17.                   For intAttemptFuture = (intAttempt + 1) To 10
    18.                      cbBox.Locked = True
    19.                   Next
    20.                End If
    21.          End If
    22.       Next
    23.     Next

  2. #2
    Addicted Member
    Join Date
    Jan 2006
    Location
    Montreal, Canada
    Posts
    152

    Re: Can someone check my VBA code???

    Did you put your code in a sub?
    If so, this is why it doesn't compile.
    If not, where do you get an error?

    VB Code:
    1. Sub MySub()
    2. Dim cbBox As Control
    3.    
    4.     For Each cbBox In [Form_Barclaycard Ladies Entry - Images_v2]
    5.       Dim intAttempt As Integer
    6.       Dim intAttemptPrevious As Integer
    7.       Dim intAttemptFuture As Integer
    8.       For intAttempt = 1 To 10
    9.          If cbBox.Name = "Attempt" & CStr(intAttempt) & "Com" Then
    10.                If cbBox.Text = "" Then
    11.                   cbBox.Locked = False
    12.                   cbBox.BackColor = -2147483643
    13.                Else
    14.                   For intAttemptPrevious = intAttempt To 1
    15.                      cbBox.Locked = True
    16.                      cbBox.BackColor = 16777164
    17.                   Next
    18.                   For intAttemptFuture = (intAttempt + 1) To 10
    19.                      cbBox.Locked = True
    20.                   Next
    21.                End If
    22.          End If
    23.       Next
    24.     Next
    25. End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    484

    Re: Can someone check my VBA code???

    It gives me an error:
    You can't reference a property or method for a control unless the control has a focus.

    I have absolutely no clue what this is.

    By the way, I have put these codes in the current, so when the form is loading, the code load as well.

    Can you help?

    Thank you very much

    PlayKid

  4. #4
    Lively Member
    Join Date
    Jun 2005
    Posts
    112

    Re: Can someone check my VBA code???

    I assume this is in Access?

    I only get that type of error in Access and it annoys the hell out of me. I have to set the focus on a control every time I want to access a property.

  5. #5
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Can someone check my VBA code???

    This loop should go backwards, like this:
    VB Code:
    1. For intAttemptPrevious = intAttempt To 1 [COLOR=Red]Step -1[/COLOR]

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Can someone check my VBA code???

    Quote Originally Posted by dglienna
    This loop should go backwards
    Why?
    All that is happening is that the colours and locked state are being changed... the direction of the loop seems irrelevant to me.

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Can someone check my VBA code???

    Because he is starting with a number and GOING to 1, which won't happen, unless the starting number is negative.

    The second loop is written differently, for some unknown reason, that I took to mean that he wants to loop backwards.

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Can someone check my VBA code???

    Since your not using a index or resetting the cbBox object variable in the nested loop its only going to affect a single object 10x. Until the next parent loop, but then again same issue.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Can someone check my VBA code???

    VBA is different from most languages in that in many cases to reference the properties of an object, the object must have the focus. In the first line of your first If block, add a line setting the focus to that control. This is an annoying "feature" of VBA.
    Another option may be to set the Tag property of the control to a unique value, and check that. But it still has to have the focus.
    Tengo mas preguntas que contestas

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    484

    Re: Can someone check my VBA code???

    so which means that my idea behide the code is correct, just that there are some annoying things about VBA that I don't understand, is that correct?
    I am so so so afraid that my idea is wrong, but let me check when I play with those codes again, as I am not with that computer at the moment.

    Thank you very much,

    if I am experiencing more problems, I will post it up as soon as I can.

    Thank you

    PlayKid

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