devxlogo

CUDL – A class to create UDL files

CUDL – A class to create UDL files

' 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.OpenUDLPrivate Const CON_UDL_LINE1 = "[oledb]"Private Const CON_UDL_LINE2 = "; Everything after this line is an OLE DB " _    & "initstring"Private mvarADOConnectString As StringPublic Property Let ADOConnectString(ByVal vData As String)    mvarADOConnectString = vDataEnd PropertyPublic Property Get ADOConnectString() As String    ADOConnectString = mvarADOConnectStringEnd PropertyPublic Function CreateUDL() As Boolean    On Error GoTo Err_CreateUDL    Dim pDataLink As New DataLinks, pConn As Connection        Set pConn = pDataLink.PromptNew    pConn.Open    mvarADOConnectString = pConn.ConnectionString    pConn.Close        CreateUDL = True    Exit FunctionErr_CreateUDL:    CreateUDL = FalseEnd FunctionPublic 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    Else        pConn.ConnectionString = mvarADOConnectString        If pDataLink.PromptEdit(pConn) Then            pConn.Open            mvarADOConnectString = pConn.ConnectionString            pConn.Close        End If    End If    OpenUDL = True    Exit FunctionErr_OpenUDL:    OpenUDL = FalseEnd FunctionPublic Sub SaveToFile(ByVal strUDLFile As String)    Dim strBuf As String, intFileNum As Integer        strBuf = CON_UDL_LINE1 & vbCrLf & CON_UDL_LINE2 & vbCrLf & _        mvarADOConnectString    strBuf = StrConv(strBuf, vbUnicode)    intFileNum = FreeFile    If Dir(strUDLFile) <> vbNullString Then Kill strUDLFile    Open strUDLFile For Binary As #intFileNum    Put #intFileNum, , strBuf    Close #intFileNumEnd SubPublic 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    Else        LoadFromFile = False    End IfEnd Function'##############################################'# This tip has been brought to you by Shoutsoft, Inc, '# the creators of COM Express, a tool which '# generates serious N(3)-tier VB application'# (with GUI, COM+ and Unlimited Hierarchical support) '# in minutes. Learn more at http://www.shoutsoft.com.'##############################################

See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist