home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Unleashed / Visual_Basic_4_Unleashed_SAMS_Publishing_1995.iso / source / chap29 / frm329.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-08-31  |  4.9 KB  |  166 lines

  1. VERSION 4.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "Form1"
  4.    ClientHeight    =   5940
  5.    ClientLeft      =   1140
  6.    ClientTop       =   1515
  7.    ClientWidth     =   6690
  8.    Height          =   6345
  9.    Left            =   1080
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   5940
  12.    ScaleWidth      =   6690
  13.    Top             =   1170
  14.    Width           =   6810
  15.    Begin VB.CommandButton Command2 
  16.       Caption         =   "test"
  17.       Height          =   495
  18.       Left            =   780
  19.       TabIndex        =   2
  20.       Top             =   1500
  21.       Width           =   1215
  22.    End
  23.    Begin VB.CommandButton Command1 
  24.       Caption         =   "Command1"
  25.       Height          =   495
  26.       Left            =   3060
  27.       TabIndex        =   1
  28.       Top             =   4140
  29.       Width           =   1215
  30.    End
  31.    Begin VB.CommandButton cmdOpenOdbc 
  32.       Caption         =   "Command1"
  33.       Height          =   495
  34.       Left            =   1140
  35.       TabIndex        =   0
  36.       Top             =   4140
  37.       Width           =   1215
  38.    End
  39. Attribute VB_Name = "Form1"
  40. Attribute VB_Creatable = False
  41. Attribute VB_Exposed = False
  42. Option Explicit
  43. Dim nenv As Long
  44. Dim nconnect As Long
  45. Const SQL_C_CHAR = 1
  46. Sub OpenOdbcSource()
  47. Dim nenv As Long
  48. Dim nok As Integer
  49. Dim cdatasource As String
  50. Dim suserid As String
  51. Dim spassword As String
  52. Dim nsql As Long
  53. Dim csql As String
  54. Dim cdata As String
  55. Dim nlong As Long
  56. Dim nlength As Integer
  57. Dim size As Integer
  58. Dim cname As String
  59. Dim ntype As Integer
  60. Dim ndecimals As Integer
  61. Dim nscale As Integer
  62. Dim nnulls As Integer
  63. nok = SQLAllocEnv(nenv)
  64. If nok <> SQL_SUCCESS Then
  65.     MsgBox "An Error Occured During the Allocation of the ODBC Environment"
  66.     Exit Sub
  67. End If
  68. nok = SQLAllocConnect(nenv, nconnect)
  69. If nok <> SQL_SUCCESS Then
  70.     MsgBox "An Error Occured During the Allocation of ODBC Connections"
  71.     Exit Sub
  72. End If
  73. ' change the following 3 lines for your environment
  74. cdatasource = "BIBLIO"
  75. suserid = "ADMIN"
  76. spassword = "PASSWORD"
  77. nok = SQLConnect(nconnect, cdatasource, Len(cdatasource), suserid, Len(suserid), spassword, Len(spassword))
  78. If nok <> SQL_SUCCESS Then
  79.     MsgBox "An Error Occured During the Conection to the ODBC Database"
  80.     Exit Sub
  81. End If
  82. MsgBox " You have sucessfully connected to the ODBC Database"
  83. nok = SQLAllocStmt(nconnect, nsql)
  84. If nok <> SQL_SUCCESS Then
  85.     MsgBox "An Error Occured Allocating the SQL Results Handle"
  86.     Exit Sub
  87. End If
  88. csql = " Select * from currency"
  89. nok = SQLExecDirect(nsql, csql, Len(csql))
  90. If nok <> SQL_SUCCESS Then
  91.     MsgBox "An Error Occured Excuting the SQL Statment"
  92.     Exit Sub
  93. End If
  94. nok = SQLFetch(nsql)
  95. If nok <> SQL_SUCCESS Then
  96.     MsgBox "An Error Occured Obtaining the SQL Result Set"
  97.     Exit Sub
  98. End If
  99. nok = SQLDescribeCol(nsql, 2, cname, 15, size, ntype, ByVal ndecimals, nscale, nnulls)
  100. nok = SQLGetData(nsql, 2, ntype, cdata, size, nlong)
  101. nok = SQLGetData(nsql, 1, SQL_C_CHAR, cdata, 30, nlong)
  102. If nok <> SQL_SUCCESS Then
  103.     processerror nenv, nconnect, 0
  104.     MsgBox "An Error Occured Obtaining the Field Value"
  105.     Exit Sub
  106.     MsgBox "The Following data was retreived " * cdata
  107. End If
  108. End Sub
  109. Private Sub cmdOpenOdbc_Click()
  110. Dim nok As Integer
  111. Dim cdatasource As String
  112. Dim suserid As String
  113. Dim spassword As String
  114. ' change the following 3 lines for your environment
  115. cdatasource = "BIBLIO"
  116. suserid = "ADMIN"
  117. spassword = "PASSWORD"
  118. nok = SQLAllocEnv(nenv)
  119. If nok <> SQL_SUCCESS Then ''''
  120.     processerror nenv, nconnect, 0
  121.     Exit Sub
  122. End If
  123. nok = SQLAllocConnect(nenv, nconnect)
  124. If nok <> SQL_SUCCESS Then
  125.     processerror nenv, nconnect, 0
  126.     Exit Sub
  127. End If
  128. nok = SQLConnect(nconnect, cdatasource, Len(cdatasource), suserid, Len(suserid), spassword, Len(spassword))
  129. If nok <> SQL_SUCCESS Then
  130.     processerror nenv, nconnect, 0
  131.     Exit Sub
  132. End If
  133. End Sub
  134. Private Sub Command1_Click()
  135. Dim nok As Integer
  136. nok = SQLDisconnect(nconnect)
  137. If nok < SQL_SUCCESS Then
  138.     processerror nenv, nconnect, 0
  139.     Exit Sub
  140. End If
  141. nok = SQLFreeConnect(nconnect)
  142. If nok < SQL_SUCCESS Then
  143.     processerror nenv, nconnect, 0
  144.     Exit Sub
  145. End If
  146. nok = SQLFreeEnv(nenv)
  147. If nok < SQL_SUCCESS Then
  148.     processerror nenv, nconnect, 0
  149.     Exit Sub
  150. End If
  151. End Sub
  152. Sub processerror(nenv As Long, nconnect As Long, nstatement As Long)
  153. Dim nok As Integer
  154. Dim csqlstate As String
  155. Dim snativeerror As Long
  156. Dim serrormessage As String * 255
  157. Dim nerrormsg As Integer
  158. nok = SQLError(nenv, nconnect, nstatement, csqlstate, snativeerror, serrormessage, Len(serrormessage), nerrormsg)
  159. MsgBox " the following Error" & serrormessage & " occurred Number " & snativeerror
  160. End Sub
  161. Private Sub Command2_Click()
  162. OpenOdbcSource
  163. End Sub
  164. Private Sub Form_Load()
  165. End Sub
  166.