Using Win32 functions in Visual FoxPro Image Gallery
ODBC API
..msdn
SQLAllocEnv
SQLAllocHandle
SQLBindCol
SQLBrowseConnect
SQLConfigDataSource
SQLConnect
SQLCreateDataSource
SQLDataSources
SQLDisconnect
SQLDriverConnect
SQLDrivers
SQLExecDirect
SQLFetch
SQLFreeEnv
SQLFreeHandle
SQLGetConfigMode
SQLGetConnectAttr
SQLGetDiagRec
SQLGetEnvAttr
SQLGetFunctions
SQLGetInfo
SQLGetInstalledDrivers
SQLGetPrivateProfileString
SQLInstallerError
SQLManageDataSources
SQLNativeSql
SQLRowCount
SQLSetConfigMode
SQLSetEnvAttr
SQLTables
Code examples:
Adding an ODBC data source with the SQLConfigDataSource; use automatic or interactive mode
How to display a dialog box with which the user can add a data source (DSN)
Adding an ODBC data source with the SQLConfigDataSource; use automatic or interactive mode

User rating: 10/10 (1 votes)
Rate this code sample:
  • ~
More code examples    Listed functions    Add comment     W32 Constants      Translate this page Printer friendly version of this code sample
Before you begin:

The dialog box is displayed only if you provide a valid window handle to the SQLConfigDataSource. If this parameter is zero, all changes are done automatically, with no user interaction.
 
#DEFINE ODBC_ADD_DSN        1
#DEFINE ODBC_CONFIG_DSN     2
#DEFINE ODBC_REMOVE_DSN     3
#DEFINE ODBC_ADD_SYS_DSN    4
#DEFINE ODBC_CONFIG_SYS_DSN 5
#DEFINE ODBC_REMOVE_SYS_DSN 6
 
DO decl
 
LOCAL hWindow, cDriver, cAttributes
 
* sample connection strings
* use one at a time :)
 
cDriver = "Microsoft Excel Driver (*.xls)"
cAttributes =;
    "DSN=NewXls" + Chr(0) +;
    "Description=Sample Excel Data Source" + Chr(0) +;
    "FileType=Excel" + Chr(0) +;
    "DataDirectory=C:\ExcelData" + Chr(0) +;
    "MaxScanRows=20" + Chr(0) + Chr(0)
 
cDriver = "Microsoft Access Driver (*.mdb)"
cAttributes =;
    "DSN=NewAccess" + Chr(0) +;
    "Description=Sample Data Source" + Chr(0) +;
    "Dbq=c:\db\test.mdb" + Chr(0) +;
    "Uid=sa" + Chr(0) +;
    "Pwd=" + Chr(0)
 
* accessing VFP database
cDriver = "Microsoft Visual FoxPro Driver"
cAttributes =;
    "DSN=NewVFP1" + Chr(0) +;
    "Description=Sample VFP Data Source" + Chr(0) +;
    "SourceDB=C:\data\vfp\receivable\prod.dbc" + Chr(0) +;
    "SourceType=DBC" + Chr(0) +;
    "Collate=Machine" + Chr(0) +;
    "BackgroundFetch=Yes" + Chr(0) +;
    "Exclusive=No" + Chr(0)
 
* accessign VFP free tables in a directory
cDriver = "Microsoft Visual FoxPro Driver"
cAttributes =;
    "DSN=NewVFP2" + Chr(0) +;
    "Description=Sample VFP Data Source" + Chr(0) +;
    "SourceDB=C:\data\vfp\payroll" + Chr(0) +;
    "SourceType=DBF" + Chr(0) +;
    "Collate=Machine" + Chr(0) +;
    "BackgroundFetch=Yes" + Chr(0) +;
    "Exclusive=No" + Chr(0)
 
* depending on window handle passed to the SQLConfigDataSource
* it either shows a dialog box on the screen (valid hWindow),
* or not when hWindow=0
 
hWindow = GetActiveWindow()
*hWindow = 0
 
IF SQLConfigDataSource(hWindow,;
    ODBC_ADD_DSN, cDriver, cAttributes) = 0
    = MessageBox(GetErr(), 48, " Error")
ENDIF
* end of main
 
FUNCTION GetErr
* returns error info for the ODBC installer functions
#DEFINE SQL_SUCCESS  0
#DEFINE SQL_SUCCESS_WITH_INFO 1
 
    LOCAL nIndex, cMsg, nBufsize, nErrCode,;
        cResult, nResult
 
    cResult = ""
    FOR nIndex = 1 TO 8
        nBufsize = 512
        cMsg = Repli(Chr(0), nBufsize)
        nErrCode = 0
 
        nResult = SQLInstallerError(nIndex, @nErrCode,;
            @cMsg, nBufsize, @nBufsize)
 
        IF INLIST(nResult, SQL_SUCCESS, SQL_SUCCESS_WITH_INFO)
            cMsg = SUBSTR(cMsg, 1, AT(Chr(0), cMsg)-1)
            cResult = cResult + Iif(nIndex=1, "", Chr(13)+Chr(13)) +;
                "Error: " + LTRIM(STR(nErrCode)) + Chr(13) + cMsg + ".     "
        ELSE
            EXIT
        ENDIF
    ENDFOR
RETURN cResult
 
PROCEDURE decl
    DECLARE INTEGER GetActiveWindow IN user32
 
    DECLARE INTEGER SQLConfigDataSource IN odbccp32;
        INTEGER hwndParent, INTEGER fRequest,;
        STRING lpszDriver, STRING lpszAttributes
 
    DECLARE INTEGER SQLInstallerError IN odbccp32;
        INTEGER iError, INTEGER @pfErrorCode, STRING @lpszErrorMsg,;
        INTEGER cbErrorMsgMax, INTEGER @pcbErrorMsg
 
 
 

User rating: 10/10 (1 votes)
Rate this code sample:
  • ~
2846 bytes  
Created: 2003-02-03 10:06:11  
Modified: 2009-01-03 15:26:33  
Visits in 7 days: 102  
Listed functions:
GetActiveWindow
SQLConfigDataSource
SQLInstallerError
Printer friendly API declarations
My comment:
As you can try sometimes (or every time) the automatic mode creates a data source without testing a validity of its path, file names or other parameters.

There is an alternative way, when you directly change Windows Registry records. I would not recommend it for two reasons: increased amount of code, and a certain risk of missing important details. Though I have been testing this option -- it worked more or less Ok.

Using this way you have to add or modify records in
HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI and
HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources sections.
Word Index links for this example:
Translate this page:
  Spanish    Portuguese    German    French    Italian  
FreeTranslation.com offers instant, free translations of text or web pages.
User Contributed Notes:
There are no notes on this subject.


Copyright © 2001-2013 News2News, Inc. Before reproducing or distributing any data from this site please ask for an approval from its owner. Unless otherwise specified, this page is for your personal and non-commercial use. The information on this page is presented AS IS, meaning that you may use it at your own risk. Microsoft Visual FoxPro and Windows are trade marks of Microsoft Corp. All other trademarks are the property of their respective owners. 

Privacy policy
Credits: PHP (4.4.9), an HTML-embedded scripting language, MySQL (5.1.55-log), the Open Source standard SQL database, AceHTML Freeware Version 4, freeware HTML Editor of choice.   Hosted by Korax Online Inc.
Last Topics Visited (50.16.36.153)
3 sec.Example: 'Changing system colors'
6 sec.
Function group: 'National Language Support'
38.37 min.Example: 'Enumerating the subkeys for a given registry key'
38.43 min.Example: 'Form Magnifier'
57.87 min.Example: 'Creating the Open dialog box to specify the drive, directory, and name of a file to open'
57.93 min.Example: 'Validating the heap of the calling process'
1.07 hrs.Example: 'How to change the name and the size of the font in the MessageBox dialog'
 Example: 'Minimizing all running applications'
1.08 hrs.Function: 'GetObjectType'
Function group: 'Device Context'
2.54 hrs.Example: 'How to make a VFP form fading out when released (GDI version)'
Google
Advertise here!