Object property/method problem

A

Ayo

Can anyone tell me what is wrong with this code. I keep getting "Object
doesn't support this property or method" error message and I can't figure out
wish object.

If TypeName(ctl) = "TextBox" And Left(ctl.Text, 2) = cmbDrive.Text Then
 
J

JLatham

For the purposes of debugging and figuring out where in the line of code the
problem lies, break things up for a while until you isolate it:

Dim anyText As String

If TypeName(ctl) = "TextBox" Then
anyText = Left(ctl.Text,2)
anyText = cmbDrive.Text
End If

You've now broken it down into separate components, run it and see who
breaks first. Also, I presume you've previously defined ctl ?
 
T

Toppers

try this:

If TypeName(ctl) = "Textbox" Then
If Left(ctl.Text, 2) = cmbdrive.text Then
.......
End If
End If
 
C

Chip Pearson

Ayo,

In VB/VBA, both halves of an AND statement are evaluated. This means that
the second half is evaluated even if the first half is False. Therefore,

Left(ctl.Text, 2) = cmbDrive.Text

is evaluated even if

TypeName(ctl) = "TextBox"

is False.

Thus, if ctl is not a textbox (or another control that has a Text property),
you'll get the error when attempting to read the Text property of ctl.

Instead of AND, use

If TypeName(ctl) = "TextBox" Then
If Left(ctl.Text,2) = cmbDrive.Text Then


This ensures that ctl is a TextBox prior to checking the Text property.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)
 
A

Ayo

I think the problem is Left(ctl.Text,2). Everything else seem to to working
fine. So how would I compare the first 2 character in a TextBox control with
the first 2 character in a ComboBox control?
 
A

Ayo

I almost forgot, thanks for the help.

JLatham said:
For the purposes of debugging and figuring out where in the line of code the
problem lies, break things up for a while until you isolate it:

Dim anyText As String

If TypeName(ctl) = "TextBox" Then
anyText = Left(ctl.Text,2)
anyText = cmbDrive.Text
End If

You've now broken it down into separate components, run it and see who
breaks first. Also, I presume you've previously defined ctl ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top