![]() |
|||||||
|
Database Operation Services (continued) The following is a set of code that uses the mvDatabase object from dbObjects to read a set of customer records and load them into a grid control within a Visual Basic program. This is the same example program used earlier except that the mvDatabase object is used instead of the VDD object and generic Database object. There are four procedures that were coded for this example application, the Form_Load event procedure, the cmdExecute_Click button control event procedure, the cmdExit_Click button control event procedure, and the Form_QueryUnload event procedure. The code in black type is what we want to focus on. Compare this code with the earlier example and you will see how much easier the mvDatabase object is to work with. Option Explicit
Private
Sub Form_Load() 'Purpose
Form Load Event handler. Define needed mvDatabase database '
object and open connection to database. 'Inputs
Void 'Outputs
Void 'Revisions
'Show form
Me.Show
Me.Refresh
lblStatus.Caption = "Creating database object..."
'Create database object
Set mMvDb = New
MVDatabase
'Set which Vdd object to use
mMvDb.VddName =
"DemoMvBase.vdd"
lblStatus.Caption = "Now opening database
source..."
'Open a connection to the database source
mMvDb.ShowSplash = False
mMvDb.ShowLogin = False
mMvDb.Connect App
'Setup grid
msgCustomers.ColWidth(0) = 900
msgCustomers.ColWidth(1) = 2400
msgCustomers.ColWidth(2) = 1100
msgCustomers.TextMatrix(0, 0) = "Cust ID"
msgCustomers.TextMatrix(0, 1) = "Name"
msgCustomers.TextMatrix(0, 2) = "City"
msgCustomers.Row = 0
msgCustomers.Col = 0
msgCustomers.CellAlignment = 4
msgCustomers.Col = 1
msgCustomers.CellAlignment = 4
msgCustomers.Col = 2
msgCustomers.CellAlignment = 4
lblStatus.Caption = ""
cmdExecute.Enabled = True
Exit Sub
ErrorHandler:
If Err.Number < 0 Then Err.Number = Err.Number -
vbObjectError
'If Err.Number <> 52 Then
MsgBox "Error: " & LTrim(Str(Err.Number)) &
" was generated by " _
& Err.Source & vbCr & Err.Description, , _
"Customer List", _
Err.HelpFile, _
Err.HelpContext
'End If
Set mMvDb =
Nothing
Set frmMain = Nothing
End End
Sub Private
Sub cmdExecute_Click() 'Purpose
Command button Click Event handler. Execute database '
stored procedure. Load output into grid control. 'Inputs
Void 'Outputs
Void 'Revisions
Dim strCustList As String
Dim dsaCustList As New DynamicArray
Dim lngCnt As Long
Dim i As Long
On Error GoTo ErrorHandler
'Clear grid
msgCustomers.Rows = 1
'Execute stored procedure
mMvDb.SendData = ""
If mMvDb.Execute("CUSTLIST", False)
Then
strCustList =
mMvDb.ReturnData
Else
strCustList =
""
End If
'Create temporary dynamic string array for customer list
dsaCustList.Create
strCustList, vxRM, vxAM
'Load returned customer list into grid control
'Change attribute marks to tabs for grid
dsaCustList.Delimiter(2) = vbTab
lngCnt = dsaCustList.Count
For i = 1 To lngCnt
msgCustomers.AddItem
dsaCustList.Extract(i)
Next i
'Clear object reference
Set dsaCustList =
Nothing
Exit Sub ErrorHandler:
If Err.Number < 0 Then Err.Number = Err.Number -
vbObjectError
MsgBox "Error: " & LTrim(Str(Err.Number)) &
" was generated by " _
& Err.Source & vbCr & Err.Description, , _
"Customer List", _
Err.HelpFile, _
Err.HelpContext
Resume Next End
Sub Private
Sub cmdExit_Click() 'Purpose
Exit command button Click Event handler. Unload form and End
program. 'Inputs
Void 'Outputs
Void 'Revisions
On Error Resume Next
Unload frmMain
End
End
Sub Private
Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) 'Purpose
Form QueryUnload Event handler. Close database connection. '
Clear object reference and reference to form. 'Inputs
Void 'Outputs
Void 'Revisions
On Error Resume Next
'Close Database session
mMvDb.CloseConnection
'Clear all references
Set mMvDb = Nothing
Set frmMain = Nothing
End
Sub
For additional information or to order a copy of mvComponents
Suite 2.0 and arrange for a consultant, please send an E-mail to: For VAR requirements and pricing please send E-mail to:
|
|||||||