Saturday, November 5, 2011

Construct the Connection String at Runtime


‘How to Prompt user to construct the connection string at runtime!!!
‘It uses the same interface that ADODC, DataEnvironment etc use.
‘References:‘1. Microsoft OLE DB Service Component 1.0‘2. Microsoft ADO 2.5
‘Visual Basic Code Below

Public Function GetConnectionString(Optional strOLE As String =“OLEDB”As StringOn Error GoTo ErrHandler
Dim Conn As ADODB.ConnectionDim ObjectDataLink As MSDASC.DataLinksSet Conn = New ADODB.ConnectionSet ObjectDataLink = New MSDASC.DataLinksSelect Case strOLECase “OLEDB”Conn = ObjectDataLink.PromptNewGetConnectionString = Conn.ConnectionStringCase “ODBC”Conn.ConnectionString = “”Conn.Properties(“Prompt”) = adPromptAlwaysConn.OpenGetConnectionString = Conn.ConnectionStringConn.CloseEnd SelectExit FunctionErrHandler:If Err.Number = 91 Or Err.Number = -2147217842 ThenExit FunctionElseMsgBox “Error: “ & Err.DescriptionEnd If
End Function

No comments:

Post a Comment