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:
Dim cbBox As Control
For Each cbBox In [Form_Barclaycard Ladies Entry - Images_v2]
Dim intAttempt As Integer
Dim intAttemptPrevious As Integer
Dim intAttemptFuture As Integer
For intAttempt = 1 To 10
If cbBox.Name = "Attempt" & CStr(intAttempt) & "Com" Then
If cbBox.Text = "" Then
cbBox.Locked = False
cbBox.BackColor = -2147483643
Else
For intAttemptPrevious = intAttempt To 1
cbBox.Locked = True
cbBox.BackColor = 16777164
Next
For intAttemptFuture = (intAttempt + 1) To 10
cbBox.Locked = True
Next
End If
End If
Next
Next
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:
Sub MySub()
Dim cbBox As Control
For Each cbBox In [Form_Barclaycard Ladies Entry - Images_v2]
Dim intAttempt As Integer
Dim intAttemptPrevious As Integer
Dim intAttemptFuture As Integer
For intAttempt = 1 To 10
If cbBox.Name = "Attempt" & CStr(intAttempt) & "Com" Then
If cbBox.Text = "" Then
cbBox.Locked = False
cbBox.BackColor = -2147483643
Else
For intAttemptPrevious = intAttempt To 1
cbBox.Locked = True
cbBox.BackColor = 16777164
Next
For intAttemptFuture = (intAttempt + 1) To 10
cbBox.Locked = True
Next
End If
End If
Next
Next
End Sub
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
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.
Re: Can someone check my VBA code???
This loop should go backwards, like this:
VB Code:
For intAttemptPrevious = intAttempt To 1 [COLOR=Red]Step -1[/COLOR]
Re: Can someone check my VBA code???
Quote:
Originally Posted by dglienna
This loop should go backwards
Why? :confused:
All that is happening is that the colours and locked state are being changed... the direction of the loop seems irrelevant to me.
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.
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.
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.
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