Difference in using "With Me" statement in dotnet VB and access VBA

  • Thread starter Thread starter darrell
  • Start date Start date
D

darrell

I recently wrote a program in Access VBA that contains this snippet of code:

For i = 1 to TotalBanks
With Me("txtBank" & Chr(i + 64))
.BackColor = vbBlue
.ForeColor = vbWhite
End With
Next i

The above code was tested and works.

I've attempted to do something similar in VB dotnet and I keep receiving
a syntax error. Here's the snippet:

For rowCtr = 0 To 2
For whlCtr = 0 To 4
With Me("r" & rowCtr & "w" & whlCtr)
.borderstyle = BorderStyle.Fixed3D
End With
Next whlCtr
Next rowCtr

The error is "Class 'TestApp.frmSlot' cannot be indexed because it has
not default property."

any ideas?

Thanks, Darrell
 
Sept. 9, 2006

Hey Darrell... the "With" statement is to help cut down on the code you
have to write if you are going to be repeatingly changing settings on a
control. . . and therefore the syntax is "With [objectname]". The problem
you are running into is there is no "object' repesented by "Me("r" & rowCtr
& "w" & whlCtr)".

Me is the current component the code is running under, and passing something
between ()s doesn't give anything back. You can do what you are wanting with
*other* components such as collections...

dim A() as string
With A(3) ' Represents the 4th string object in this 0-based string array
end with

A.GetValue(int) is the set default property for people who want to use the
short-hand version A(int)... it is just a shortcut. So while each class can
have a different type of default property & parameters, the "Me" class
doesn't have one.

So if there is an object in Me which you need... you'll need to dig through
Intellisense for Me to find the property/function you need to call to get
the contained object.

If you let us know what the object is and what it is doing in Me, then we
could probably help you in finding the property/function you need.

Hope this helps!
--

Joseph Bittman
Microsoft Certified Solution Developer
Microsoft Most Valuable Professional -- DPM

Blog/Web Site: http://CactiDevelopers.ResDev.Net/
 
darrell said:
I recently wrote a program in Access VBA that contains this snippet of code:

For i = 1 to TotalBanks
With Me("txtBank" & Chr(i + 64))
.BackColor = vbBlue
.ForeColor = vbWhite
End With
Next i

The above code was tested and works.

I've attempted to do something similar in VB dotnet and I keep receiving
a syntax error. Here's the snippet:

For rowCtr = 0 To 2
For whlCtr = 0 To 4
With Me("r" & rowCtr & "w" & whlCtr)
.borderstyle = BorderStyle.Fixed3D
End With
Next whlCtr
Next rowCtr

The error is "Class 'TestApp.frmSlot' cannot be indexed because it has
not default property."

You must pay attention to what is "Me" in each context. In the second
case, it seems it's your form and the error message is clear: it needs
a default, indexed property for that syntax to be valid.

I don't know what you were trying to select with those "coordinates",
so I'll pretend it was a Grid object (I'm inventing things here, due to
the lack of information from your post). So if you wanted your form to
return a given cell from that grid, you'd have:

<aircode>
Public Default ReadOnly Property _
Cell(Coords As String) As GridCell 'Whatever
Get
Dim Result As New GridCell
'Translate the coordinates
'Assign the cell to the result variable
Return Result
End Get
End Property
</aircode>

With something like this, I suppose you can use the short circuit
syntax Me(xxx) (where xxx, in your case, would be a valid coordinate).

HTH.

Regards,

Branco.
 
Thanks for responding, Joseph.

example 1:
For rowCtr = 2 To 5
With Me("TextBox" & rowCtr)
.borderstyle = Fixed3D
.BackColor = BlanchedAlmond
End With
Next rowCtr

example 2:
With Me.TextBox2
.BorderStyle = BorderStyle.Fixed3D
.BackColor = Color.BlanchedAlmond
End With
repeat for TextBox3 through <whatever>
or
Me.TextBox2.BorderStyle = BorderStyle.Fixed3D
Me.TextBox2.BackColor = Color.BlanchedAlmond
repeat for TextBox3 through <whatever>

I have four textboxes on my form (in this example), named TextBox2
through TextBox5. In VBA for Access, example 1 works - no squawks. In
dotnet VB, example 1 bombs, example 2 though works fine. That's fine
for three or four boxes...for ten or twelve, it's a pain. I'm
constantly changing background colors, and am developing with the border
style set to FixedSingle. It seems to me that VB dotnet should be able
to something like example 1. :)

Darrell
 
Sept. 9, 2006

Hey Darrell... so what you want to do is just use a method to look up your
control name in the Me container basically.....

Common to other "container" objects (including big ones like Groupboxes,
panels, etc, and small ones like user controls), there is a "Controls"
property of Me to find a control. . . use the Find method (pass the ID of
the control - like Button1, and then pass True to search all child controls
as the second parameter).

This Me.Controls.Find method returns an array of Object types. . . . with
each member of the array being a match to your control ID paramter -
however, I absolutely have no idea how you could have more than one control
returned because .Net doesn't allow two controls to have the same name.

So since we know at design time that it will only return one object in that
array, we just use the first member - ie, Object(0) member.

So we've got our control by specifying the 0 index of the object array, but
since it is still of an Object type, we need to cast it to a Button type so
we can change its properties. . . which is done with the
Ctype(OriginalObject, newtypename) operation...

'loop with I as integer being your button1 , 2, 3, 4 etc
dim O() as object = Me.Controls.Find("Button" & I, True)
dim OurButton as Button = O(0)
*OurButton.Property = New Value

or in a very condensed form if you were only going to change 1 property:

Ctype(Me.Controls.Find("Button" & I, True)(0), Button).Property = Value

:) Hopefully this works for you!
--

Joseph Bittman
Microsoft Certified Solution Developer
Microsoft Most Valuable Professional -- DPM

Blog/Web Site: http://CactiDevelopers.ResDev.Net/
darrell said:
Thanks for responding, Joseph.

example 1:
For rowCtr = 2 To 5
With Me("TextBox" & rowCtr)
.borderstyle = Fixed3D
.BackColor = BlanchedAlmond
End With
Next rowCtr

example 2:
With Me.TextBox2
.BorderStyle = BorderStyle.Fixed3D
.BackColor = Color.BlanchedAlmond
End With
repeat for TextBox3 through <whatever>
or
Me.TextBox2.BorderStyle = BorderStyle.Fixed3D
Me.TextBox2.BackColor = Color.BlanchedAlmond
repeat for TextBox3 through <whatever>

I have four textboxes on my form (in this example), named TextBox2 through
TextBox5. In VBA for Access, example 1 works - no squawks. In dotnet
VB, example 1 bombs, example 2 though works fine. That's fine for three
or four boxes...for ten or twelve, it's a pain. I'm constantly changing
background colors, and am developing with the border style set to
FixedSingle. It seems to me that VB dotnet should be able to something
like example 1. :)

Darrell

Sept. 9, 2006

Hey Darrell... the "With" statement is to help cut down on the code you
have to write if you are going to be repeatingly changing settings on a
control. . . and therefore the syntax is "With [objectname]". The problem
you are running into is there is no "object' repesented by "Me("r" &
rowCtr & "w" & whlCtr)".

Me is the current component the code is running under, and passing
something between ()s doesn't give anything back. You can do what you are
wanting with *other* components such as collections...

dim A() as string
With A(3) ' Represents the 4th string object in this 0-based string array
end with

A.GetValue(int) is the set default property for people who want to use
the short-hand version A(int)... it is just a shortcut. So while each
class can have a different type of default property & parameters, the
"Me" class doesn't have one.

So if there is an object in Me which you need... you'll need to dig
through Intellisense for Me to find the property/function you need to
call to get the contained object.

If you let us know what the object is and what it is doing in Me, then we
could probably help you in finding the property/function you need.

Hope this helps!
 
Back
Top