Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Controls / July 2008



Tip: Looking for answers? Try searching our database.

a syntax error when programming with MSComm

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Adrian Chen - 12 Jul 2008 16:29 GMT
I'am a beginner in Visual Basic 6.0. Now I try to write a ActiveX control
that is in charge of communication through COM1. So I decided to adopt
MSComm control.
   I draged a button onto the usercontrol.Then I wrote some statements in
its double-click event handler,like this:

    Private Sub Command1_Click()
    Dim MyComm As MSComm
    MyComm.CommPort = 1
    MyComm.Settings = "9600,N,8,1"
    MyComm.PortOpen = True
    End Sub

Then I started to run it in a web page by printing F5. When I double-clicked
the button, a message box jumped out on which it says as following:

Run-time error '91':
Object variable or With block variable not set

Please tell me why this happened? Where did I wrote incorrectly? Thank all
of you!

                                                                    Adrian
Chen
Bob O`Bob - 12 Jul 2008 22:52 GMT
>      Private Sub Command1_Click()
>      Dim MyComm As MSComm
>      MyComm.CommPort = 1
>      MyComm.Settings = "9600,N,8,1"
>      MyComm.PortOpen = True
>      End Sub

> Run-time error '91':
> Object variable or With block variable not set
>
> Please tell me why this happened? Where did I wrote incorrectly? Thank all
> of you!

The Dim statement tells VB what type of object the variable "MyComm"
is capable of holding a reference to.  (That's all)

It does not actually create an object of that type.

When you try to put a value into the ComPort property,
VB notices that although "MyComm" has a type, it
doesn't actually have a reference to an object.
MyComm Is Nothing.

So before you can use it, you need to create such an object
and put a reference to that object into the variable.

The easiest way to do both of these things would probably
be:

    Set MyComm = New MSComm

    Bob
Adrian Chen - 13 Jul 2008 01:51 GMT
Thank you, Bob! I was enlightened much through your words.
But I run into another problem. When I replaced that statement with "Set
MyComm = New MSComm",
the error changed to "Compile error: Invalid use of New keyword"
Why??

"Bob O`Bob" <filterbob@yahoogroups.com>
??????:%23GBzVmG5IHA.4344@TK2MSFTNGP03.phx.gbl...

>>      Private Sub Command1_Click()
>>      Dim MyComm As MSComm
[quoted text clipped - 28 lines]
>
> Bob
Bob O`Bob - 13 Jul 2008 06:16 GMT
> Thank you, Bob! I was enlightened much through your words.
> But I run into another problem. When I replaced that statement with "Set
> MyComm = New MSComm",
> the error changed to "Compile error: Invalid use of New keyword"
> Why??

That's a harder question.

I didn't actually check earlier, but I was pretty sure MSComm
was one of the controls you can't easily use that way.

What you ought to do is put the control on your form
at design time, and then you won't need "New" at all.
Since you're building a user control, there is probably
no reason /not/ to have it there all the time.

As for why that's really so, I don't think I actually know
well enough to explain.  There probably are replacement
classes you could install to completely replace MSComm,
if you were building something which had no UI at all,
but I wouldn't recommend that to any beginner.

    Bob
--
Adrian Chen - 13 Jul 2008 10:09 GMT
Bob, I've found the mistake! I was such a fool!

When I design the axtivex, I drag the MSComm control from control panel onto
usercontrol, a MSComm object has been generated automatically, so I needn't
define a object myself.
Sorry to cause you a lot of trouble! Thanks a lot!

"Bob O`Bob" <filterbob@yahoogroups.com>
??????:u5Yu1eK5IHA.3804@TK2MSFTNGP03.phx.gbl...
>> Thank you, Bob! I was enlightened much through your words.
>> But I run into another problem. When I replaced that statement with "Set
[quoted text clipped - 19 lines]
>
> Bob
Bob O`Bob - 13 Jul 2008 19:55 GMT
> Bob, I've found the mistake! I was such a fool!
>
> When I design the axtivex, I drag the MSComm control from control panel onto
> usercontrol, a MSComm object has been generated automatically, so I needn't
> define a object myself.
> Sorry to cause you a lot of trouble! Thanks a lot!

It's no trouble.

This is exactly what some of us enjoy and choose to do,
more or less as a hobby.

To see the learning process take place is more than enough
reward for the tiny bit of time I invested.

    Bob
--
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.