|
-
Jan 13th, 2006, 09:15 AM
#1
Thread Starter
Hyperactive Member
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
-
Jan 13th, 2006, 09:33 AM
#2
Addicted Member
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
-
Jan 13th, 2006, 09:38 AM
#3
Thread Starter
Hyperactive Member
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
-
Jan 13th, 2006, 03:35 PM
#4
Lively Member
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.
-
Jan 13th, 2006, 03:37 PM
#5
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]
-
Jan 13th, 2006, 03:49 PM
#6
Re: Can someone check my VBA code???
 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.
-
Jan 13th, 2006, 04:00 PM
#7
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.
-
Jan 13th, 2006, 04:21 PM
#8
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jan 13th, 2006, 06:57 PM
#9
Frenzied Member
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
-
Jan 15th, 2006, 12:41 PM
#10
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|