| Algunos Trozos de Código Util |
| Existe
una enorme cantidad de código útil o "snippets" de VB por la
web, los siguientes son solo unos pocos ejemplos que he encontrado
útiles en su momento, lamentablemente no tengo las referencias
originales de donde los saqué, pero igual van los agradecimientos a sus
creadores.
Esta sección estará en constante actualización, conviene revisarla frecuentemente |
| Trabajar con Archivos INI |
| Se crea un
módulo funcini.bas con lo siguiente:
' DECLARACIONES API Sub InitProgram() |
| Cómo dar formato para celdas numéricas en el control DBGRID |
A las columnas numéricas de un dbgrid se les puede aplicar un formato de visualización de los números. Para establecer este formato a través de código debe utilizarse la propiedad NumberFormat, pero en base al formato americano. Por ejemplo, imaginemos que la tercera columna en un dbgrid es una columna numérica en la que aparecen cifras de ventas con valores decimales, y queremos visualizar el punto para las unidades de millar y dos decimales tras la coma. Tenderemos a escribir el siguiente código:
Pues bien, el resultado será que, una vez ejecutado este código, esa columna aparecerá en blanco. La razón es que esta propiedad debe utilizarse en base al formato americano, o dicho de otro modo, el punto decimal debe ser una coma (",") y la separación de decimales un punto ("."). De esta forma, si utilizamos el siguiente código:
|
| Programación del Clipboard |
| : Copiar Texto Clipboard.Clear ' Limpiamos el destino Clipboard.SetText Text1.SelText ' Copiamos el texto seleccionado Text1.SetFocus
O usar los procesos implicitos de Windows, estas líneas ahorran mucho código, pero puede tener un resultado no deseado. |
| Activar la Selección al Recibir el Foco |
En el evento GotFocus del control debes poner: Text1.SelStart = 0 Text1.SelLength = Len(Text1) |
| Conversión Números a Letras |
Function NumerosALetras(Numero As Double) As String
Dim letras As String
Dim HuboCentavos As Boolean
Dim Decimales As Double
Decimales = Numero - Int(Numero)
Numero = Int(Numero)
Inicializar
letras = ""
Do
'*---> Validación si se pasa de 100 millones
If Numero >= 1000000000 Then
letras = "Error en Conversión a Letras"
Numero = 0
Decimales = 0
End If
'*---> Centenas de Millón
If (Numero < 1000000000) And (Numero >= 100000000) Then
If (Int(Numero / 100000000) = 1)_
And ((Numero - (Int(Numero / 100000000) * 100000000)) < 1000000) Then
letras = letras & "cien millones "
Else
letras = letras & Centenas(Int(Numero / 100000000))
If (Int(Numero / 100000000) <> 1) And (Int(Numero / 100000000) <> 5)_
And (Int(Numero / 100000000) <> 7) And (Int(Numero / 100000000) <> 9) Then
letras = letras & "cientos "
Else
letras = letras & " "
End If
End If
Numero = Numero - (Int(Numero / 100000000) * 100000000)
End If
'*---> Decenas de Millón
If (Numero < 100000000) And (Numero >= 10000000) Then
If Int(Numero / 1000000) < 16 Then
letras = letras & Decenas(Int(Numero / 1000000))
letras = letras & " millones "
Numero = Numero - (Int(Numero / 1000000) * 1000000)
Else
letras = letras & Decenas(Int(Numero / 10000000) * 10)
Numero = Numero - (Int(Numero / 10000000) * 10000000)
If Numero > 1000000 Then
letras = letras & " y "
End If
End If
End If
'*---> Unidades de Millón
If (Numero < 10000000) And (Numero >= 1000000) Then
If Int(Numero / 1000000) = 1 Then
letras = letras & " un millón "
Else
letras = letras & Unidades(Int(Numero / 1000000))
letras = letras & " millones "
End If
Numero = Numero - (Int(Numero / 1000000) * 1000000)
End If
'*---> Centenas de Millar
If (Numero < 1000000) And (Numero >= 100000) Then
If (Int(Numero / 100000) = 1) And ((Numero - (Int(Numero / 100000) * 100000)) < 1000) Then
letras = letras & "cien mil "
Else
letras = letras & Centenas(Int(Numero / 100000))
If (Int(Numero / 100000) <> 1) And (Int(Numero / 100000) <> 5)_
And (Int(Numero / 100000) <> 7) And (Int(Numero / 100000) <> 9) Then
letras = letras & "cientos "
Else
letras = letras & " "
End If
End If
Numero = Numero - (Int(Numero / 100000) * 100000)
End If
'*---> Decenas de Millar
If (Numero < 100000) And (Numero >= 10000) Then
If Int(Numero / 1000) < 16 Then
letras = letras & Decenas(Int(Numero / 1000))
letras = letras & " mil "
Numero = Numero - (Int(Numero / 1000) * 1000)
Else
letras = letras & Decenas(Int(Numero / 10000) * 10)
Numero = Numero - (Int((Numero / 10000)) * 10000)
If Numero > 1000 Then
letras = letras & " y "
Else
letras = letras & " mil "
End If
End If
End If
'*---> Unidades de Millar
If (Numero < 10000) And (Numero >= 1000) Then
If Int(Numero / 1000) = 1 Then
letras = letras & "un"
Else
letras = letras & Unidades(Int(Numero / 1000))
End If
letras = letras & " mil "
Numero = Numero - (Int(Numero / 1000) * 1000)
End If
'*---> Centenas
If (Numero < 1000) And (Numero > 99) Then
If (Int(Numero / 100) = 1) And ((Numero - (Int(Numero / 100) * 100)) < 1) Then
letras = letras & "cien "
Else
letras = letras & Centenas(Int(Numero / 100))
If (Int(Numero / 100) <> 1) And (Int(Numero / 100) <> 5)_
And (Int(Numero / 100) <> 7) And (Int(Numero / 100) <> 9) Then
letras = letras & "cientos "
Else
letras = letras & " "
End If
End If
Numero = Numero - (Int(Numero / 100) * 100)
End If
'*---> Decenas
If (Numero < 100) And (Numero > 9) Then
If Numero < 16 Then
letras = letras & Decenas(Int(Numero))
Numero = Numero - Int(Numero)
Else
letras = letras & Decenas(Int((Numero / 10)) * 10)
Numero = Numero - (Int((Numero / 10)) * 10)
If Numero > 0.99 Then
letras = letras & " y "
End If
End If
End If
'*---> Unidades
If (Numero < 10) And (Numero > 0.99) Then
letras = letras & Unidades(Int(Numero))
Numero = Numero - Int(Numero)
End If
Loop Until (Numero = 0)
'*---> Decimales
If (Decimales > 0) Then
letras = letras & " con "
letras = letras & Format(Decimales * 100, "00") & "/100"
Else
If (letras <> "Error en Conversión a Letras") And (Len(Trim(letras)) > 0) Then
letras = letras & " exactos"
End If
End If
NumerosALetras = letras
End Function
Function Centenas(VCentena As Double) As String
If VCentena = 1 Then
Centenas = Numeros(100)
Else
If VCentena = 5 Then
Centenas = Numeros(101)
Else
If VCentena = 7 Then
Centenas = letras & Numeros(102)
Else
If VCentena = 9 Then
Centenas = letras & Numeros(103)
Else
Centenas = Numeros(VCentena)
End If
End If
End If
End If
End Function
Function Unidades(VUnidad As Double) As String
Unidades = Numeros(VUnidad)
End Function
Function Decenas(VDecena As Double) As String
Decenas = Numeros(VDecena)
End Function
Sub Inicializar()
Numeros(0) = "cero"
Numeros(1) = "uno"
Numeros(2) = "dos"
Numeros(3) = "tres"
Numeros(4) = "cuatro"
Numeros(5) = "cinco"
Numeros(6) = "seis"
Numeros(7) = "siete"
Numeros(8) = "ocho"
Numeros(9) = "nueve"
Numeros(10) = "diez"
Numeros(11) = "once"
Numeros(12) = "doce"
Numeros(13) = "trece"
Numeros(14) = "catorce"
Numeros(15) = "quince"
Numeros(20) = "veinte"
Numeros(30) = "treinta"
Numeros(40) = "cuarenta"
Numeros(50) = "cincuenta"
Numeros(60) = "sesenta"
Numeros(70) = "setenta"
Numeros(80) = "ochenta"
Numeros(90) = "noventa"
Numeros(100) = "ciento"
Numeros(101) = "quinientos"
Numeros(102) = "setecientos"
Numeros(103) = "novecientos"
End Sub
|
| Simular Máscara Entrada de Fechas |
Este código lo uso para simular una mascara de entrada para fechas a medida que voy ingresando los numeros me agrega la barra (/) la propiedad MaxLength deberia ser = 10, se puede mejorar validando los datos para dias, meses y años. Private Sub Text1_KeyPress(KeyAscii As Integer) If Len(Text1) = 2 Then Text1 = Text1 & "/" Text1.SelStart = Len(Text1) ElseIf Len(Text1) = 5 Then Text1 = Text1 & "/" Text1.SelStart = Len(Text1) End If End Sub |
| Comprobar la Existencia de un Archivo |
| : Esta funcion devuelve verdadero o falso segun exista o no el archivo buscado Function ExisteArchivo(cArchivo As String) As Boolean ExisteArchivo = IIf(Dir$(cArchivo) = "", False, True) End Function |
| Envio de e-mail Desde VB |
1.- Adjuntar al proyecto los controles MAPI (Proyecto/Componentes y señalar Microsoft MAPI controls) 2.- En tu formulario, coloca los controles MAPISession y MAPIMessages 3.- Para enviar el mail: MAPISession1.UserName = "nombre del remitente"
|
| Funciones Para Programar el MAPI |
Los códigos siguientes muestran rutins para programar el MAPI de mode de enviar y recibir mensajes de correo electrónico automáticamente, comandados por programa. Tal como en el ejemplo anterior todas estas funciones necesitan tener instalados los MAPI controls en una form y tener funcionando algun cliente de correo MAPI predeterminado (como Outlook por ejemplo) Private Sub Command1_Click() Dim RetVal As Boolean Dim i As Long ' In a real application we'd check the return values to ensure all was well ' Start a session RetVal = AlterMailSession(Me, StartSession) ' Send a message with multiple addresses and attachments RetVal = CreateMailMessage(Me, "Test Message 1", " " & Chr(13) & "Test Contents" &_ Chr(13) & "More stuff") RetVal = MailMessageTo(Me, "elliot spencer", Primary) RetVal = MailMessageTo(Me, "elliot spencer", CC) RetVal = MailMessageTo(Me, "elliot spencer", Primary) RetVal = MailMessageTo(Me, "elliot spencer", CC) RetVal = MailMessageTo(Me, "elliot spencer", BlindCC) RetVal = AddAttachment(Me, "pq.doc", "c:\pq.doc", DataFile) RetVal = AddAttachment(Me, "pq.doc", "c:\pq.doc", OLEStatic) RetVal = SendMailMessage(Me) ' Abort this message RetVal = CreateMailMessage(Me, "Test Message 2", "Test Contents" & Chr(13) & "More_ stuff") RetVal = MailMessageTo(Me, "elliot spencer", CC) RetVal = AbortMailMessage(Me) ' Save this message RetVal = CreateMailMessage(Me, "Test Message 3", "Test Contents" & Chr(13) & "More_ stuff") RetVal = MailMessageTo(Me, "elliot spencer", Primary) RetVal = SaveMailMessage(Me) ' Stop the current session RetVal = AlterMailSession(Me, StopSession) End Sub -------------------------------------------------------------------------------- This code comes from part of my MAPIFunc module and mainly deals with sending rather than receiving email messages. The module was tested against Exchange on NT4 but should work on Win 95 - Outlook setups. ' ' Created by E.Spencer (elliot@spnc.demon.co.uk) - This code is public domain. ' Private Declare Function RegOpenKey Lib "AdvAPI32.dll" Alias "RegOpenKeyA" (ByVal hKey_ As Long, _ByVal lpSubKey As String, phkResult As Long) As Long Private Declare Function RegQueryValueEx Lib "AdvAPI32.dll" Alias "RegQueryValueExA"_ (ByVal hKey As Long, _ ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal_ lpData As String, _ lpcbData As Long) As Long Private Declare Function RegCloseKey Lib "AdvAPI32.dll" (ByVal hKey As Long) As Long Private Const HKEY_CURRENT_USER = &H80000001 Public Enum SessMode StartSession StopSession End Enum Public Enum AddrType Primary CC BlindCC End Enum Public Enum AttachType DataFile OLEEmbedded OLEStatic End Enum Public SStatus, MStatus As String ' Call this function to start and stop MAPI sessions ' Example :- MyBool = AlterMailSession(Me, StartSession) ' MyBool = AlterMailSession(Me, StopSession) ' MyBool will be true if operation succeeded ' First parameter is reference to form that contains MAPI message / session controls ' Second parameter is the required session mode - stop or start. Public Function AlterMailSession(ByRef FName As Form, Mode As SessMode) As Boolean AlterMailSession = True On Error GoTo SessError If Mode = StartSession Then ' Get the default exchange profile name FName.MAPISession1.UserName = ReadRegistry(HKEY_CURRENT_USER, _ "Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\", "DefaultProfile") ' If session is already open return immediately If SStatus = "Open" Then Exit Function ' Set up profile - Default for exchange FName.MAPISession1.UserName = "MS Exchange Settings" FName.MAPISession1.SignOn ' Start mail session FName.MAPIMessages1.SessionID = FName.MAPISession1.SessionID ' Allocate session ID to_ Mail holder SStatus = "Open" MStatus = "Ready" ElseIf Mode = StopSession Then ' If session is already closed return immediately If SStatus = "Closed" Then Exit Function FName.MAPISession1.SignOff ' End mail session FName.MAPIMessages1.SessionID = 0 SStatus = "Closed" MStatus = "NotReady" End If Exit Function SessError: SStatus = "Closed" MStatus = "NotReady" AlterMailSession = False End Function ' Call this function to start a new mail message ' Example :- MyBool = CreateMailMessage(Me, "Test Message", "Test Contents") ' MyBool will be true if operation succeeded ' First parameter is reference to form that contains MAPI message / session controls ' The second parameter is the message subject line. ' The third parameter is the message content text (embed Chr(13) for newlines) Public Function CreateMailMessage(ByRef FName As Form, Subject As String, Contents As String) As Boolean CreateMailMessage = False ' If session is not open return immediately If SStatus <> "Open" Then Exit Function CreateMailMessage = True On Error GoTo MessError FName.MAPIMessages1.Compose ' Start new message composition FName.MAPIMessages1.MsgSubject = Subject ' Insert message subject line FName.MAPIMessages1.MsgNoteText = Contents & Chr(13) & " " ' Insert message text MStatus = "Open" Exit Function MessError: MStatus = "NotReady" CreateMailMessage = False End Function ' Call this function to abort a mail message ' Example :- MyBool = AbortMailMessage(Me) ' MyBool will be true if operation succeeded ' First parameter is reference to form that contains MAPI message / session controls Public Function AbortMailMessage(ByRef FName As Form) As Boolean AbortMailMessage = False ' If session is not open return immediately If SStatus <> "Open" Then Exit Function ' If no current mail message then return immediately If MStatus <> "Open" Then Exit Function AbortMailMessage = True On Error GoTo MessError FName.MAPIMessages1.Delete (mapMessageDelete) MStatus = "Ready" Exit Function MessError: AbortMailMessage = False End Function ' Call this function to send a complete mail message ' Example :- MyBool = SendMailMessage(Me) ' MyBool will be true if operation succeeded ' First parameter is reference to form that contains MAPI message / session controls Public Function SendMailMessage(ByRef FName As Form) As Boolean Dim Tries As Integer SendMailMessage = False ' If session is not open return immediately If SStatus <> "Open" Then Exit Function ' If no current mail message then return immediately If MStatus <> "Open" Then Exit Function SendMailMessage = True On Error GoTo MessError Retry: FName.MAPIMessages1.Send MStatus = "Ready" Exit Function MessError: Tries = Tries + 1 If Tries < 10 Then GoTo Retry SendMailMessage = False End Function ' Call this function to save a complete mail message without sending it ' Example :- MyBool = SaveMailMessage(Me) ' MyBool will be true if operation succeeded ' First parameter is reference to form that contains MAPI message / session controls Public Function SaveMailMessage(ByRef FName As Form) As Boolean SaveMailMessage = False ' If session is not open return immediately If SStatus <> "Open" Then Exit Function ' If no current mail message then return immediately If MStatus <> "Open" Then Exit Function SaveMailMessage = True On Error GoTo MessError FName.MAPIMessages1.Save MStatus = "Ready" Exit Function MessError: SaveMailMessage = False End Function ' Call this function to address a mail message to a recipient ' Example :- MyBool = MailMessageTo(Me, "elliot spencer", Primary) ' MyBool will be true if operation succeeded. Supply display names from address book ' list - names will be resolved to addresses in the address book before being added to ' recipient list. ' First parameter is reference to form that contains MAPI message / session controls ' Second parameter is name of recipient (as displayed in address list) ' Third parameter is type of recipient Public Function MailMessageTo(ByRef FName As Form, ToName As String, AddrMode As AddrType) As Boolean MailMessageTo = False ' If session is not open return immediately If SStatus <> "Open" Then Exit Function ' If no current mail message then return immediately If MStatus <> "Open" Then Exit Function MailMessageTo = True On Error GoTo MessError FName.MAPIMessages1.RecipIndex = FName.MAPIMessages1.RecipCount ' Update count of recipients If AddrMode = Primary Then FName.MAPIMessages1.RecipType = 1 ' Set to primary recipient type If AddrMode = CC Then FName.MAPIMessages1.RecipType = 2 ' Set to carbon copy type If AddrMode = BlindCC Then FName.MAPIMessages1.RecipType = 3 ' Set to blind carbon copy type FName.MAPIMessages1.RecipDisplayName = ToName ' Display name as provided FName.MAPIMessages1.ResolveName ' Resolve display name to real address via address book Exit Function MessError: MailMessageTo = False End Function ' Call this function to address a mail message to a recipient ' Example :- MyBool = AddAttachment(Me, "Test File", "c:\test.txt", DataFile) ' MyBool will be true if operation succeeded. ' First parameter is reference to form that contains MAPI message / session controls ' Second parameter is name of recipient (as displayed in address list) ' Third parameter is type of recipient Public Function AddAttachment(ByRef FName As Form, AName As String, APath As String, AttMode As AttachType) As Boolean AddAttachment = False ' If session is not open return immediately If SStatus <> "Open" Then Exit Function ' If no current mail message then return immediately If MStatus <> "Open" Then Exit Function AddAttachment = True On Error GoTo MessError FName.MAPIMessages1.AttachmentIndex = FName.MAPIMessages1.AttachmentCount ' Update count of attachments If AttMode = DataFile Then FName.MAPIMessages1.AttachmentType = 0 If AttMode = OLEEmbedded Then FName.MAPIMessages1.AttachmentType = 1 If AttMode = OLEStatic Then FName.MAPIMessages1.AttachmentType = 2 FName.MAPIMessages1.AttachmentPosition = FName.MAPIMessages1.AttachmentIndex FName.MAPIMessages1.AttachmentPathName = APath ' File or object path as provided FName.MAPIMessages1.AttachmentName = AName ' File or object name as provided Exit Function MessError: AddAttachment = False End Function ' From my registry read module - just to get the default ' exchange user name (profile name) ' Public Function ReadRegistry(ByVal Group As Long, ByVal Section As String, ByVal Key As String) As String Dim lResult As Long, lKeyValue As Long, lDataTypeValue As Long, lValueLength As Long, sValue As String On Error Resume Next lResult = RegOpenKey(Group, Section, lKeyValue) sValue = Space$(2048) lValueLength = Len(sValue) lResult = RegQueryValueEx(lKeyValue, Key, 0&, lDataTypeValue, sValue, lValueLength) If (lResult = 0) And (Err.Number = 0) Then sValue = Left$(sValue, lValueLength - 1) Else sValue = "Not Found" End If lResult = RegCloseKey(lKeyValue) ReadRegistry = sValue End Function |
| Convertir Texto a Mayúsculas o Minúsculas |
| El siguiente código usa
funciones API para convertir automáticamente TODOS los ingresos de
texto en una form a mayúsculas o minúsculas
' Las siguientes sentencias incluir en un Módulo (.BAS)
|
| Rutinas Para Ordenar stings |
|
Aquí están el Quick, Merge e Insert Sort, recomiendo el uso de Quicksort tal como dicen los comentarios, lo he probado en grandes arrays y anda muy bien
|
| Clase de Utilidades para ADO |
| VERSION 1.0 CLASS BEGIN MultiUse = -1 'True Persistable = 0 'NotPersistable DataBindingBehavior = 0 'vbNone DataSourceBehavior = 0 'vbNone MTSTransactionMode = 0 'NotAnMTSObject END Attribute VB_Name = "AdoUtils" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = True Attribute VB_PredeclaredId = False Attribute VB_Exposed = False 'INCLUDE A REFERENCE TO MICROSOFT ACTIVE DATA OBJECTS 'IN ORDER TO USE THIS CLASS Option Explicit Private m_sConnectionString As String Private m_sLastError As String Private m_bIsSQL As Boolean Public Property Get ConnectionString() As String ConnectionString = m_sConnectionString End Property Public Property Let ConnectionString(ByVal NewValue As String) 'for some of the functions in this class 'you will need to firsta 'set this property to a 'valid connection string 'or reset it when you need to change the connection m_sConnectionString = NewValue End Property Public Property Get isSQL() As Boolean isSQL = m_bIsSQL End Property Public Property Let isSQL(ByVal NewValue As Boolean) 'SET TO TRUE IF YOU ARE USING SQL SERVER 'DEFAULT IS ACCESS m_bIsSQL = NewValue End Property Public Function Clone(ByVal objRecordset As ADODB.Recordset, Optional ByVal LockType As ADODB.LockTypeEnum = adLockBatchOptimistic) As ADODB.Recordset 'RETURNS A CLONE (COPY OF AN EXISTING RECORDSET) Dim objNewRS As ADODB.Recordset Dim objField As Object Dim lngCnt As Long On Error GoTo LocalError Set objNewRS = New ADODB.Recordset objNewRS.CursorLocation = adUseClient objNewRS.LockType = LockType For Each objField In objRecordset.Fields objNewRS.Fields.Append objField.Name, objField.Type, objField.DefinedSize, objField.Attributes Next objField If Not objRecordset.RecordCount = 0 Then Set objNewRS.ActiveConnection = objRecordset.ActiveConnection objNewRS.Open objRecordset.MoveFirst While Not objRecordset.EOF objNewRS.AddNew For lngCnt = 0 To objRecordset.Fields.Count - 1 objNewRS.Fields(lngCnt).Value = objRecordset.Fields(lngCnt).Value Next lngCnt objRecordset.MoveNext Wend objNewRS.MoveFirst End If Set Clone = objNewRS Exit Function LocalError: m_sLastError = Err.Number & " - " & Err.Description If objNewRS.State = adStateOpen Then objNewRS.Close End If Set objNewRS = Nothing End Function Function Datashape(ByVal tblParent As String, _ ByVal tblChild As String, _ ByVal fldParent As String, _ ByVal fldChild As String, _ Optional ordParent As String = "", _ Optional ordChild As String = "", _ Optional WhereParent As String = "", _ Optional WhereChild As String = "", _ Optional ParentFields As String = "*", _ Optional ChildFields As String = "*", _ Optional MaxRecords As Long = 0) As ADODB.Recordset '========================================================= 'This function will return a DisConnected SHAPEd RecordSet 'Assumptions: ' 'tblParent = Valid Table in the Database - String \ Parent Table 'tblChild = Valid Table in the Database - String / Child Table ' 'fldParent = Valid Field in Parent Table - String \ relate this field 'fldChild = Valid Field in Child Table - String / ..to this field ' 'ordParent = Valid Field in Parent Table - String \ ordering 'ordChild = Valid Field in Child Table - String / ' 'WhereParent = Valid SQL Where Clause - Variant (Optional) 'WhereChild = Valid SQL Where Clause - Variant (Optional) ' 'ParentFields = Specific Fields to return - String (pipe delimitered) 'ChildFields = Specific Fields to return - String (pipe delimitered) 'MaxRecords = Specify Maximum Child Records - Long (0 = ALL) 'NOTE: You may have to change connection string: Normal Connection Strings 'Begin with "Provider=". For the MsDataShape Provider, the connection string 'begins with "Data Provider = " 'EXAMPLE: THIS RETURNS A HYPOTHETICAL RECORDSET OF CUSTOMERS, 'WHERE ONE OF THE MEMBERS IS A HYPOTHETICAL CHILD RECORDSET 'OF THE CUSTOMERS' ORDERS 'Dim sShapeConnectionString As String 'Dim oCustRs As ADODB.Recordset 'Dim oOrderRs As ADODB.Recordset 'Dim oADO As New AdoUtils 'Dim sSQL As String 'sShapeConnectionString = "Data Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyBusiness.mdb" 'sSQL = "SELECT * FROM CUSTOMERS" 'With oTest ' .ConnectionString = sShapeConnectionString ' Set oCustRs = .Datashape("Customers", "Orders", "ID", "CustomerID") ' Set oOrderRs = ors.Fields(ors.Fields.Count - 1).Value 'End With '========================================================= On Error GoTo ErrHandler Dim cn As ADODB.Connection Dim rs As ADODB.Recordset Dim lSQL As String Dim pSQL As String Dim cSQL As String Dim pWhere As String Dim cWhere As String Dim pOrder As String Dim cOrder As String 'Define the SQL Statement lSQL = "" ParentFields = Replace(ParentFields, "|", ", ") ChildFields = Replace(ChildFields, "|", ", ") pWhere = WhereParent cWhere = WhereChild pOrder = ordParent cOrder = ordChild If WhereParent <> "" Then WhereParent = " WHERE " & WhereParent If WhereChild <> "" Then WhereChild = " WHERE " & WhereChild If pOrder <> "" Then pOrder = " ORDER By " & pOrder If cOrder <> "" Then cOrder = " ORDER By " & cOrder 'Define Parent SQL Statement pSQL = "" If MaxRecords > 0 Then If isSQL Then pSQL = pSQL & "{SET ROWCOUNT " & MaxRecords & " SELECT [@PARENTFIELDS]" Else pSQL = pSQL & "{SELECT TOP " & MaxRecords & " [@PARENTFIELDS]" End If Else pSQL = pSQL & "{SELECT " & "[@PARENTFIELDS]" End If pSQL = pSQL & " FROM [@PARENT]" pSQL = pSQL & " [@WHEREPARENT]" pSQL = pSQL & " [@ORDPARENT]} " 'Substitute for actual values pSQL = Replace(pSQL, "[@PARENTFIELDS]", ParentFields) pSQL = Replace(pSQL, "[@PARENT]", tblParent) pSQL = Replace(pSQL, "[@WHEREPARENT]", pWhere) pSQL = Replace(pSQL, "[@ORDPARENT]", pOrder) pSQL = Trim(pSQL) 'Define Child SQL Statement cSQL = "" cSQL = cSQL & "{SELECT " & "[@CHILDFIELDS]" cSQL = cSQL & " FROM [@CHILD]" cSQL = cSQL & " [@WHERECHILD]" cSQL = cSQL & " [@ORDCHILD]} " 'Substitute for actual values cSQL = Replace(cSQL, "[@CHILDFIELDS]", ChildFields) cSQL = Replace(cSQL, "[@CHILD]", tblChild) cSQL = Replace(cSQL, "[@WHERECHILD]", cWhere) cSQL = Replace(cSQL, "[@ORDCHILD]", cOrder) cSQL = Trim(cSQL) 'Define Parent Properties lSQL = "SHAPE " & pSQL & vbCrLf 'Define Child Properties lSQL = lSQL & "APPEND (" & cSQL & " RELATE " & fldParent & " TO " & fldChild & ") AS ChildItems" 'Get the data Set cn = New ADODB.Connection With cn .ConnectionString = ConnectionString .CursorLocation = adUseServer .Provider = "MSDataShape" .Open End With Set rs = New ADODB.Recordset With rs .CursorType = adOpenForwardOnly .LockType = adLockReadOnly .Source = lSQL .ActiveConnection = cn .Open End With Set rs.ActiveConnection = Nothing cn.Close Set cn = Nothing Set Datashape = rs Set rs = Nothing Exit Function ErrHandler: If Not cn Is Nothing Then If cn.State = adStateOpen Then cn.Close Set cn = Nothing End If m_sLastError = Err.Number & " - " & Err.Description End Function Public Function EmptyRS(ByVal adoRS As ADODB.Recordset) As Boolean 'Checks for an EMPTY RecordSet On Error GoTo ErrHandler EmptyRS = True If Not adoRS Is Nothing Then EmptyRS = ((adoRS.BOF = True) And (adoRS.EOF = True)) End If Exit Function ErrHandler: m_sLastError = Err.Number & " - " & Err.Description EmptyRS = True End Function Public Function Execute(SQL As String) As Boolean 'TO DIRECTLY EXECUTE AN INSERT, UPDATE, OR DELETE 'SQL STATMENT. SET THE CONNECTION STRING PROPERTY 'TO A VALID CONNECTION STRING FIRST On Error GoTo LocalError Dim cn As New ADODB.Connection With cn .ConnectionString = ConnectionString .CursorLocation = adUseServer .Open .BeginTrans .Execute SQL .CommitTrans .Close End With Set cn = Nothing Execute = True Exit Function LocalError: m_sLastError = Err.Number & " - " & Err.Description If cn.State = adStateOpen Then cn.RollbackTrans cn.Close End If Set cn = Nothing Execute = False End Function Public Function GetRS(SQL As String) As ADODB.Recordset 'SET THE CONNECTION STRING PROPERTY TO A VALID CONNECTION STRING 'PASS AN SQL STATEMENT TO THIS FUNCTION 'THE RETURN VALUE WILL BE AN ADODB RECORDSET Dim rs As New ADODB.Recordset On Error GoTo LocalError With rs .ActiveConnection = ConnectionString .CursorLocation = adUseClient .LockType = adLockOptimistic .CursorType = adOpenKeyset .Source = SQL .Open Set .ActiveConnection = Nothing End With Set GetRS = rs Set rs = Nothing Exit Function LocalError: m_sLastError = Err.Number & " - " & Err.Description Set rs = Nothing End Function Public Function GetCount(TableName As String, Optional WhereClause As String = "") As Long 'RETURNS COUNT OF RECORDS WITHIN A TABLE, WITH OPTIONAL WHERE CLAUSE On Error GoTo LocalError Dim rs As New ADODB.Recordset Dim SQL As String GetCount = 0 If WhereClause <> "" Then SQL = "Select COUNT (*) FROM " & TableName & " WHERE " & WhereClause Else SQL = "Select COUNT (*) FROM " & TableName End If With rs .ActiveConnection = ConnectionString .CursorLocation = adUseClient .LockType = adLockReadOnly .CursorType = adOpenKeyset .Source = SQL .Open Set .ActiveConnection = Nothing End With GetCount = rs.Fields(0).Value Set rs = Nothing Exit Function LocalError: m_sLastError = Err.Number & " - " & Err.Description If rs.State = adStateOpen Then rs.Close End If Set rs = Nothing GetCount = -1 End Function Public Function PutRS(rs As ADODB.Recordset) As Boolean 'USE THIS TO UPDATE A RECORDSET IN BATCH (TRANSACTIONAL) MODE 'IF CHANGES TO THE RECORDSET'S WERE MADE PRIOR TO THIS CALL 'THIS FUNCTION WILL COMMIT THEM TO THE UNDERYLING DATABASE On Error GoTo LocalError PutRS = False If EmptyRS(rs) Then Exit Function ElseIf rs.LockType = adLockReadOnly Then Exit Function Else Dim cn As New ADODB.Connection With cn .ConnectionString = ConnectionString .CursorLocation = adUseServer .Open .BeginTrans End With With rs .ActiveConnection = cn .UpdateBatch cn.CommitTrans Set .ActiveConnection = Nothing End With cn.Close Set cn = Nothing End If PutRS = True Exit Function LocalError: m_sLastError = Err.Number & " - " & Err.Description If cn.State = adStateOpen Then cn.RollbackTrans cn.Close End If Set cn = Nothing PutRS = False End Function Public Function sqlBoolean(TrueFalse As Boolean) As Integer 'CONVERTS BIT RETURN VALUE FROM SQL SERVER 'This is because SQL True = 1 'VB True = -1 sqlBoolean = TrueFalse If isSQL Then If TrueFalse = True Then sqlBoolean = TrueFalse * TrueFalse End If End Function Public Function sqlDate(ByVal vDate As Variant) As String 'THIS FUNCTION TAKES VALUES THAT ARE POSSIBLE 'DATES AND FORMATS THEM PROPERFOR INSERTION INTO 'DATABASE COLUMNS DEFINED AS DATES On Error GoTo LocalError 'Remove all invalid characters vDate = Trim(CStr(vDate)) vDate = Replace(vDate, "#", "") vDate = Replace(vDate, "'", "") vDate = Replace(vDate, Chr(34), "") '-------------------------------------- 'Convert the Date to a Double Precision ' for international compatability '-------------------------------------- sqlDate = "" 'First see in what format the data came ' Validate parameter If Not IsDate(vDate) Or IsNull(vDate) Then 'Maybe it is a number If IsNumeric(vDate) Then vDate = CDate(vDate) End If If Not IsDate(vDate) Then 'Still not a date Exit Function End If End If If isSQL Then 'Format is MM/DD/??YY sqlDate = Format(vDate, "mm\/dd\/yyyy") sqlDate = "'" & sqlDate & "'" Else 'Format by Regional Settings sqlDate = FormatDateTime(vDate, vbShortDate) sqlDate = "#" & sqlDate & "#" End If Exit Function LocalError: m_sLastError = Err.Number & " - " & Err.Description sqlDate = "" End Function Public Function sqlDateTime(ByVal vDate As Variant) As String 'THIS FUNCTION TAKES VALUES THAT ARE POSSIBLE 'DATES AND FORMATS THEM PROPERFOR INSERTION INTO 'DATABASE COLUMNS DEFINED AS DATE/TIMES On Error GoTo LocalError 'Remove all invalid characters vDate = Trim(CStr(vDate)) vDate = Replace(vDate, "#", "") vDate = Replace(vDate, "'", "") vDate = Replace(vDate, Chr(34), "") '-------------------------------------- 'Convert the Date to a Double Precision ' for international compatability '-------------------------------------- sqlDateTime = "" 'First see in what format the data came ' Validate parameter If Not IsDate(vDate) Or IsNull(vDate) Then 'Maybe it is a number If IsNumeric(vDate) Then vDate = CDate(vDate) End If If Not IsDate(vDate) Then 'Still not a date Exit Function End If End If If isSQL Then 'Format is MM/DD/??YY HH:MM:SS sqlDateTime = Format(vDate, "mm\/dd\/yyyy hh\:mm\:ss") sqlDateTime = "'" & sqlDateTime & "'" Else 'Format by Regional Settings sqlDateTime = FormatDateTime(vDate, vbShortDate) & " " & Format(vDate, "hh\:mm\:ss") sqlDateTime = "#" & sqlDateTime & "#" End If Exit Function LocalError: m_sLastError = Err.Number & " - " & Err.Description sqlDateTime = "" End Function Public Function sqlTime(ByVal vDate As Variant) As String 'THIS FUNCTION TAKES VALUES THAT ARE POSSIBLE 'DATES AND FORMATS THEM PROPERFOR INSERTION INTO 'DATABASE COLUMNS DEFINED AS TIME ONLY On Error GoTo LocalError 'Remove all invalid characters vDate = Trim(CStr(vDate)) vDate = Replace(vDate, "#", "") vDate = Replace(vDate, "'", "") vDate = Replace(vDate, Chr(34), "") '-------------------------------------- 'Convert the Date to a Double Precision ' for international compatability '-------------------------------------- sqlTime = "" 'First see in what format the data came ' Validate parameter If Not IsDate(vDate) Or IsNull(vDate) Then 'Maybe it is a number If IsNumeric(vDate) Then vDate = CDate(vDate) End If If Not IsDate(vDate) Then 'Still not a date Exit Function End If End If If isSQL Then 'Format is MM/DD/??YY HH:MM:SS sqlTime = FormatDateTime(vDate, vbLongTime) sqlTime = "'" & sqlTime & "'" Else 'Format by Regional Settings sqlTime = FormatDateTime(vDate, vbLongTime) sqlTime = "#" & sqlTime & "#" End If Exit Function LocalError: m_sLastError = Err.Number & " - " & Err.Description sqlTime = "" End Function Public Function sqlEncode(sqlValue) As String 'IF A STRING VALUE IN AN SQL STATMENT HAS A ' CHARACTER, 'USE THIS FUNCTION SO THE STRING CAN BE USED IN THE STATEMENT sqlEncode = Replace(sqlValue, "'", "''") End Function Public Property Get LastError() As String 'IF AN ERROR OCCURS IN CALLING ONE OF THE FUNCTIONS IN THIS CLASS 'READ THIS PROPERTY TO SEE WHAT THE ERROR WAS LastError = m_sLastError m_sLastError = "" End Property Public Function ExecuteID(SQL As String) As Long 'PURPOSE: RETURN VALUE OF IDENTITY COLUMN 'OF A NEWLY INSERTED RECORD 'SQL is a valid Insert statement. 'ConnetionString properyt has been set to a valid Connection String 'Tested on SQL7 as well as ACCESS 2000 using Jet4 On Error GoTo LocalError Dim cn As New ADODB.Connection Dim rs As New ADODB.Recordset Dim AutoID As Long With rs 'Prepare the RecordSet .CursorLocation = adUseServer .CursorType = adOpenForwardOnly .LockType = adLockReadOnly .Source = "SELECT @@IDENTITY" End With With cn .ConnectionString = ConnectionString .CursorLocation = adUseServer .Open .BeginTrans .Execute SQL, , adCmdText + adExecuteNoRecords With rs .ActiveConnection = cn .Open , , , , adCmdText AutoID = rs(0).Value .Close End With .CommitTrans .Close End With Set rs = Nothing Set cn = Nothing 'If we get here ALL was Okay ExecuteID = AutoID Exit Function LocalError: m_sLastError = Err.Number & " - " & Err.Description ExecuteID = 0 End Function |