Atlanta Custom Software Development 

   Search        Code/Page

User Login



Forgot the Password?
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
» Regular Expr Tester
» Free Tools

CUDL - A class to create UDL files
[ All Languages » VB »  ADO]

Total Hit ( 2009)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article



Click here to copy the following block
' A class that creates UDL files for use as ADO Data Sources.

' Universal Data link files are handy for select data sources but there
' are lack of a way to create/edit this files. The little program i
' attached teaches how to read/write a UDL file.

' IMPORTANT: requires that you add a reference to the following library:
'  Microsoft OLE DB Service Component 1.0 Type Llibrary
' in the oledb32.dll file

' Usage example:
'   ' To create a new UDL file.
'   Dim udl As New cUdl
'   If udl.CreateUDL Then udl.SaveToFile "c:\yourfile.udl"

'   ' To open and edit an existing UDL file
'   Dim udl As New cUdl
'   udl.LoadFromFile "c:\yourfile.udl"
'   udl.OpenUDL

Private Const CON_UDL_LINE1 = "[oledb]"
Private Const CON_UDL_LINE2 = "; Everything after this line is an OLE DB " _
  & "initstring"
Private mvarADOConnectString As String

Public Property Let ADOConnectString(ByVal vData As String)
  mvarADOConnectString = vData
End Property

Public Property Get ADOConnectString() As String
  ADOConnectString = mvarADOConnectString
End Property

Public Function CreateUDL() As Boolean
  On Error GoTo Err_CreateUDL
  Dim pDataLink As New DataLinks, pConn As Connection
  Set pConn = pDataLink.PromptNew
  mvarADOConnectString = pConn.ConnectionString
  CreateUDL = True
  Exit Function
  CreateUDL = False
End Function

Public Function OpenUDL(Optional ByVal strConnectString As String) As Boolean
  On Error GoTo Err_OpenUDL
  Dim pDataLink As New DataLinks, pConn As New Connection
  If mvarADOConnectString = vbNullString Then
    OpenUDL = CreateUDL
    Exit Function
    pConn.ConnectionString = mvarADOConnectString
    If pDataLink.PromptEdit(pConn) Then
      mvarADOConnectString = pConn.ConnectionString
    End If
  End If
  OpenUDL = True
  Exit Function
  OpenUDL = False
End Function

Public Sub SaveToFile(ByVal strUDLFile As String)
  Dim strBuf As String, intFileNum As Integer
  strBuf = CON_UDL_LINE1 & vbCrLf & CON_UDL_LINE2 & vbCrLf & _
  strBuf = StrConv(strBuf, vbUnicode)
  intFileNum = FreeFile
  If Dir(strUDLFile) <> vbNullString Then Kill strUDLFile
  Open strUDLFile For Binary As #intFileNum
  Put #intFileNum, , strBuf
  Close #intFileNum
End Sub

Public Function LoadFromFile(ByVal strUDLFile As String) As Boolean
  Dim intFileNum As Integer, strBuf As String, i As Integer
  intFileNum = FreeFile
  Open strUDLFile For Binary As #intFileNum
  strBuf = Input(LOF(intFileNum), #intFileNum)
  Close #intFileNum
  strBuf = StrConv(strBuf, vbFromUnicode)
  i = InStr(strBuf, "Provider=")
  If i > 0 Then
    mvarADOConnectString = Mid(strBuf, i)
    LoadFromFile = True
    LoadFromFile = False
  End If
End Function

Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with, SQL Server and other MS technologies. He is, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )

Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.