We can change this function to accept both Access and SQL Server:
Then you can call it like this: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 Enum enumDBType eACCESS = 0 eSQL_SERVER = 1 End Enum Public Function CreateDSN(p_DSNName As String, p_DSNType As enumDSN_Type, _ p_DBType As enumDBType, p_strDatabase As String, _ Optional p_strServer As String = "", _ Optional p_strUser As String = "", _ Optional p_strPassword As String = "") As Boolean Dim lngRetVal Dim strDriver As String Dim strAttributes As String CreateDSN = False Select Case p_DBType Case eACCESS strDriver = "Microsoft Access Driver (*.mdb)" & Chr$(0) strAttributes = "DSN=" & p_DSNName & Chr$(0) strAttributes = Attributes & "Uid=Admin" & Chr$(0) & "pwd=" & Chr$(0) strAttributes = Attributes & "DBQ=" & pDBPath & Chr$(0) Case eSQL_SERVER strDriver = "SQL Server" strAttributes = "SERVER=" & p_strServer & Chr$(0) strAttributes = strAttributes & "DESCRIPTION=" & Chr$(0) strAttributes = strAttributes & "DSN=" & p_DSNName & Chr$(0) strAttributes = strAttributes & "DATABASE=" & p_strDatabase & Chr$(0) strAttributes = strAttributes & "UID=" & p_strUser & Chr$(0) strAttributes = strAttributes & "PWD=" & p_strPassword & Chr$(0) End Select lngRetVal = SQLConfigDataSource(0, p_DSNType, strDriver, strAttributes) 'If the Return Value 1 then 'DataSource is created succesefully 'otherwise it has falied CreateDSN = lngRetVal End Function
VB Code:
Private Sub Command1_Click() Call CreateDSN("YourDSN", eSystemDSN, eSQL_SERVER, "YourDatabase", "ServerName", "sa") End Sub




Reply With Quote