Example

This example program provides the framework for most DB-Library for Visual Basic applications. The example uses sample subroutine names for Visual Basic event procedures with which this code might be associated.

SUB Form_Load()
   'Initialize VBSQL.
   IF SqlInit$() = "" THEN
      PRINT "VBSQL has not been initialized."
      END
   END IF
END SUB

SUB LoginCmd_Click()
   'Get a Login record and set login attributes.
   Login% = SqlLogin%()
   loginid = LoginIDText.Text
   passwd = PasswordText.Text
   example = ExampleText.Text
   Result% = SqlSetLUser%(Login%, loginid)
   Result% = SqlSetLPwd%(Login%, passwd)
   Result% = SqlSetLApp%(Login%, example)

   'Get a connection for communicating with SQL Server.
   server = ServerText.Text
   Sqlconn% = SqlOpen%(Login%, server)
END SUB 

SUB ExecuteCmd_Click()
   'Retrieve two columns from the "authors" table
   'in the "pubs" database.

   'Put the command into the command buffer.
   Result% = SqlCmd%(Sqlconn%, "SELECT au_lname, city")
   Result% = SqlCmd%(Sqlconn%, " FROM pubs..authors")
   Result% = SqlCmd%(Sqlconn%, " WHERE state = 'CA'")

   'Send the command to SQL Server and start execution.
   Result% = SqlExec%(Sqlconn%)
   Result% = SqlResults%(Sqlconn%)

   'Process the command.
   IF Result% = SUCCEED THEN
      'Retrieve and print the result rows.
      PRINT
      DO UNTIL SqlNextRow%(Sqlconn%) = NOMOREROWS
         Name$ = SqlData$(Sqlconn%, 1)
         City$ = SqlData$(Sqlconn%, 2)
         PRINT Name$, City$
      LOOP

   END IF
END SUB 

SUB QuitApp_Click()
   'Close connection and exit program.
   SqlExit
   SqlWinExit
   END
END SUB

Sub VBSQL1_Error (Sqlconn%, Severity%, ErrorNum%, ErrorStr$, RetCode%)
   MsgBox ("DB-Library Error: "  Str$(ErrorNum%)  " "  ErrorStr$)
END SUB

Sub VBSQL1_Message (Sqlconn%, Message&, State%, Severity%, MsgStr$)
   MsgBox ("SQL Server Error: "  Str$(Message&)  " "  MsgStr$)
END SUB