VB Code:
  1. Const ODBC_ADD_SYS_DSN = 4       'Add System data source
  2. Const ODBC_ADD_DSN = 1       'Add User data source
  3. Const ODBC_CONFIG_SYS_DSN = 5    'Configure (edit) data source
  4. Const ODBC_REMOVE_SYS_DSN = 6    'Remove data source
  5.  
  6. Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal _
  7.    hwndParent As Long, ByVal fRequest As Long, ByVal _
  8.    lpszDriver As String, ByVal lpszAttributes As String) As Long
  9.  
  10. Public Enum enumDSN_Type
  11.     eUserDSN = ODBC_ADD_DSN
  12.     eSystemDSN = ODBC_ADD_SYS_DSN
  13. End Enum
  14. Public Function CreateDSN(pDSNName As String, pDBPath As String, pDSNType As enumDSN_Type) As Boolean
  15.     Dim lngRetVal
  16.     Dim strDriver As String
  17.     Dim strAttributes As String
  18.    
  19.     CreateDSN = False
  20.      
  21.     strDriver = "Microsoft Access Driver (*.mdb)" & Chr(0)
  22.     strAttributes = "DSN=" & pDSNName & Chr(0)
  23.     strAttributes = Attributes & "Uid=Admin" & Chr(0) & "pwd=" & Chr(0)
  24.     strAttributes = Attributes & "DBQ=" & pDBPath & Chr(0)
  25.     lngRetVal = SQLConfigDataSource(0, pDSNType, strDriver, strAttributes)
  26.    
  27.     'If the Return Value 1 then
  28.     'DataSource is created succesefully
  29.     'otherwise it has falied
  30.     CreateDSN = lngRetVal
  31. End Function

You can call this routine like this:
VB Code:
  1. Private Sub Command1_Click()
  2.     Call CreateDSN("MyDSN", "C:\MyDB.mdb", eSystemDSN)
  3. End Sub

This will create a SYstem DSN for Access database.
By changing the driver type you can setup ODBC DSN for any type of database/server.