Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 7

Accessing SQL Server through DMO Sample #1

Page

Accessing SQL Server through DMO Sample


This sample demonstrates how to access SQL Server 6.5 using DMO . This sample is written in Visual Basic. This sample contains code for accessing Customer information.

SQLGeneral.BAS ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'This is a sample for connecting Visual Basic & SQL Server 6.5 'Using DMO 'Author : Raja B ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Option Explicit Public oSQLServer As New SQLOLE.SQLServer Public oDatabase As SQLOLE.Database This is your SQL Server ( C_BRAIN) This is the Database to use

MdiMain.frm Option Explicit Private Sub MDIForm_QueryUnload(Cancel As Integer, UnloadMode As Integer) oSQLServer.DisConnect Cancel = False End Sub Private Sub mnuExit_Click() End End Sub Private Sub mnuProcess_Click(Index As Integer) Select Case Index Case 1: frmCustomer.Show Case 2: frmProduct.Show Case 3 frmInvoice.Show End Select End Sub

This sample contains code only for this part.

K7 Computing

Raja.B

Accessing SQL Server through DMO Sample #2

Page

K7 Computing

Raja.B

Accessing SQL Server through DMO Sample #3


FrmLogin.frm This is your statup form. This is not a Mdichild form. Option Explicit Private Sub cmdCancel_Click() End End Sub Private Sub cmdOK_Click() If (ConnectToServer()) Then Unload Me mdiMain.Show End If End Sub Private Function ConnectToServer() As Boolean This function makes connection to you SQL Server [C_BRAIN is my Server] And makes OfficeAutomation database for use. Dim nMousePointer As Integer nMousePointer = Screen.MousePointer Screen.MousePointer = vbHourglass On Error Resume Next If (Trim(txtUserName.Text) <> vbNullString) Then oSQLServer.LoginTimeout = 10 Call oSQLServer.Connect("C_BRAIN", Trim(txtUserName.Text), Trim(txtPassword.Text)) If (Err <> 0) Then MsgBox "Connection to the SQL Server failed..." ConnectToServer = False Else ConnectToServer = True Set oDatabase = oSQLServer.Databases("OfficeAutomation") End If Else MsgBox "Please enter the UserName..." ConnectToServer = False End If Screen.MousePointer = nMousePointer Exit Function End Function

Page

K7 Computing

Raja.B

Accessing SQL Server through DMO Sample #4


frmCustomer.frm ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'This is a sample for connecting Visual Basic & SQL Server 6.5 'Using DMO 'Author : Raja B ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Option Explicit Private Private Private Private Private Private Private bAddMode As Boolean nCurrentRow As Integer bEnableNavigate As Boolean qryResult As SQLOLE.QueryResults nCustomerID As Integer nRows As Integer nCols As Integer

Page

Private Sub cmdAdd_Click() Call ClearAllTextBoxes bAddMode = True cmdAdd.Visible = False cmdOK.Visible = True cmdEdit.Visible = False cmdDelete.Visible = False EnableNavigation (False) End Sub Private Sub cmdDelete_Click() This function will delete the current record after the confirmation Dim strSQLDelete As String Dim nReturn As Integer nReturn = MsgBox("Are you sure to Delete this current Record..?" & _ vbCrLf & "Press 'Yes' to Delete , 'No' to Cancel the operation", vbYesNo, "Confirmation") If (nReturn = vbYes) Then strSQLDelete = "DELETE FROM tblCustomer WHERE CustomerID = " & CStr(nCustomerID) Call oDatabase.ExecuteImmediate(Command:=strSQLDelete) qryResult.Refresh nCols = qryResult.Columns nRows = qryResult.Rows If (nRows > 0) Then nCurrentRow = 1 bEnableNavigate = True EnableNavigation (True) Call ShowCustomerAddress Else EnableNavigation (False) bEnableNavigate = False End If End If End Sub

K7 Computing

Raja.B

Accessing SQL Server through DMO Sample #5


Private Sub cmdEdit_Click() bAddMode = False cmdAdd.Visible = False cmdEdit.Visible = False cmdOK.Visible = True cmdDelete.Visible = False EnableNavigation (False) End Sub Private Sub cmdFirst_Click() If (bEnableNavigate) Then nCurrentRow = 1 Call ShowCustomerAddress End If End Sub Private Sub cmdLast_Click() If (bEnableNavigate) Then nCurrentRow = nRows Call ShowCustomerAddress End If End Sub Private Sub cmdNext_Click() If (bEnableNavigate) Then nCurrentRow = IIf(nCurrentRow < nRows, nCurrentRow + 1, nCurrentRow) Call ShowCustomerAddress End If End Sub Private Sub cmdOK_Click() Add a New Customer or Edit the currrent customer..? Dim strSQLInsertCustomer As String Call oDatabase.ExecuteImmediate(Command:="BEGIN TRANSACTION CustUpdate") If (bAddMode) Then 'sp_AddCustomer is a Stored Procedure in OfficeAutomation database strSQLInsertCustomer = "sp_AddCustomer " & _ "'" & Trim(txtName.Text) & "'," & _ "'" & Trim(txtAddress.Text) & "'," & _ "'" & Trim(txtCity.Text) & "'," & _ "'" & Trim(txtState.Text) & "'," & _ "'" & Trim(txtPincode.Text) & "'" Else strSQLInsertCustomer = "UPDATE tblCustomer SET " & _ "CustomerName = """ & txtName.Text & """," & _ "Address = """ & txtAddress.Text & """," & _ "City = """ & txtCity.Text & """," & _ "State = """ & txtState.Text & """," & _ "PinCode = """ & txtPincode.Text & """ WHERE CustomerID = " & _ CStr(nCustomerID) End If Call oDatabase.ExecuteImmediate(Command:=strSQLInsertCustomer) Call oDatabase.ExecuteImmediate(Command:="COMMIT TRANSACTION")

Page

K7 Computing

Raja.B

Accessing SQL Server through DMO Sample #6


qryResult.Refresh nCols = qryResult.Columns nRows = qryResult.Rows cmdAdd.Visible = True cmdEdit.Visible = True cmdOK.Visible = False cmdDelete.Visible = True EnableNavigation (True) End Sub Private Sub cmdPrev_Click() If (bEnableNavigate) Then nCurrentRow = IIf(nCurrentRow <> 1, nCurrentRow - 1, nCurrentRow) ShowCustomerAddress End If End Sub Private Sub Form_Load() cmdOK.Visible = False cmdAdd.Visible = True cmdEdit.Visible = True Call ExecuteQuery End Sub Private Sub EnableNavigation(bEnable As Boolean) cmdFirst.Enabled = bEnable cmdPrev.Enabled = bEnable cmdNext.Enabled = bEnable cmdLast.Enabled = bEnable End Sub Public Sub ShowCustomerAddress() Dim nColLoop As Integer For nColLoop = 1 To nCols Select Case qryResult.ColumnName(nColLoop) Case "CustomerID" nCustomerID = qryResult.GetColumnString(nCurrentRow, nColLoop) Case "CustomerName" txtName.Text = Trim(qryResult.GetColumnString(nCurrentRow, nColLoop)) Case "Address" txtAddress.Text = Trim(qryResult.GetColumnString(nCurrentRow, nColLoop)) Case "City" txtCity.Text = Trim(qryResult.GetColumnString(nCurrentRow, nColLoop)) Case "State" txtState.Text = Trim(qryResult.GetColumnString(nCurrentRow, nColLoop)) Case "PinCode" txtPincode.Text = Trim(qryResult.GetColumnString(nCurrentRow, nColLoop)) End Select Next nColLoop End Sub

Page

Private Sub ClearAllTextBoxes() txtName.Text = vbNullString txtAddress.Text = vbNullString

K7 Computing

Raja.B

Accessing SQL Server through DMO Sample #7


txtCity.Text = vbNullString txtState.Text = vbNullString txtPincode.Text = vbNullString End Sub Private Sub ExecuteQuery() Dim strSQLCustomer As String On Error GoTo TRAP1: strSQLCustomer = "SELECT * FROM tblCustomer" Set qryResult = oDatabase.ExecuteWithResults(Command:=strSQLCustomer) nCols = qryResult.Columns nRows = qryResult.Rows If (nRows > 0) Then nCurrentRow = 1 bEnableNavigate = True EnableNavigation (True) Call ShowCustomerAddress Else EnableNavigation (False) bEnableNavigate = False End If Exit Sub TRAP1: MsgBox "Error in Query" End Sub

Page

This sample is written in Visual Basic 5.0 Here frmLogim.frm is the ultimate code for loging into your SQL Server. [Here the Server name in coded as C_BRAIN]. Code in frmCustomer.frm demonstrates record navigation, adding a new record , editing the Current record and delete the current record.

For More details contact: Mullai manavalan . P.N.S Raja. B

Raja. B for K7 Computing

K7 Computing

Raja.B

You might also like