[size="1"]Private Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Const SW_SHOWNORMAL = 1
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
(ByVal lpFileName As String, ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private strUDLFile As String
Private Sub Command1_Click()
Dim blnUDLFileOpen As Boolean
Dim strReturnedConnStr As String
strUDLFile = "C:\tempudlfile.udl"
blnUDLFileOpen = True
' The open statement creates a file if it doesn't exist. Other than
' this, you could use the CreateFile API call to create the UDL file.
Open strUDLFile For Output As #1
Close #1
' Put a temporary pause in here giving the file a chance to close
' properly before attempting the next line.
Sleep 600
' Use the shellexecute api call to open the file up. You might want to
' look at the SetWindowPos api call to keep this the topmost window also
ShellExecute Me.hwnd, vbNullString, strUDLFile, _
vbNullString, "C:\", SW_SHOWNORMAL
' Temporarily pause excecution of this code until the user has setup the
' connection string & closed the udl options file. The findwindow Api call
' looks for an open instance of the udl file window.
Do While blnUDLFileOpen = True
DoEvents
blnUDLFileOpen = CBool(FindWindow(vbNullString, _
"Data Link Properties") <> 0)
Loop
' Rename the udl file to a text file, I noticed that this fails if a text
' with this path & name exists, so I've put a check in to delete this first.
If (Len(CStr(Dir("C:\tempconnstr.txt"))) > 0) Then
Kill "C:\tempconnstr.txt"
Sleep 600
End If
Name strUDLFile As "C:\tempconnstr.txt"
' Open the file again, this time reading the last line (i.e. the line
' with the connection string in), into a string variable.
Open "C:\tempconnstr.txt" For Input As #1
Do Until EOF(1)
Line Input #1, strReturnedConnStr
Loop
Close #1
End Sub[/size]