Solved

MSCOMM

Posted on 2004-08-17
1
2,387 Views
Last Modified: 2013-12-25
Hello,
I have a vb program where I let the user enter a com port number that they will be using for an rs232 device.
It works well.

If I try to open a comport that I do not have
(com port 4) it gives me an error that says
 invalid com port number.


   MSComm1.CommPort = myComPortNumber
   MSComm1.InputLen = 0
   MSComm1.Settings = "9600,N,8,1"
   MSComm1.PortOpen = True


How do I test for this before the error happens?
0
Comment
Question by:jackjohnson44
1 Comment
 
LVL 11

Accepted Solution

by:
bingie earned 125 total points
ID: 11825301
Try this function

Private Function DoesCommPortExist(CommPort As Integer) As Boolean
On Error Resume Next
        MSComm1.CommPort = CommPort
        MSComm1.PortOpen = True
        If Not (Err.Number = 8002) Then  
            MsgBox "CommPort Exists"
            DoesCommPortExist = True
        Else
            MsgBox "CommPort Does Not Exist"
            DoesCommPortExist = False
        End If
        MSComm1.PortOpen = False
End Function


Just pass the comm port number you wish to test with the following:

x = DoesCommPortExist (comm port number)

It will return a boolean true / false based on the result. You can then comment out the msgboxes.

Bingie
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now