Try the following bit of code, call the fCreate_DSN function and pass in the name of the DSN you wish to create as well as server name and Database. It assumes a few things but you can probably see the options for yourself in the code.
The function will also check for existing DSN and only create if needed
Cheers
Private Declare Function SQLConfigDataSource Lib "odbccp32.dll" _
(ByVal hwndParent As Long, _
ByVal fRequest As Integer, _
ByVal lpszDriver As String, _
ByVal lpszAttributes As String) As Long
Private Declare Function RegOpenKeyEx _
Lib "advapi32.dll" Alias "RegOpenKeyExA" _
(ByVal hKey As Long, _
ByVal lpSubKey As String, _
ByVal ulOptions As Long, _
ByVal samDesired As Long, _
phkResult As Long) As Long
Private Declare Function RegEnumKeyEx Lib "advapi32.dll" _
Alias "RegEnumKeyExA" _
(ByVal hKey As Long, _
ByVal dwIndex As Long, _
ByVal lpName As String, _
lpcbName As Long, _
ByVal lpReserved As Long, _
ByVal lpClass As String, _
lpcbClass As Long, _
ByVal lpftLastWriteTime As String) As Long
Const ODBC_ADD_SYS_DSN = 4
Function fCreate_DSN(strDSN As String, strServer As String, strdb As String)
'============================================================
' Purpose: Create a new system DSN
' Programmer: Richard Jervis
' Date: 26/09/01
'============================================================
On Error GoTo fCreate_DSN_Err
Dim strErrMsg As String 'For Error Handling
Dim lngKeyHandle As Long
Dim lngResult As Long
Dim lngCurIdx As Long
Dim strValue As String
Dim classValue As String
Dim timeValue As String
Dim lngValueLen As Long
Dim classlngValueLen As Long
Dim lngData As Long
Dim lngDataLen As Long
Dim strResult As String
Dim DSNfound As Long
Dim syscmdresult As Long
'If fDoes_DSN_Exists(JDS_DSN_name) = True Then
syscmdresult = SysCmd(acSysCmdSetStatus, "Creating System DSN " & strDSN & "...")
lngResult = SQLConfigDataSource(0, _
ODBC_ADD_SYS_DSN, _
"SQL Server", _
"DSN=" & strDSN & Chr(0) & _
"Server=" & strServer & Chr(0) & _
"Database=" & strdb & Chr(0) & _
"UseProcForPrepare=Yes" & Chr(0) & _
"Trusted_Connection=Yes" & Chr(0) & _
"Description=Database" & Chr(0) & Chr(0))
If lngResult = False Then
MsgBox "ERROR: Could not create the System DSN " & strDSN & "." & vbCrLf & vbCrLf & _
"Please make sure that the SQL Server ODBC drivers have been installed." & vbCrLf & _
"Contact your IT Help Desk for more information."
End If
syscmdresult = SysCmd(acSysCmdClearStatus)
'End If
fCreate_DSN_Exit:
Exit Function
fCreate_DSN_Err:
Select Case Err
Case Else
strErrMsg = strErrMsg & "Error #: " & Format$(Err.Number) & vbCrLf & vbCrLf
strErrMsg = strErrMsg & "Error Description: " & Err.Description & vbCrLf
MsgBox strErrMsg, vbInformation, "Error in fCreate_DSN procedure"
Resume fCreate_DSN_Exit
End Select
End Function
Function fDoes_DSN_Exists(strDSNName As String) As Boolean
'============================================================
' Purpose: Search for system DSN
' Programmer: Richard Jervis
' Date: 26/09/01
'============================================================
On Error GoTo fDoes_DSN_Exists_Err
Dim strErrMsg As String 'For Error Handling
' Look for our System Data Source Name. If we find it, then great!
' If not, then let's create one on the fly.
Dim lngKeyHandle As Long
Dim lngResult As Long
Dim lngCurIdx As Long
Dim strValue As String
Dim classValue As String
Dim timeValue As String
Dim lngValueLen As Long
Dim classlngValueLen As Long
Dim lngData As Long
Dim lngDataLen As Long
Dim strResult As String
Dim syscmdresult As Long
syscmdresult = SysCmd(acSysCmdSetStatus, "Looking for System DSN " & strDSNName & " ...")
' Let's open the registry key that contains all of the
' System Data Source Names.
lngResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _
"SOFTWARE\ODBC\ODBC.INI", _
0&, _
KEY_READ, _
lngKeyHandle)
If lngResult <> ERROR_SUCCESS Then
MsgBox "ERROR: Cannot open the registry key HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI." & vbCrLf & vbCrLf & _
"Please make sure that ODBC and the SQL Server ODBC drivers have been installed." & vbCrLf & _
"Contact call your MDTS System Administrator for more information."
syscmdresult = SysCmd(acSysCmdClearStatus)
'Unable to open the registry, so we cannot check to see if the
'DSN exists. So we are going to assume that it does.
fDoes_DSN_Exists = True
End If
' Now that the key is open, Let's look among all of
' the possible system data source names for the one
' we want.
lngCurIdx = 0
fDoes_DSN_Exists = False
Do
lngValueLen = 512
classlngValueLen = 512
strValue = String(lngValueLen, 0)
classValue = String(classlngValueLen, 0)
timeValue = String(lngValueLen, 0)
lngDataLen = 512
lngResult = RegEnumKeyEx(lngKeyHandle, _
lngCurIdx, _
strValue, _
lngValueLen, _
0&, _
classValue, _
classlngValueLen, _
timeValue)
lngCurIdx = lngCurIdx + 1
If lngResult = ERROR_SUCCESS Then
' Is this our System Data Source Name?
If strValue = strDSNName Then
'DSN Found
fDoes_DSN_Exists = True
syscmdresult = SysCmd(acSysCmdClearStatus)
End If
End If
Loop While lngResult = ERROR_SUCCESS And Not fDoes_DSN_Exists
syscmdresult = SysCmd(acSysCmdClearStatus)
fDoes_DSN_Exists_Exit:
Exit Function
fDoes_DSN_Exists_Err:
Select Case Err
Case Else
strErrMsg = strErrMsg & "Error #: " & Format$(Err.Number) & vbCrLf & vbCrLf
strErrMsg = strErrMsg & "Error Description: " & Err.Description & vbCrLf
MsgBox strErrMsg, vbInformation, "Error in fDoes_DSN_Exists procedure"
Resume fDoes_DSN_Exists_Exit
End Select
End Function