HOWTO: Create a 3-Tier App using VB, MTS and SQL ServerID: Q186342
|
This article describes how to create a Visual Basic database application,
which uses components that are installed under Microsoft Transaction Server
(MTS). The steps in the MORE INFORMATION section outline what is necessary to set up the database application using Visual Basic (VB) and MTS.
This article assumes that you are running MTS 2.0 and SQL Server 6.5 on the
same computer on which you develop the application.
To set up an application that uses remote components running under the control of MTS there are four basic steps:
Option Explicit
Public Function Database_Test_Method( _
ByVal szConnect As String) As String
On Error GoTo errhandler
Dim ctxObject As ObjectContext
Set ctxObject = GetObjectContext()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cn = New ADODB.Connection
With cn
.ConnectionTimeout = 10
.ConnectionString = szConnect
.Open
End With
Set rs = New ADODB.Recordset
rs.Open "SELECT * FROM Authors", cn, adOpenForwardOnly, _
adLockReadOnly
Database_Test_Method = "First value from database: " _
& rs(0).Value
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
ctxObject.SetComplete
Exit Function
errhandler:
ctxObject.SetAbort
Err.Raise vbObjectError, "MTSTest.Class1.Database_Test_Method", _
Err.Description
Exit Function
End Function
Public Function General_Test_Method() As String
On Error GoTo errhandler
Dim ctxObject As ObjectContext
Set ctxObject = GetObjectContext()
General_Test_Method = "String from General_Test_Method"
ctxObject.SetComplete
Exit Function
errhandler:
ctxObject.SetAbort
Err.Raise vbObjectError, "MTSTest.Class1.General_Test_Method", _
Err.Description
Exit Function
End Function
Private Sub Command1_Click()
Dim obj As Object
Set obj = CreateObject("MTSTest.Class1")
MsgBox obj.General_Test_Method
Set obj = Nothing
End Sub
Private Sub Command2_Click()
Dim szConnect As String
szConnect = "Driver={SQL Server};" & _
"Server=YourServerName;Uid=sa;Pwd=;Database=pubs"
'(NOTE: You may need to change the connection
' information to connect to your database.)
Dim obj As Object
Set obj = CreateObject("MTSTest.Class1")
MsgBox obj.Database_Test_Method(szConnect)
Set obj = Nothing
End Sub
Private Sub Form_Load()
Command1.Caption = "Call General_Test_Method"
Command2.Caption = "Call Database_Test_Method"
End Sub
MTS 2.0 Online Help
For additional information about distributing Visual Basic Clients using the Visual Basic setup wizard, please see the following
article(s) in the Microsoft Knowledge Base:
Q161837 HOWTO: Create a DCOM Client/Server Application
Q177394 HOWTO: Troubleshoot Run-Time Error '429' in DCOM Applications
Q172869 HOWTO: Debug a Component Written with Visual Basic 5.0
Additional query words:
Keywords : kbADO200 kbDatabase kbDCOM kbMTS kbVBp600
Version : WINDOWS:1.5
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: May 27, 1999