Visual Basic 6.0 Example

From CometWiki

(Difference between revisions)
Jump to: navigation, search
(New page: '''Cometlib -- The Open Interface to the Comet File System.''' '''VB6.0 Sample'''. This document will show you how to build a small VB program that: Establishes a connection with CFAM ...)
Line 5: Line 5:
This document will show you how to build a small VB program that:  
This document will show you how to build a small VB program that:  
-
Establishes a connection with CFAM through CometLib.
+
* Establishes a connection with CFAM through CometLib.
   
   
-
Erases and creates a keyed file.
+
* Erases and creates a keyed file.
   
   
-
Writes some records to the file.
+
* Writes some records to the file.
   
   
-
Gets some information about the file such as first and last key.
+
* Gets some information about the file such as first and last key.
   
   
-
Reads some records from the file.
+
* Reads some records from the file.
   
   
-
Closes the file and terminates the connection with CometLib.
+
* Closes the file and terminates the connection with CometLib.
   
   
'''Comet MUST be running on the same computer as the Visual Basic program. The ONLY directories that are accessible are those designated as type C directories in the Comet INI file.'''
'''Comet MUST be running on the same computer as the Visual Basic program. The ONLY directories that are accessible are those designated as type C directories in the Comet INI file.'''
Line 21: Line 21:
Here is a step-by-step guide that will get a sample program running on your own system.  
Here is a step-by-step guide that will get a sample program running on your own system.  
-
Open Visual Basic 6.0  
+
*1 Open Visual Basic 6.0  
-
Click on New Project  
+
*2 Click on New Project  
-
Click on Standard EXE and click OK  
+
*3 Click on Standard EXE and click OK  
-
You should get a small dialog grid on your screen.  
+
*4 You should get a small dialog grid on your screen.  
-
Click on the menu item view, and click toolbox to bring up the control builder.  
+
*5 Click on the menu item view, and click toolbox to bring up the control builder.  
-
In the toolbox, click on Button.  
+
*6 In the toolbox, click on Button.  
-
On the dialog grid, drag your mouse to make a button in the center. By default it should be called Command1 when you let go of the mouse.  
+
*7 On the dialog grid, drag your mouse to make a button in the center. By default it should be called Command1 when you let go of the mouse.  
-
Double click on the button you created. You should see a window with code in it. In particular, you should see this:  
+
*9 Double click on the button you created. You should see a window with code in it. In particular, you should see this:  
-
Private Sub Command1_Click()  
+
Private Sub Command1_Click()  
-
End Sub  
+
End Sub  
 +
 
 +
*10 Under the project menu, click References.
 +
*11 Scroll down and check “CometLib 1.0 Type Library”, and click OK.
-
Under the project menu, click References.
 
-
Scroll down and check “CometLib 1.0 Type Library”, and click OK.
 
Use the clipboard to copy the following code between the Private Sub line and the End Sub line.  
Use the clipboard to copy the following code between the Private Sub line and the End Sub line.  
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Line 136: Line 137:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-
That is the whole program.  
+
That is the whole program.
-
On the Build Menu, select Build solution. This will compile the project.  
+
-
Now, let’s go through this program in some finer detail.  
+
On the Build Menu, select Build solution. This will compile the project.
-
The first two lines make a Comet File System object.  
+
-
Dim f As CometLib.CometFiles  
+
Now, let’s go through this program in some finer detail.
-
Set f = New CometLib.CometFiles  
+
 +
The first two lines make a Comet File System object.
 +
 +
Dim f As CometLib.CometFiles  
 +
Set f = New CometLib.CometFiles  
 +
 
You only need one of these objects in your program. I just called the object f, but you could call it anything you want. From this point on, anything you want to do with the Comet File System, you proceed with “f.”. These two statements really belong outside of a click event. They really establish the connection to the object and this should happen when the application starts up. I put them inside of a click event for clarity only.  
You only need one of these objects in your program. I just called the object f, but you could call it anything you want. From this point on, anything you want to do with the Comet File System, you proceed with “f.”. These two statements really belong outside of a click event. They really establish the connection to the object and this should happen when the application starts up. I put them inside of a click event for clarity only.  
The next line:  
The next line:  
-
f.Initialize("Hello-World")  
+
f.Initialize("Hello-World")  
-
Tells CometLib to actually make the connection with CFAM and uses the string “Hello-World” to identify this connection. Any short string of characters will do, but don’t use embedded blanks in the name.  
+
 
 +
Tells CometLib to actually make the connection with CFAM and uses the string “Hello-World” to identify this connection. Any short string of characters will do, but don’t use embedded blanks in the name.
 +
Most of the code from here should be easy to decipher. If you use the object browser, you can see all of the methods that are available. We think it is rather complete.  
Most of the code from here should be easy to decipher. If you use the object browser, you can see all of the methods that are available. We think it is rather complete.  
Most methods return a Boolean. This may be used as an indication of a comet error. If the call is not placed in an if statement, the result is thrown away as in the erase call.  
Most methods return a Boolean. This may be used as an indication of a comet error. If the call is not placed in an if statement, the result is thrown away as in the erase call.  
-
f.Erase file, fdir  
+
f.Erase file, fdir  
 +
 
In this case I chose to ignore errors. The following create call  
In this case I chose to ignore errors. The following create call  
-
If f.CreateKeyedFile(file, fdir, 65, 34) = False Then  
+
 
-
excp = "Create "  
+
If f.CreateKeyedFile(file, fdir, 65, 34) = False Then  
-
GoTo Bad  
+
excp = "Create "  
-
End If  
+
GoTo Bad  
-
Will Goto the label “Bad:” if there were some sort of comet error on the create.  
+
End If
 +
 +
Will Goto the label “Bad:” if there were some sort of comet error on the create.
 +
Other methods such as the key methods return strings.  
Other methods such as the key methods return strings.  
-
key = f.FirstKey(lun)  
+
key = f.FirstKey(lun)  
-
Output = Output & "First key = " & key & vbNewLine  
+
Output = Output & "First key = " & key & vbNewLine  
-
key = f.NextKey(lun)  
+
key = f.NextKey(lun)  
-
Output = Output & "Next key = " & key & vbNewLine  
+
Output = Output & "Next key = " & key & vbNewLine  
-
key = f.PrevKey(lun)  
+
key = f.PrevKey(lun)  
-
Output = Output & "Prev key = " & key & vbNewLine  
+
Output = Output & "Prev key = " & key & vbNewLine  
-
key = f.LastKey(lun)  
+
key = f.LastKey(lun)  
-
Output = Output & "Last key = " & key & vbNewLine  
+
Output = Output & "Last key = " & key & vbNewLine  
-
If the string is empty, It may be assumed that the program has reached the end of file, or beginning of file, or there are no keys in the file depending on which method is called.  
+
 
 +
If the string is empty, It may be assumed that the program has reached the end of file, or beginning of file, or there are no keys in the file depending on which method is called.
 +
The last section to understand is how to properly shutdown the program. This is accomplished at the label “Done:”  
The last section to understand is how to properly shutdown the program. This is accomplished at the label “Done:”  
-
Done:  
+
Done:  
 +
 
 +
On Error Resume Next      ' No errors allowed here
 +
f.Close(lun)
 +
f.Terminate
 +
set f = Nothing
 +
End
-
On Error Resume Next      ' No errors allowed here
 
-
f.Close(lun)
 
-
f.Terminate
 
-
set f = Nothing
 
-
End
 
Here your program should close all open files, Terminate the CometLib connection and clear the value of the object. This code should be executed when your application ends.
Here your program should close all open files, Terminate the CometLib connection and clear the value of the object. This code should be executed when your application ends.

Revision as of 12:07, 24 May 2009

Cometlib -- The Open Interface to the Comet File System.

VB6.0 Sample.

This document will show you how to build a small VB program that:

  • Establishes a connection with CFAM through CometLib.
  • Erases and creates a keyed file.
  • Writes some records to the file.
  • Gets some information about the file such as first and last key.
  • Reads some records from the file.
  • Closes the file and terminates the connection with CometLib.

Comet MUST be running on the same computer as the Visual Basic program. The ONLY directories that are accessible are those designated as type C directories in the Comet INI file.

Here is a step-by-step guide that will get a sample program running on your own system.

  • 1 Open Visual Basic 6.0
  • 2 Click on New Project
  • 3 Click on Standard EXE and click OK
  • 4 You should get a small dialog grid on your screen.
  • 5 Click on the menu item view, and click toolbox to bring up the control builder.
  • 6 In the toolbox, click on Button.
  • 7 On the dialog grid, drag your mouse to make a button in the center. By default it should be called Command1 when you let go of the mouse.
  • 9 Double click on the button you created. You should see a window with code in it. In particular, you should see this:
Private Sub Command1_Click() 
End Sub 
  • 10 Under the project menu, click References.
  • 11 Scroll down and check “CometLib 1.0 Type Library”, and click OK.

Use the clipboard to copy the following code between the Private Sub line and the End Sub line.


Dim f As CometLib.CometFiles On Error GoTo CantInit

Set f = New CometLib.CometFiles

f.Initialize ("Hello")

On Error Resume Next

Dim lun As Integer Dim file As String Dim fdir As String Dim record As String Dim key As String Dim func As String

Dim i As Integer Dim last As String

Dim Output As String

lun = 43 file = "hello" fdir = "tmp"

' Create the file

f.Erase file, fdir

If f.CreateKeyedFile(file, fdir, 65, 34) = False Then

func = "Create " GoTo Bad End If f.Close (lun)

If f.Open(lun, file, fdir) = False Then

func = "Open " GoTo Bad End If ' Build the file

For i = 111 To 222

f.WriteInit (lun) record = "Hello World, this is record " f.PutRecField lun, record, 0, 30 f.PutRecField lun, i, 30, 5 key = "** " & i f.Write lun, key Next i

    ' Test the file -- get some info from it 

f.Position lun, "** 140"

key = f.FirstKey(lun) Output = Output & "First key = " & key & vbNewLine key = f.NextKey(lun) Output = Output & "Next key = " & key & vbNewLine key = f.PrevKey(lun) Output = Output & "Prev key = " & key & vbNewLine key = f.LastKey(lun) Output = Output & "Last key = " & key & vbNewLine

For i = 1 To 10

key = f.NextKey(lun) If key = "" Then func = "Nextkey" GoTo Bad End If If f.ReadNext(lun) = False Then func = "Read " GoTo Bad End If record = f.GetRecField(lun, 0, 65) Output = Output & RTrim(key) & " -- " & RTrim(record) & vbNewLine Next i MsgBox (Output)

On Error Resume Next ' No errors allowed here f.Close (lun) f.Terminate set f = Nothing End

Bad:

last = f.GetCometExcpString(f.LastCometError) MsgBox (func & "got func " & f.LastCometError & " -- " & last) End CantInit:

If MsgBox("Error loading CometLib, make sure Comet is loaded and click Retry. Click Cancel to exit.", vbRetryCancel, "Is Comet Loaded?") = vbRetry Then Resume End


That is the whole program.

On the Build Menu, select Build solution. This will compile the project.

Now, let’s go through this program in some finer detail.

The first two lines make a Comet File System object.

Dim f As CometLib.CometFiles 
Set f = New CometLib.CometFiles 

You only need one of these objects in your program. I just called the object f, but you could call it anything you want. From this point on, anything you want to do with the Comet File System, you proceed with “f.”. These two statements really belong outside of a click event. They really establish the connection to the object and this should happen when the application starts up. I put them inside of a click event for clarity only. The next line:

f.Initialize("Hello-World") 

Tells CometLib to actually make the connection with CFAM and uses the string “Hello-World” to identify this connection. Any short string of characters will do, but don’t use embedded blanks in the name.

Most of the code from here should be easy to decipher. If you use the object browser, you can see all of the methods that are available. We think it is rather complete.

Most methods return a Boolean. This may be used as an indication of a comet error. If the call is not placed in an if statement, the result is thrown away as in the erase call.

f.Erase file, fdir 

In this case I chose to ignore errors. The following create call

If f.CreateKeyedFile(file, fdir, 65, 34) = False Then 
excp = "Create " 
GoTo Bad 
End If

Will Goto the label “Bad:” if there were some sort of comet error on the create.

Other methods such as the key methods return strings.

key = f.FirstKey(lun) 
Output = Output & "First key = " & key & vbNewLine 
key = f.NextKey(lun) 
Output = Output & "Next key = " & key & vbNewLine 
key = f.PrevKey(lun) 
Output = Output & "Prev key = " & key & vbNewLine 
key = f.LastKey(lun) 
Output = Output & "Last key = " & key & vbNewLine 

If the string is empty, It may be assumed that the program has reached the end of file, or beginning of file, or there are no keys in the file depending on which method is called.

The last section to understand is how to properly shutdown the program. This is accomplished at the label “Done:”

Done: 
On Error Resume Next       ' No errors allowed here 
f.Close(lun) 
f.Terminate 
set f = Nothing 
End 

Here your program should close all open files, Terminate the CometLib connection and clear the value of the object. This code should be executed when your application ends.

Personal tools