VB Code:
Const ODBC_ADD_SYS_DSN = 4 'Add System data source Const ODBC_ADD_DSN = 1 'Add User data source Const ODBC_CONFIG_SYS_DSN = 5 'Configure (edit) data source Const ODBC_REMOVE_SYS_DSN = 6 'Remove data source Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal _ hwndParent As Long, ByVal fRequest As Long, ByVal _ lpszDriver As String, ByVal lpszAttributes As String) As Long Public Enum enumDSN_Type eUserDSN = ODBC_ADD_DSN eSystemDSN = ODBC_ADD_SYS_DSN End Enum Public Function CreateDSN(pDSNName As String, pDBPath As String, pDSNType As enumDSN_Type) As Boolean Dim lngRetVal Dim strDriver As String Dim strAttributes As String CreateDSN = False strDriver = "Microsoft Access Driver (*.mdb)" & Chr(0) strAttributes = "DSN=" & pDSNName & Chr(0) strAttributes = Attributes & "Uid=Admin" & Chr(0) & "pwd=" & Chr(0) strAttributes = Attributes & "DBQ=" & pDBPath & Chr(0) lngRetVal = SQLConfigDataSource(0, pDSNType, strDriver, strAttributes) 'If the Return Value 1 then 'DataSource is created succesefully 'otherwise it has falied CreateDSN = lngRetVal End Function
You can call this routine like this:
VB Code:
Private Sub Command1_Click() Call CreateDSN("MyDSN", "C:\MyDB.mdb", eSystemDSN) 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.




Reply With Quote