Solved

Question about MS Comm Control

Posted on 2001-08-21
13
293 Views
Last Modified: 2012-06-21
Hi all!!

I'm trying to write a class module with some functions to communicate with the device attached to the comm port.

I am aware that the MS Comm Control is able to do this with forms but is it possible to use the component using late binding?? i.e. create the instance of the component during runtime as my project does not contain any forms at all

Thanks
0
Comment
Question by:boonhui78
  • 3
  • 3
  • 2
  • +5
13 Comments
 
LVL 19

Expert Comment

by:DreamMaster
ID: 6409168
I'm not sure of this, since I have never used the Comm control...but I would say, yes that is possible...

If I am mistaking please correct me folks...

Max.
0
 
LVL 39

Expert Comment

by:appari
ID: 6409231
i think its not possible. alternatively you can take a form with the Comm Control and without showing the form( simply by loading ) you can take the reference of the control on the form.

0
 
LVL 2

Expert Comment

by:tdubroff
ID: 6409423
It is a common VB practice to have a hidden form with several ActiveX controls that require a form.  This hidden form just acts as a repository for your form-based controls.  In VB.NET this is no longer a problem.  Of course VB.NET is only in Beta 2 right now.  I assume most companies won't be upgrading until a year from now or so.

If your control shows up in the component window, then it needs a form.  If instead, you just have a reference to the type library, and your control does not show up in the component window, then you don't need a form to host the control.
0
 
LVL 1

Expert Comment

by:falkor
ID: 6409889
I agree that it is not necessary to drop the control on a form, but it can be very time-consuming/cumbersom.  The following example can be used to bring up the common dialog control. Use like 'msgbox fnGetPath("Title")'

I know it's not the control you want, but the amount of code to do it without a form may make you think twice about this

'---------start of code
Private Const ALLFILES = "All Files"

' Declarations for Windows Common Dialogs procedures
Private Type API_OPENFILE
  strFilter As String             ' Filter string
  intFilterIndex As Long          ' Initial Filter to display.
  strInitialDir As String         ' Initial directory for the dialog to open in.
  strInitialFile As String        ' Initial file name to populate the dialog with.
  strDialogTitle As String        ' Dialog title
  strDefaultExtension As String   ' Default extension to append to file if user didn't specify one.
  lngFlags As Long                ' Flags (see constant list) to be used.
  strFullPathReturned As String   ' Full path of file picked.
  strFileNameReturned As String   ' File name of file picked.
  intFileOffset As Integer        ' Offset in full path (strFullPathReturned) where the file name (strFileNameReturned) begins.
  intFileExtension As Integer     ' Offset in full path (strFullPathReturned) where the file extension begins.
End Type

Private Type API_WINOPENFILENAME
    lStructSize As Long
    hWndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustrFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    Flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustrData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type

Declare Function API_GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" _
  (pOpenfilename As API_WINOPENFILENAME) _
As Boolean

Public Function fnGetPath(strTitle As String) As String
  ' Comments  : Simple file open routine. For additional options, use GetFileOpenEX()
  ' Parameters: strInitialDir - path for the initial directory, or blank for the current directory
  '             strTitle - title for the dialog
  ' Returns   : string path, name and extension of the file selected
  '
  Dim fOK As Boolean
  Dim typWinOpen As API_WINOPENFILENAME
  Dim typOpenFile As API_OPENFILE
  Dim strFilter As String
  Dim strInitialDir As String

  On Error GoTo PROC_ERR
   
  strInitialDir = CurDir
   
  ' Set reasonable defaults for the structure
  strFilter = CreateFilterString("Word Files (*.doc)", "*.doc", "Text Files (*.txt)", "*.txt", "Excel Files (*.xls)", "*.xls")
   
  If strInitialDir <> "" Then
    typOpenFile.strInitialDir = strInitialDir
  Else
    typOpenFile.strInitialDir = CurDir()
  End If
   
  If strTitle <> "" Then
    typOpenFile.strDialogTitle = strTitle
  End If
   
  typOpenFile.strFilter = strFilter
  typOpenFile.lngFlags = &H1000 Or &H1
   
  ' Convert the  structure to a Win structure
  Convert2Win typOpenFile, typWinOpen
   
  ' Call the Common dialog
  fOK = API_GetSaveFileName(typWinOpen)
   
  ' Convert the Win structure back to a  structure
  ConvertWin2 typWinOpen, typOpenFile
   
  fnGetPath = typOpenFile.strFullPathReturned
       
PROC_EXIT:
  Exit Function
   
PROC_ERR:
  fnGetPath = ""
  Resume PROC_EXIT

End Function

Private Function CreateFilterString(ParamArray varFilt() As Variant) As String
  ' Comments  : Builds a Windows formatted filter string for "file type"
  ' Parameters: varFilter - parameter array in the format:
  '                          Text, Filter, Text, Filter ...
  '                         Such as:
  '                          "All Files (*.*)", "*.*", "Text Files (*.TXT)", "*.TXT"
  ' Returns   : windows formatted filter string
  '
  Dim strFilter As String
  Dim intCounter As Integer
  Dim intParamCount As Integer

  On Error GoTo PROC_ERR
   
  ' Get the count of paramaters passed to the function
  intParamCount = UBound(varFilt)
   
  If (intParamCount <> -1) Then
     
    ' Count through each parameter
    For intCounter = 0 To intParamCount
      strFilter = strFilter & varFilt(intCounter) & Chr$(0)
    Next
     
    ' Check for an even number of parameters
    If (intParamCount Mod 2) = 0 Then
      strFilter = strFilter & "*.*" & Chr$(0)
    End If
     
  End If

  CreateFilterString = strFilter
   
PROC_EXIT:
  Exit Function
   
PROC_ERR:
  CreateFilterString = ""
  Resume PROC_EXIT
   
End Function

Private Sub Convert2Win(Struct As API_OPENFILE, Win_Struct As API_WINOPENFILENAME)
  ' Comments  : Converts the passed API structure to a Windows structure
  ' Parameters: Struct - record of type API_OPENFILE
  '             Win_Struct - record of type API_WINOPENFILENAME
  ' Returns   : Nothing
  '
  Dim strFile As String * 512

  On Error GoTo PROC_ERR
   
  Win_Struct.hWndOwner = Application.hWndAccessApp
  Win_Struct.hInstance = 0

  If Struct.strFilter = "" Then
    Win_Struct.lpstrFilter = ALLFILES & Chr$(0) & "*.*" & Chr$(0)
  Else
    Win_Struct.lpstrFilter = Struct.strFilter
  End If
  Win_Struct.nFilterIndex = Struct.intFilterIndex

  If Struct.strInitialFile <> "" Then
    Win_Struct.lpstrFile = Struct.strInitialFile & String$(512 - Len(Struct.strInitialFile), 0)
  Else
    Win_Struct.lpstrFile = String(512, 0)
  End If
   
  Win_Struct.nMaxFile = 511
   
  Win_Struct.lpstrFileTitle = String$(512, 0)
  Win_Struct.nMaxFileTitle = 511

  Win_Struct.lpstrTitle = Struct.strDialogTitle
  Win_Struct.lpstrInitialDir = Struct.strInitialDir
  Win_Struct.lpstrDefExt = Struct.strDefaultExtension

  Win_Struct.Flags = Struct.lngFlags

  Win_Struct.lStructSize = Len(Win_Struct)
   
PROC_EXIT:
  Exit Sub
   
PROC_ERR:
  Resume PROC_EXIT
   
End Sub

Private Sub ConvertWin2(Win_Struct As API_WINOPENFILENAME, Struct As API_OPENFILE)
  ' Comments  : Converts the passed API structure to a Windows structure
  ' Parameters: Win_Struct - record of type API_WINOPENFILENAME
  '             Struct - record of type API_OPENFILE
  ' Returns   : Nothing
  '
  On Error GoTo PROC_ERR
       
  Struct.strFullPathReturned = Left(Win_Struct.lpstrFile, InStr(Win_Struct.lpstrFile, vbNullChar) - 1)
  Struct.strFileNameReturned = RemoveNulls(Win_Struct.lpstrFileTitle)
  Struct.intFileOffset = Win_Struct.nFileOffset
  Struct.intFileExtension = Win_Struct.nFileExtension
   
PROC_EXIT:
  Exit Sub
   
PROC_ERR:
  Resume PROC_EXIT
   
End Sub

Private Function RemoveNulls(strIn As String) As String
  ' Comments  : Removes terminator from a string
  ' Parameters: strIn - string to modify
  ' Return    : modified string
  '
  Dim intChr As Integer

  intChr = InStr(strIn, Chr$(0))

  If intChr > 0 Then
    RemoveNulls = Left$(strIn, intChr - 1)
  Else
    RemoveNulls = strIn
  End If

End Function
'---------end of code
0
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6410310
The MSComm control is an ocx and as such requires a container form in order to operate.
Add your MSComm Control to a dummy form and make the forms visible property to false. You can then access all the functionality of the control.

The code above will work only with the common control as it also exposes it's methods through a dll comdlg32.dll

The MSComm control doesn't.

If you want to do this type of thing without the MSComm control. You will have to use other API calls to do it.
Vin.
0
 

Author Comment

by:boonhui78
ID: 6411825
Hi everyone!

Guess I've found a way to do it already =)

All I did was to include the component (MS Comm Control 6.0) in my class module and then under the Class Initialize procedure I just need to put in the code below...
Set objComm = CreateObject("MSCommLib.MSComm")

Many thanks,
Eric
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:tdubroff
ID: 6413246
Hmm, yes well, that's late-binding and not early-binding, but if it works for you...
0
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6413259
Can you let us know if your late binding technique worked.

Vin.
0
 

Author Comment

by:boonhui78
ID: 6416028
Hi Vincent!

It works =)

Cheers!
0
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6416824
How are you handling the OnComm event for the MSComm Control ?

Vin.

0
 

Author Comment

by:boonhui78
ID: 6421239
Hi Vincent,

I don't need the OnComm event as my program does it's on polling on the input buffer

Eric
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7202414
boonhui78, an EE Moderator will handle this for you.
Moderator, my recommended disposition is:

    Refund points and save as a 0-pt PAQ.
    *** I think five separate 'experts' said it was impossible.  lol

DanRollins -- EE database cleanup volunteer
0
 
LVL 5

Accepted Solution

by:
Netminder earned 0 total points
ID: 7216012
Per recommendation, points refunded and question closed.

Netminder
CS Moderator
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

943 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

15 Experts available now in Live!

Get 1:1 Help Now