ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Object property/method problem (https://www.excelbanter.com/excel-discussion-misc-queries/156516-object-property-method-problem.html)

Ayo

Object property/method problem
 
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

JLatham

Object property/method problem
 
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 ?

"Ayo" wrote:

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


Toppers

Object property/method problem
 
try this:

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

"Ayo" wrote:

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


Chip Pearson

Object property/method problem
 
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)

"Ayo" wrote in message
...
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



Ayo

Object property/method problem
 
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?


"JLatham" wrote:

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 ?

"Ayo" wrote:

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


Ayo

Object property/method problem
 
I almost forgot, thanks for the help.

"JLatham" wrote:

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 ?

"Ayo" wrote:

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


Ayo

Object property/method problem
 
Wow !!!. You are the man Chip. That was it. Thanks a lot.

"Chip Pearson" wrote:

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)

"Ayo" wrote in message
...
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




All times are GMT +1. The time now is 08:18 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com